0

I am a newbie to yii. I have stuck my mind with yii-tutorials for creating roles in yii. but I am not getting how can I create role in yii. means I want to create two roles admin & staff and I want to give different priviliage to them.
I have created a role in user table, but I am not getting that how can I create a role and can assign priviliages to them, please help me guys

Thanks in advance

Vainglory07
  • 5,073
  • 10
  • 43
  • 77
Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105
  • This link might be of some help: [Getting to Understand Hierarchical RBAC Scheme](http://www.yiiframework.com/wiki/136/getting-to-understand-hierarchical-rbac-scheme/) – Stu Jan 07 '13 at 14:15
  • I am not getting one thing that where should I use this code – Rohitashv Singhal Jan 07 '13 at 14:17
  • The logic is, in the Controller access rules, you check first if the user is logged in, and then what their role is, and assign different access rules for each role type – Brett Gregson Jan 07 '13 at 15:15

2 Answers2

2

In your copenents/UserIdentity.php

class UserIdentity extends CUserIdentity{
private $_id;



public function authenticate()
{
    $record=Members::model()->findByAttributes(array('username'=>trim($this->username)));

    if($record===null)
    $this->errorCode=self::ERROR_USERNAME_INVALID;

    else if($record->password!==md5(trim($this->password)))
    $this->errorCode=self::ERROR_PASSWORD_INVALID;

    else
    {
        $this->_id=$record->id;
        $this->setState('username', $record->username);
        $this->setState('name', $record->name);
        $this->setState('type', $record->role);
        $this->errorCode=self::ERROR_NONE;

    }
    return !$this->errorCode;
}

public function getId()
{
    return $this->_id;
}

public function setId($id)
{
    $this->_id = $id;
}
}

You can create a new column name as "role". set the members type "admin" or "staff" to role column.

Be careful to that line.

$this->setState('type', $record->role);

Create a new helper file. /protected/helpers/RoleHelper.php

class RoleHelper {

public static function GetRole(){

    if (Yii::app()->user->type == "admin"){
        //set the actions which admin can access
        $actionlist = "'index','view','create','update','admin','delete'";
    }
    elseif (Yii::app()->user->type = "staff"){
        //set the actions which staff can access
        $actionlist = "'index','view','create','update'";
    }
    else {
        $actionlist = "'index','view'";
    }

    return $actionlist;

}

}

and in your controllers -> accessRules function

public function accessRules()
{

    return array(
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array(RoleHelper::GetRole()),
            'users'=>array('@'),
        ),

        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}

and dont forget to add 'application.helpers.*' to /config/main.php

'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.helpers.*',
),
Burhan Çetin
  • 676
  • 7
  • 16
2

This source is pretty good specially for beginners..I am using this method till now: Simple RBAC in YII

Just follow the instructions given while having your desired modifications.

Concrete Example:

WebUser.php (/components/WebUser.php)

<?php 
class WebUser extends CWebUser
{
    /**
     * Overrides a Yii method that is used for roles in controllers (accessRules).
     *
     * @param string $operation Name of the operation required (here, a role).
     * @param mixed $params (opt) Parameters for this operation, usually the object to access.
     * @return bool Permission granted?
     */
    public function checkAccess($operation, $params=array())
    {
        if (empty($this->id)) {
            // Not identified => no rights
            return false;
        }
        $role = $this->getState("evalRoles");

        if ($role === 'SuperAdmin') {
            return 'SuperAdmin'; // admin role has access to everything
        }
        if ($role === 'Administrator') {
            return 'Administrator'; // admin role has access to everything
        }
        if ($role === 'Professor') {
            return 'Professor'; //Regular Teaching Professor, has limited access
        }
        // allow access if the operation request is the current user's role
        return ($operation === $role);
    }
}

Just connect it with your components/UserIdentity.php and config/main.php:

'components' => array(
// ...
'user' => array(
    'class' => 'WebUser',
),

and thats it..

to check the role of the logged in:

Yii::app->checkAccess("roles");
where checkAccess is the name of your function in WebUser...

Please note that evalRoles is a column in your table that will supply the role of an account (on my link provided, that would be the word roles used in the major part snippet)

Vainglory07
  • 5,073
  • 10
  • 43
  • 77