-1

Environment: os: mac mojave php: 7.2.9 yii2: 2.0.21 -basic template

firstly my yii2 app 2.0.5 and everything work ok. then php updated to 7.2.9. but application goes error

Fatal error: Cannot use 'Object' as class name as it is reserved

then yii2 updated to 2.0.21

then yii2 app run normally but all user can access all permission or role

this is my RbacController.php

<?php
namespace app\commands;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
    public function actionInit()
    {
        $permissions = [
            /* template permission
            'nama_permission' => 'deskripsi',
            */
            'kelolaCuti' => 'Kelola Cuti',

            'kelolaStaff' => 'Kelola data staff',

        ];

        $roles = [
            /* template role
            'nama_role' => ['permission/role', ...],
            */
            'staff' => [
                'kelolaCuti'
            ],
            'admin' => [
                'staff',
                'kelolaStaff',
            ],
            'supervisor' => [
                'admin',
            ],
            'direksi'=>[
                'supervisor'
            ],
            'administrator' => [
                'direksi'
            ],
            'superuser' => [
                'administrator'
            ]
        ];

        // Siapkan authManager
        $auth = Yii::$app->authManager;
        $auth->removeAll();

        $rule = new \app\rbac\UserGroupRule;
        $auth->add($rule);

        // Tambahkan permission
        foreach($permissions as $name => $description) {
            $permission = $auth->createPermission($name);
            $permission->description = $description;
            $auth->add($permission);
        }

        // Tambahkan role
        foreach($roles as $name => $children) {
            $role = $auth->createRole($name);
            $auth->add($role);

            foreach($children as $child) {
                $auth->addChild($role, $auth->getItem($child));
            }
        }
    }
}

this is my rule

<?php
namespace app\rbac;

use Yii;
use yii\rbac\Rule;

class UserGroupRule extends Rule
{
    public $name = 'userGroup';

    public function execute($user, $item, $params)
    {
        /* kode ROLES dari app\models\Staff
        const ROLES = [
            1 => 'Super User',
            2 => 'Administrator',
            3 => 'Direksi',
            4 => 'Supervisor',
            5 => 'Admin',
            6 => 'Staff',
        ];
        */
        if(!Yii::$app->user->isGuest) {
            $group = Yii::$app->user->identity->role;

            if($item->name === 'superuser') {
                return $group == 1;
            } elseif($item->name === 'administrator') {
                return in_array($group, [1, 2]);
            } elseif($item->name === 'direksi') {
                return in_array($group, [1, 2, 3]);
            } elseif($item->name === 'supervisor') {
                return in_array($group, [1, 2, 3, 4]);
            } elseif($item->name === 'admin') {
                return in_array($group, [1, 2, 3, 4, 5]);
            } elseif($item->name === 'staff') {
                return in_array($group, [1, 2, 3, 4, 5, 6]);
            } 
        }

        return false;
    }
}

this is my config/web.php

...
'authManager' => [
            'class' => 'yii\rbac\PhpManager',
            'defaultRoles' => ['superuser', 'administrator', 'direksi', 'supervisor', 'admin', 'staff'],
        ],
...

there is role field in user table as group in rbac

like describe in code that role staff has no grant to access kelolaStaff but when user with role staff loggedin and checked with Yii::$app->user->can('kelolaStaff') return TRUE

Edited

as addviced by @Bizley below:

while to get my App work: 1. set defaultRole just staff

...
'authManager' => [
'class' => 'yii\rbac\PhpManager',
          'supervisor', 'admin', 'staff'],
            'defaultRoles' => ['staff'],
        ],
...
  1. assign role manually after login
$auth = \Yii::$app->authManager;
        $auth->revokeAll($this->_user->id);
        switch ($this->_user->role) {
            case Staff::ROLE_SUPERUSER :
                if (!Yii::$app->user->can('superuser')){
                    $grantRole = $auth->getRole('superuser');
                    $auth->assign($grantRole, $this->_user->id);
                }
                break;

            case Staff::ROLE_ADMINISTRATOR :
                if (!Yii::$app->user->can('administrator')){
                    $grantRole = $auth->getRole('administrator');
                    $auth->assign($grantRole, $this->_user->id);
                }
                break;
            case Staff::ROLE_STAFF :
                if (!Yii::$app->user->can('staff')){
                    $grantRole = $auth->getRole('staff');
                    $auth->assign($grantRole, $this->_user->id);
                }
                break;
        }
Ade Supriyadi
  • 37
  • 1
  • 7
  • Just searching on google [https://forum.yiiframework.com/t/cannot-use-object-as-class-name-as-it-is-reserved-on-login/87957](https://forum.yiiframework.com/t/cannot-use-object-as-class-name-as-it-is-reserved-on-login/87957) – Sfili_81 Jun 24 '19 at 06:31
  • yes I was searching on google and I decide to update yii2 to solving the first question as I described, I extend identityInterface from ActiveRecord ```php class Staff extends ActiveRecord implements IdentityInterface ``` problem is that all user can access all permission or role, thanks @Sfili_81 – Ade Supriyadi Jun 24 '19 at 10:41
  • As an alternative for RBAC, You can try PHP-Casbin together with the Yii middleware: https://github.com/php-casbin/yii-casbin – hsluoyz Jun 25 '19 at 15:23

1 Answers1

0

The first part of your question has been answered by @Sfili_81 in comment but you took care of it by updating the Yii.

As for the second part:

Don't set all your roles in defaultRoles in config for authManager.

As described in Guide:

A default role is a role that is implicitly assigned to all users

So every user (including guests) has got all these roles by default with your config. Remove this config option and assign roles to users that should get them (details how to do it are described in linked Guide's section).

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • In [Guide](https://www.yiiframework.com/doc/guide/2.0/en/security-authorization#using-default-roles) > A default role is a role that is implicitly assigned to all users but it limited by the Rules > A default role is usually associated with a rule which determines if the role applies to the user being checked. My App (yii2 2.0.5) work correctly before yii2 updated to 2.0.21 – Ade Supriyadi Jun 25 '19 at 01:57