Answer the question
In order to leave comments, you need to log in
How to set up a user access system in Yii2?
I'm trying to set up an access system like admin, moder, user on Yii2. The more I dig, the more I cease to understand why everything is so complicated. I understand RBAC is flexible and all, but it requires 4 tables in a DB to use and it's too complicated.
if it is possible to write in the controller rules
...
[
'allow' => true,
'actions' => ['create','view'],
'roles' => array('admin'),
],
...
then how to make it as simple as possible to define 3 roles: admin, moder, user
and what role does the current user belong to determine stupidly from the field from the DB,
just if $user->type === 'admin' then assign him the admin role and that's it?
Answer the question
In order to leave comments, you need to log in
Yii2 eventually calls the can method in the user class, the rules https://github.com/yiisoft/yii2/blob/master/framew... are passed there, you can override it. Or through rbac, just use your authManager class, change the getAssignments method to your own and return an array of roles in it that correspond to the user being checked from https://github.com/yiisoft/yii2/blob/master/framew... Instead of a base you can use the file rbac https://github.com/yiisoft/yii2/blob/master/framew... it will just have three files, in your case two will return an empty array, and in items:
<?php
return [
'admin' => [
'type' => 1,
],
'moder' => [
'type' => 1,
],
'user' => [
'type' => 1
]
];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question