Yii 2.0: yii2-admin

RBAC Manager for Yii 2
21 followers

Yii 2 Extension to manage Role Base Access Control.

Requirements

Yii 2.0 or above

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require mdmsoft/yii2-admin "*"

or add

"mdmsoft/yii2-admin": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply modify your application configuration as follows:

return [
    'modules' => [
        'admin' => [
            'class' => 'mdm\admin\Module',
        ]
        ...
    ],
    ...
    'components' => [
        ....
        'authManager' => [
            'class' => 'yii\rbac\PhpManager', // or use 'yii\rbac\DbManager'
        ]
    ],
    'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
        'allowActions' => [
            'admin/*', // add or remove allowed actions to this list
        ]
    ,
];

See Yii RBAC for more detail. You can then access Auth manager through the following URL:

http://localhost/path/to/index.php?r=admin

Resources

Fork me on github

Total 20 comments

#19523 report it
marvix at 2015/08/20 08:03am
./yii error

An error is showing when i run ./yii:

Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\console\Application::homeUrl'
 
/vendor/yiisoft/yii2/di/ServiceLocator.php(73): yii\base\Component->__get('homeUrl')
/vendor/mdmsoft/yii2-admin/Module.php(100): yii\di\ServiceLocator->__get('homeUrl')

I must disable it to use ./yii

#19158 report it
marianboricean at 2015/03/30 07:44am
@Misbahul D Munir

Thank you. I know it's not about yii2-admin, but you can't blame me for trying taping into you vast knowledge about Yii2 :) If I remove that part with "as access" with the class of yii2-admin, the application works. I will read again from the link you sent me, but that documentation is so dry.. no examples. I hope to be able to figure it out...

Thank you anyway for taking the time to reply so far.

#19151 report it
Misbahul D Munir at 2015/03/29 11:52pm
@marianboricean

Please read Yii2 docs. http://www.yiiframework.com/doc-2.0/yii-web-user.html Your error is not about yyi2-admin

#19150 report it
marianboricean at 2015/03/29 11:25pm
@Misbahul D Munir: Attaching filter to application breaks things

I am not sure what's wrong with my config. Now I get this message when tying to load my /site/index page:

