phuongdev89 / yii2-user-role
这是一个简单的Yii2用户角色
1.2.0
2023-01-10 17:01 UTC
Requires
- dektrium/yii2-user: @dev
- phuongdev89/yii2-base: @dev
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.php
或app/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.php
或app/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());