第一步:在console/main.php裡面加入rbac 的配置:
資料庫帶表字首的使用如下配置:
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => '{{%auth_item}}',
'assignmentTable' => '{{%auth_assignment}}',
'itemChildTable' => '{{%auth_item_child}}',
'ruleTable' => '{{%auth_rule}}',
],
不帶表字首使用配置:
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => 'auth_item',
'assignmentTable' => 'auth_assignment',
'itemChildTable' => 'auth_item_child',
'ruleTable' => 'auth_rule',
],
第二步.進入yii網站的根目錄在指令行下執行:
yii migrate [email protected]/rbac/migrations
這樣資料庫多了四張表,
migration表也生成了對應的記錄資料:
第三步:如何自動根據目前的controller類和action自動生成資料填充表,這裡給大家寫了個類
<?php
namespace console\controllers;
use backend\models\Admin;
use yii\console\Controller;
class DataController extends Controller
{
public function actionInit()
{
$auth = \Yii::$app->authManager;
$db = \Yii::$app->db;
$connection = $db->createCommand();
$transaction = $db->beginTransaction();
try{
echo "============使用者填充【開始】============\n";
$admin = new Admin();
$admin->username = 'admin';
$admin->password_hash = '123456';
$admin->email = '[email protected]';
$admin->save(false);
echo '1111111111111111111';
echo "填充使用者名:{$admin['username']}\n";
echo "============使用者填充【完成】============\n";
echo "\n";
echo "============權限填充【開始】============\n";
$controllerPath = 'backend\\controllers\\';
foreach (scandir($controllerPath) as $fileName){
if ($fileName!='.' && $fileName!='..'){
$content = file_get_contents($controllerPath.$fileName);
//取控制器名稱
if (preg_match('/class (\w+)Controller/',$content,$mach1)){
$controller = strtolower($mach1[1]);
$connection->insert('{{%auth_item}}',[
'name' => $controller.'/*',
'type' => 2,
'description' => $controller.'/*',
'created_at' => time(),
'updated_at' => time(),
])->execute();
echo "填充權限".$controller."/*\n";
//取actions名稱
if (preg_match_all('/public function action(\w+)\(/',$content,$mach2)){
foreach ($mach2[1] as $action){
$connection->insert('{{%auth_item}}',[
'name' => $controller.'/'.strtolower($action),
'type' => 2,
'description' => $controller.'/'.strtolower($action),
'created_at' => time(),
'updated_at' => time(),
])->execute();
echo "--填充權限".$controller.'/'.strtolower($action)."\n";
}
}
}
}
}
echo "============權限填充【完成】============\n";
echo "\n";
echo "============角色填充【開始】============\n";
$datas = [
['name' => 'root','description'=>'超級管理者'],
['name' => 'admin','description'=>'管理者'],
['name' => 'tester','description'=>'測試人員'],
['name' => 'editor','description'=>'編輯'],
];
foreach ($datas as $data){
$role = $auth->createRole($data['name']);
$role->description = $data['description'];
$auth->add($role);
echo "填充角色{$data['name']}-{$data['description']}\n";
}
echo "============角色填充【完成】============\n";
echo "\n";
echo "============授權填充【開始】============\n";
$role = $auth->getRole('root');
$permissions = $auth->getPermissions();
foreach ($permissions as $key=>$item){
$auth->assign($item,$admin->id);
echo "為使用者:".$admin->username."授權".$item->description."權限\n";
$auth->addChild($role,$item);
echo "為角色:".$role->description."授權".$item->description."權限\n";
}
$roles = $auth->getRoles();
foreach ($roles as $key=>$item){
$auth->assign($item,$admin->id);
echo "為使用者:".$admin->username."授權".$item->description."角色\n";
}
echo "============授權填充【完成】============\n";
echo "\n";
$transaction->commit();
}catch (\Exception $exception){
$transaction->rollBack();
echo "????????????運作出錯????????????\n";
echo $exception->getMessage();
echo "\n";
}
}
}
放到console裡的controller然後在指令行執行如下指令即可:
即可,檢視資料庫新生成的四張表都填充好了自己目前開發狀态下的權限配置