Invalid Configurationyii\base\InvalidConfigException
User::identityClass must be set.
 
    1. in /home/marian/mywww/public_html/RCCL_IT_Scorecard/vendor/yiisoft/yii2/web/User.php at line 151
    142143144145146147148149150151152153154155156157158159160
 
 
        /**
         * Initializes the application component.
         */
        public function init()
        {
            parent::init();
 
            if ($this->identityClass === null) {
                throw new InvalidConfigException('User::identityClass must be set.');
            }
            if ($this->enableAutoLogin && !isset($this->identityCookie['name'])) {
                throw new InvalidConfigException('User::identityCookie must contain the "name" element.');
            }
        }
 
        private $_identity = false;
 
        /**
 
    2. in /home/marian/mywww/public_html/RCCL_IT_Scorecard/vendor/yiisoft/yii2/base/Object.php – yii\web\User::init() at line 107
    3. yii\base\Object::__construct([])
[......]
        require(__DIR__ . '/../../common/config/main.php'),
        require(__DIR__ . '/../../common/config/main-local.php'),
        require(__DIR__ . '/../config/main.php'),
        require(__DIR__ . '/../config/main-local.php')
    );
 
    $application = new yii\web\Application($config);
    $application->run();
#19149 report it
marianboricean at 2015/03/29 11:02pm
@Misbahul D Munir, thank you

Thank you @Misbahul D Munir, that makes a lot of sense. Let me try it.. I'm really impressed that you replied to my concern right away :) Thank you again!

#19148 report it
Misbahul D Munir at 2015/03/29 10:28pm
Attach filter to application

@marianboricean AccessControl filter is not part of components, but to application. move this code

'as access' => [
            'class' => 'mdm\admin\components\AccessControl',
            'allowActions' => [
                'site/login',
                'site/error',
            ],  
        ],

to

return [
    'bootstrap'=>[...],
    'modules'=>[...],
    'components'=>[....],
 
    'as access' => [
       // here...  
    ],
];
#19147 report it
marianboricean at 2015/03/29 10:17pm
What am I doing wrong? The module is accessible by guest!

Hi guys, sorry if this is really stupid, but I can't figure it out. I am still struggling to get this right. I installed the module OK, I can access the user, I create some roles and permissions, I associated roles to users, but... the admin module is accessible even if no user is logged in! Here is my code in config:

],  
    'modules' => [
        'admin' => [
            'class' => 'mdm\admin\Module',
            'layout' => 'left-menu',
            'controllerMap' => [
                'assignment' => [
                    'class' => 'mdm\admin\controllers\AssignmentController',
                    //'userClassName' => 'dektrium\user\models\User',
                    'userClassName' => 'common\models\User',
                    'idField' => 'id',
                ],  
            ],  
            'menus' => [
                'assignment' => [
                    'label' => 'Users'
                ],  
                'route' => null,
            ],  
        ],

And here is the code under "components":

'components' => [
       'authManager' => [
            'class' => 'yii\rbac\PhpManager',
        ],  
        'as access' => [
            'class' => 'mdm\admin\components\AccessControl',
            'allowActions' => [
                'site/login',
                'site/error',
            ],  
        ], 
      ],

I am using the yii2-user module as well and common\models\User extends from dektrium\user\models\User

Any ideas?

#18557 report it
Misbahul D Munir at 2014/11/16 07:45pm
Admin Access

@metall go to admin/role select your admin role. assign it with route admin/*. remove admin/* from mdm\admin\components\AccessControl

#18553 report it
metall at 2014/11/16 03:14am
Admin access

Hi how can I force module be accessible for just admin user?

#18399 report it
Misbahul D Munir at 2014/10/24 04:41am
Scott_Huang

:D, what you asking its how this module used. You should grand access via application(module). Create new role(or permission), assign that role with route admin/*. Then assign user with that role. After that, remove admin/* from allowAction. Do the same for all route that you want to controll.

#18398 report it
Scott_Huang at 2014/10/24 04:11am
@Misbahul D Munir

Issue: Today, each uses can direct access locahost/mydomain/admin.

Below code allow all people can access, if I remove admin/*, then all people cannot access.

How to setting to only allow limited users which have specific roles or permission to access it? Or I have to go to mdmsoft/yii2-admin/controllers to add AccessControl for each controller in behavior function?

'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
        'allowActions' => [
            'admin/*', // add or remove allowed actions to this list
        ]
],
#18378 report it
Scott_Huang at 2014/10/22 09:49am
@Misbahul D Munir

After check your code, I realize that actually I can use below code directly , thanks.

\Yii::$app->user->can('admin','author');

Anyway, your behavior code also work, thx.

#18360 report it
Misbahul D Munir at 2014/10/20 07:02am
@laozhuforever

Insyaallah 1 November :D. Please submit your issue here for better stable version.

#18359 report it
laozhuforever at 2014/10/20 06:02am
when will the stable version release?

when will the stable version release?

#18356 report it
Misbahul D Munir at 2014/10/19 10:30pm
@Scott_Huang

This is not scope of extension. You can override yii\web\User and add that method or create behavior and attach to user component.

class UserBehavior extends \yii\base\Behavior
{
    public function checkRole($roles)
    {
        $roles = (array)$roles;
        foreach ($roles as $role) {
            if($this->owner->can($role)){
                return true;
            }
        }
        return false;
    }
}

then attach to user component (at config)

'components' => [
    ...
    'user' => [
        'identityClass' => 'app\models\User',
        'as ext' => 'path\to\UserBehavior',
    ],
]
#18354 report it
Scott_Huang at 2014/10/19 10:13pm
@Misbahul D Munir

Could you advise how to check user have which roles? I would like below example(not existing now):

\Yii::$app->user->checkRole(['admin','author']);
#18352 report it
Misbahul D Munir at 2014/10/19 11:32am
@Scott_Huang

Can you elaborate your question. "code" what?

#18351 report it
Scott_Huang at 2014/10/19 10:28am
How to find user role by code?

I am successful install yii2-admin, also able create role/permission, thanks for your extension. I am able check permission by code like below

if (\Yii::$app->user->can('setRight')) {
            //if (\Yii::$app->user->identity->role == 10) { 
                $menuItems[] = ['label' => 'SetRight', 'url' => ['/admin']];
            }

Question: How to find user role by code?

#18234 report it
realtebo at 2014/10/03 09:53am
What about advanced template?

In the advanced template there is a common/config dir with 5 files.

Where must we insert the config ?

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet