0
  1. How to create permissions, roles and rules in yii2 using gii?
  2. How to assign roles in yii2?

How to implement all these using below 4 tables?

  1. auth_item
  2. auth_item_child
  3. auth_assignment
  4. auth_rule

Consider the case:

There are two users Admin and FieldOfficer:

I have SHGProfile CRUD application.

FieldOfficer can access only create and view actions in SHGProfile. Whereas Admin can access all create, view, update and delete actions.

Here Roles are Admin and FieldOfficer. Permissions are createGroup, viewGroup, updateGroup, and deleteGroup

Here in which table we need to create Roles and Permissions and how to assign it to user?

Questions
  • 69
  • 1
  • 12

1 Answers1

0

I will explain here which tables contains role and permissions and how to assign permissions to user :

  1. Insert your all roles in auth_item table i.e Admin , FieldOfficer , createGroup , viewGroup , updateGroup , deleteGroup.

  2. Assign createGroup, viewGroup, updateGroup, and deleteGroup to Admin role in auth_item_child table.

  3. Assign createGroup, viewGroup to FieldOfficer role in auth_item_child table.

  4. Assign permission to user in auth_assignment table with role id and user id . Assign only parent role like Admin or FieldOfficer.

  5. Now in your controller use AccessControl for give access to logged in user as per their role

    public function behaviors()
    {
    
       return [
          'access' => [
          'class' => \yii\filters\AccessControl::className(),
          'only' => ['create', 'view' , 'update' , 'delete'],
          'rules' => [
    
              // allow all actions to Admin role users
              [
                  'allow' => true,
                  'actions' => ['create', 'view' , 'update' , 'delete'],
                  'roles' => ['Admin'],
              ],
              // allow create , view actions to FieldOfficer role users
              [
                  'allow' => true,
                  'actions' => ['create', 'view'],
                  'roles' => ['FieldOfficer'],
              ],
              // everything else is denied
           ],
         ],
       ];
       }
    
  • I designed the above process, and logged in using the user having role admin. Now I tried to createGroup. It gives the foll error: Invalid Configuration – yii\base\InvalidConfigException Rule not found: – Questions Aug 10 '17 at 14:48
  • You need to follow yii2 doc for implement AccessControll properly . I have written a demo code for you. – Lakhan Singh Aug 11 '17 at 11:10
  • It gives,Invalid Configuration – yii\base\InvalidConfigException Rule not found error – Questions Aug 14 '17 at 14:48