phuongdev89/yii2-user-role

这是一个简单的Yii2用户角色

安装: 106

依赖者: 0

建议者: 0

安全性: 0

星星: 0

关注者: 0

分支: 0

类型:yii2-extension

1.2.0 2023-01-10 17:01 UTC

This package is auto-updated.

Last update: 2024-09-26 11:51:32 UTC


README

安装

composer require phuongdev89/yii2-user-role "@dev"

配置

backend/config/main.phpapp/config/web.php

[php]
'modules' => [
    'user' => [
        'class' => 'dektrium\user\Module',
        'modelMap' => [
            'User' => 'phuongdev89\role\models\User',//IMPORTANT & REQUIRED, change to your User model if overridden
            'LoginForm' => 'phuongdev89\role\models\forms\LoginForm',//IMPORTANT & REQUIRED, change to your User model if overridden
        ],
    ],
   'role' => [
        'class' => 'phuongdev89\role\Module',
        'controllers' => [ 
            //namespaces of controllers you want to control
            'app\controllers',
            'phuongdev89\role\controllers',
        ],
    ],
],

console/config/main.phpapp/config/console.php

'controllerMap' => [
    ...
    'migrate'  => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => [
            '@console/migrations',
            '@vendor/dektrium/yii2-user/migrations',
            '@phuongdev89/role/migrations',
        ],
    ],
],

运行迁移

php yii migrate/up

用法

在模型User中,如果您覆盖了它

class User extends \phuongdev89\role\models\User

在您想要检查角色的每个控制器中

class SiteController extends Controller {

    public function behaviors() {
        return [
            'verbs' => [
            ....
            ],
            'role'  => [
                'class'   => RoleFilter::className(),
                'name'    => 'Trang chủ', //NOT REQUIRED, only if you want to translate
                'actions' => [
                    'create', //without translate
                    'index' => 'Danh sách', //with translated, which will display on role _form
                ],
            ],
        ];
    }
}

在所有地方

use phuongdev89\role\helpers\RoleChecker;
...
//public static function isAuth($controller, $action = '', $role_id = null)
$boolean = RoleChecker::isAuth(SiteController::className(), 'index', Yii::$app->user->identity->getRoleId());