简体   繁体   中英

Yii2 RBAC implementation using DBManager assignment rule fail

I'm trying to implement RBAC using DBManager in my Yii2 advanced application. I've read about RBAC in several source and implement RBAC like in https://yii2-cookbook.readthedocs.io/security-rbac/ but it doesn't work. Here is my code.

in the main.php under common/config

    return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],
];

in RbacController.php under console/controllers

namespace console\controllers;

use Yii;
use yii\console\Controller;

class RbacController extends Controller {
public function actionAssign($role, $username) {
       $user = User::find()->where(['username' => $username])->one();
        if (!$user) {
            throw new InvalidParamException("There is no user \"$username\".");
        }
        $auth = Yii::$app->authManager;
        $asrole = $auth->getRole($role);
        if (!$asrole) {
            throw new InvalidParamException("There is no role \"$role\".");
        }
        $auth->assign($asrole, $user->id);
            }
}

the migration file

use yii\db\Migration;

class m160616_092939_rbac_init extends Migration
{
    public function up()
    {
       $auth = Yii::$app->authManager;
       //add permission
        $manageGivenTable = $auth->createPermission('manageGivenTable');
        $manageGivenTable->description = 'Manage and Generate Given Table ';
        $auth->add($manageGivenTable);

        //add permission
        $manageUsers = $auth->createPermission('manageUsers');
        $manageUsers->description = 'Manage users';
        $auth->add($manageUsers);

        //add role. dan ngasih tahu kalau yang tergabung di sbr dapat memanage given tabel
        $sbr = $auth->createRole('sbr');
        $sbr->description = 'Tim SBR BPS HQ';
        $auth->add($sbr);
        $auth->addChild($sbr, $manageGivenTable);

        //add role dan ngasih tahu kalau admin dapat memanage user dan sekaligus mewarisi sifat-sifat sbr
        $admin = $auth->createRole('admin');
        $admin->description = 'Web Administrator, Editor, and Developer';
        $auth->add($admin);
        $auth->addChild($admin, $sbr);
        $auth->addChild($admin, $manageUsers);
    }

And I try it in about page. in the controller file, I add

'actions' => ['about'],
'allow' => true,
'roles' => ['manageUsers'],

NOTE: may be this info is needed

Can you help me,please? Edit : I've implement role assign using command prompt something like this

yii rbac/assign admin adminname

in the future, I want to assign user role via admin panel.

the role seems not to assigned. Try to add this

try {
    $info = $auth->assign($asrole, $user->id);
    VarDumper::dumpAsString("Role has been assigned ".$info);
} catch (Excpetion $e) {
    VarDumper::dumpAsString("Exception:".$e);
}

in the RbacController.php I add

use common\models\User;

and it work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM