Giter VIP home page Giter VIP logo

yii2-rbac's Introduction

RBAC Manager for Yii 2

RBAC Auth manager for Yii2

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiier/yii2-rbac "*"

or add

"yiier/yii2-rbac": "*"

to the require section of your composer.json file.

Usage

配置

  • 修改配置文件,比如 修改 config/main.php, 添加:
return [
    'modules' => [
        'rbac' => [
            'class' => 'yiier\rbac\Module',
            // 'ignoreModules' => ['gii', 'debug', 'rbac'],
            // 'menus' => [
            //    'rbac' =>'RBAC',
            //    'permissions' =>'Permissions',
            //    'roles' =>'Roles',
            //    'users' =>'Users',
            //    'rules' =>'Rules',
            // ],
            // 'safeDelete' => false, // 默认为 true,当为 true 时,角色有用户时,不能直接删除角色,要先去掉角色的所有用户
            // 'rulesPath' => ['@app/rbac/rules'], // rules 路径
            // 'userClassName' => 'app\models\User',
            // 'idField' => 'id',
            // 'usernameField' => 'shop_name',
            // 'mainLayout' => '@app/views/layout/rbac.php',
            // 'isAdvanced' => true, // 如果使用的是高级模板
            // 'advancedConfigs' = [ //如果你还有其他应用需要加入控制
            //     [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@frontend/config/main.php',
            //           '@frontend/config/main-local.php',
            //      ],
            //      [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@api/config/main.php',
            //           '@api/config/main-local.php',
            //      ],
            //      [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@backend/config/main.php',
            //           '@backend/config/main-local.php',
            //      ],
            //    ],
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],
];
  • 执行数据库操作,生成表,命令如下:
php yii migrate --migrationPath=@yii/rbac/migrations/
  • 控制访问,修改配置文件,比如 修改 config/main.php,添加:
'components' => [
    // ...
],
'as access' => [
    'class' => 'yiier\rbac\components\AccessControl',
],

可选操作

  • 修改默认参数,修改 config/params.php 添加:
'yiier.rbac.config' => [
    'cacheDuration' => 3600, // 缓存时间,单位是秒。默认是 30 天
    'superManId' => 12 // 拥有所有权限的用户 ID
    'freeAccessRoutes' => ['/site/login', '/site/signup', '/site/error', '/site/captcha', '/site/logout', '/order/*'], // 白名单 URL
]
  • 使用 rbac 模块的自带菜单
echo  \yii\widgets\Menu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'fa-dashboard',
                'active' => Yii::$app->request->url === Yii::$app->homeUrl
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);
  • 使用 \yiier\rbac\helpers\Html::a ,判断是否有权限,如果没有权限自动隐藏链接
<?= \yiier\rbac\helpers\Html::a(Yii::t('rbac', 'Clear Cache'), ['clear-cache']) ?>
  • 使用 yiier\rbac\widgets\Menu::widget ,判断是否有权限,如果没有权限自动隐藏菜单中的链接
echo  yiier\rbac\widgets\Menu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'fa-dashboard',
                'active' => Yii::$app->request->url === Yii::$app->homeUrl
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);
  • 使用 yiier\rbac\widgets\SpuerMenu::widget ,判断是否有权限,如果没有权限自动隐藏菜单中的链接,并且能实现自动高亮:
echo  yiier\rbac\widgets\SpuerMenu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'dashboard',
            ],
            [
                'label' => Yii::t('app', 'Create Product'), 
                'icon' => 'plus', 
                'url' => ['/product/create'], 
                'strict' => true // 严格模式
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);

界面操作教程

  • 在『权限列表』中勾选你要控制的菜单,不勾选说明不打算加入权限控制

你也可以添加菜单的描述,更方便使用。描述会默认读取对应 actionXXX 方法中注释的 @title 权限列表

另外操作会自动立即保存,无需再点保存。

  • 在『角色管理』中,添加完角色之后再可以分别给角色分配用户和权限。操作的时候点击对应的权限/用户会立即自动保存。

  • 『用户列表』:只能看某个用户拥有的所有角色和所有权限

  • 『规则列表』:

待补充

Get√Yii Web development has never been so fun! https://getyii.com/

yii2-rbac's People

Contributors

forecho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-rbac's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.