: This article mainly introduces the use of RBAC in Yii2. For more information about PHP tutorials, see. 1. in/basic/config/console. php and/basic/config/web. php, configure the component. here only the code in console. php is pasted:
'basic-console', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log', 'gii'], 'controllerNamespace' => 'app\commands', 'modules' => [ 'gii' => 'yii\gii\Module', ], 'components' => [ 'cache' => [ 'class' => 'yii\caching\FileCache', ], 'log' => [ 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], 'db' => $db,'authManager' => [ 'class' => 'yii\rbac\DbManager', 'itemTable' => 'web_auth_item', 'assignmentTable' => 'web_auth_assignment', 'itemChildTable' => 'web_auth_item_child', 'ruleTable'=>'web_auth_rule' ], ], 'params' => $params,];
If no configuration is available in console. php, the following error is reported:
You should configure "authManager" component to use database before executing this migration.
2. open the command line
3. switch the cd command to the/php/basic directory.
4. enter the command: yii migrate -- migrationPath = @ yii/rbac/migrations/
5. create Permission:
Public function createPermission ($ item) {$ auth = Yii: $ app-> authManager; $ createPost = $ auth-> createPermission ($ item ); $ createPost-> description = 'created '. $ item. 'license '; $ auth-> add ($ createPost );}
6. create a Role:
Public function createRole ($ item) {$ auth = Yii: $ app-> authManager; $ role = $ auth-> createRole ($ item ); $ role-> description = 'created '. $ item. 'role'; $ auth-> add ($ role );}
7. Role allocation Permission
static public function createEmpowerment($items) { $auth = Yii::$app->authManager; $parent = $auth->createRole($items['name']); $child = $auth->createPermission($items['description']); $auth->addChild($parent, $child); }
8. assign a role to a user:
static public function assign($item) { $auth = Yii::$app->authManager; $reader = $auth->createRole($item['name']); $auth->assign($reader, $item['description']); }
9. verify permissions:
Public function beforeAction ($ action) {$ action = Yii: $ app-> controller-> action-> id; if (\ Yii :: $ app-> user-> can ($ action) {return true;} else {throw new \ yii \ web \ UnauthorizedHttpException ('sorry, you have not yet obtained the permission for this operation ');}}
10. permission verification in Controller
class SiteController extends Controller{ public function behaviors() { return [ 'access' => [ 'class' => \yii\web\AccessControl::className(), 'only' => ['login', 'logout', 'signup'], 'rules' => [ [ 'actions' => ['login', 'signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], ]; } // ...
11. Custom Verification in Controller
class SiteController extends Controller{ public function behaviors() { return [ 'access' => [ 'class' => \yii\web\AccessControl::className(), 'only' => ['special-callback'], 'rules' => [ [ 'actions' => ['special-callback'], 'allow' => true, 'matchCallback' => function ($rule, $action) { return date('d-m') === '31-10'; } ],
//... // Match callback called! This page can only be accessed for every October 31 public function actionSpecialCallback () {return $ this-> render ('happy-Halloween ');}
The above introduces the use of RBAC in Yii2, including some content, and hope to be helpful to friends who are interested in PHP tutorials.