PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 12, 2022

[FIXED] Yii2 RBAC based on permissions

 January 12, 2022     yii, yii2, yii2-rbac     No comments   

Issue

I am designing a system but I need to give the admin user the power to create roles and assign a set of permissions against them.

Currently in the RBAC

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['index','view'], // these action are accessible 
                                                   //only the yourRole1 and yourRole2
                    'allow' => true,
                    'roles' => ['yourRole1', 'yourRole2'],
                ],
                [    // all the action are accessible to superadmin, admin and manager
                    'allow' => true,  
                    'roles' => ['superAdmin', 'admin', 'manager'],
                ],   
            ],
        ],
    ];
}

However what I ideally need is

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index','view'], 
                        'allow' => true,
                        'permission' => ['canView'],
                    ],
                    [    
                        'actions' => ['update','delete'], // these action are accessible 
                        'allow' => true,  
                        'permission' => ['canDelete', 'canUpdate'],
                    ],   
                ],
            ],
        ];
    }

By doing this and creating a set of permissions an admin user can then create roles, assign permissions and assign roles to users.

Does anyone know of a package for yii2 that does this?


Solution

The AccessControl Filter you are using already allows you to do that via the "permissions" field.

[
    'actions' => ['index','view'], 
    'allow' => true,
    'permissions' => ['canView'],
],

Check the documentation: http://www.yiiframework.com/doc-2.0/yii-filters-accessrule.html#$permissions-detail



Answered By - Patrick
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing