yii2 rbac检查角色用户 - > can()

时间:2014-10-31 15:32:07

标签: database yii2 rbac

我使用DBManager在yii2中安装并配置了rbac但是我没有使用“check”:

if (Yii::$app->user->can('waitAccess')) {
    echo "yes it is pending.";
} else {
    echo "nothing";
}

我用不同的角色让3个用户,但他们每个人都能看到第一行,尽管他们没有权限。 “在我看来”

这是我的rbacController

<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
public function actionInit()
{
    $auth = Yii::$app->authManager;

    // add "user2View" permission
    $user2View = $auth->createPermission('user2View');
    $user2View->description = 'user2 view';
    $auth->add($user2View);

    // add "user1View" permission
    $user1View = $auth->createPermission('user1View');
    $user1View->description = 'user1 view';
    $auth->add($user1View);

    // add "waitAccess" permission
    $waitAccess = $auth->createPermission('waitAccess');
    $waitAccess->description = 'wait for Access';
    $auth->add($waitAccess);

    // add "seeConfig" permission
    $seeConfig = $auth->createPermission('seeConfig');
    $seeConfig->description = 'Access to the administrative Config';
    $auth->add($seeConfig);

    // add "user2" role and give this role the "user2View" permission
    $user2 = $auth->createRole('user2');
    $auth->add($user2);
    $auth->addChild($user2, $user2View);

    // add "user1" role and give this role the "user1View" permission
    $user1 = $auth->createRole('user1');
    $auth->add($user1);
    $auth->addChild($user1, $user1View);

    // add "pending" role and give this role the "waitAccess" permission
    $pending = $auth->createRole('pending');
    $auth->add($pending);
    $auth->addChild($pending, $waitAccess);

    // add "superadmin" role and give this role the "seeConfig" permission
    $superadmin = $auth->createRole('superadmin');
    $auth->add($superadmin);
    $auth->addChild($superadmin, $seeConfig);
    $auth->addChild($superadmin, $user2View);
    $auth->addChild($superadmin, $user1View);
    $auth->addChild($superadmin, $waitAccess);

}
}

也许任何人都知道我能找到什么。

更新:这是我的数据库结构 DB Structure of my yii2 rbac

更新2:

我解决了! 愚蠢没有看到所有用户写下来的默认规则。所以每个人都有权限。删除该行并为标准添加待处理已解决该问题。

0 个答案:

没有答案