Heim >Backend-Entwicklung >PHP-Tutorial >Yii2 verwendet RBAC
1. Konfigurieren Sie Komponenten in /basic/config/console.php und /basic/config/web.php. Hier wird nur der Code in console.php veröffentlicht:
<?php Yii::setAlias('@tests', dirname(__DIR__) . '/tests'); $params = require(__DIR__ . '/params.php'); $db = require(__DIR__ . '/db.php'); return [ 'id' => '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, ];Wenn Konsole vorhanden ist Liegt keine Konfiguration in .php vor, wird der folgende Fehler gemeldet:
You should configure "authManager" component to use database before executing this migration.
2. Öffnen Sie die Befehlszeile
3.cd-Befehl zum Wechseln in das /php/basic-Verzeichnis
4. Geben Sie den Befehl ein: yii migrate --migrationPath=@yii/rbac/migrations/
5 >
6. Rolle erstellen:public function createPermission($item) { $auth = Yii::$app->authManager; $createPost = $auth->createPermission($item); $createPost->description = '创建了 ' . $item . ' 许可'; $auth->add($createPost); }
public function createRole($item) { $auth = Yii::$app->authManager; $role = $auth->createRole($item); $role->description = '创建了 ' . $item . ' 角色'; $auth->add($role); }
static public function createEmpowerment($items) { $auth = Yii::$app->authManager; $parent = $auth->createRole($items['name']); $child = $auth->createPermission($items['description']); $auth->addChild($parent, $child); }
static public function assign($item) { $auth = Yii::$app->authManager; $reader = $auth->createRole($item['name']); $auth->assign($reader, $item['description']); }
public function beforeAction($action) { $action = Yii::$app->controller->action->id; if(\Yii::$app->user->can($action)){ return true; }else{ throw new \yii\web\UnauthorizedHttpException('对不起,您现在还没获此操作的权限'); } }
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' => ['@'], ], ], ], ]; } // ...
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! 此页面可以访问只有每个10月31日 public function actionSpecialCallback() { return $this->render('happy-halloween'); }Das Obige stellt die Verwendung von RBAC in Yii2 vor, einschließlich verschiedener Aspekte. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.