2
votes

I have followed some guides to set up rbac in yii 2

I have set up the table added a few user and so on.

RBAC init controller.

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

        $baseUser = $auth->createRole('base_user');
        $auth->add($baseUser);

        $support = $auth->createRole('support');
        $auth->add($support);

        $admin = $auth->createRole('admin');
        $auth->add($admin);
        $auth->addChild($admin, $baseUser);
        $auth->addChild($admin, $support);

        $auth->assign($support, 2);
        $auth->assign($admin, 1);
    }
}

So I have an admin and some user groups.

Now in one of my controllers I have

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'allow' => true,
                    'actions' => ['create'],
                    'roles' => ['admin'],
                ],
                [
                    'allow' => true,
                    'actions' => ['index'],
                    'roles' => ['@'],
                ],
            ],
        ],
    ];
}

In the 'roles' I would like to put admin. I can't quite work out how to get this to work.

Is it overkill for my project using RBAC, I just want certain groups of users to access certain areas/functions of the site.

1
in auth_assignment table, you are having value admin ?Nana Partykar
Yeah I have a value for every user that is signed upuser1724416
Show your confi/main.php components, authManager configuration and check if the controller contain use yii\filters\AccessControl;scaisEdge
freetuts.org/tutorial/view?id=6 I think this link will help you a lot with RBACSohel Ahmed Mesaniya

1 Answers

0
votes
 $auth->assign($support, 2);
 $auth->assign($admin, 1);

1 and 2 have to be your users ID.