0

我是yii的新手。我一直坚持使用 yii-tutorials 在 yii 中创建角色。但我不知道如何在 yii 中创建角色。意味着我想创建两个角色adminstaff并且我想给他们不同的特权。
我已经role在用户表中创建了一个,但我不知道如何创建角色并为他们分配特权,请帮帮我

提前致谢

4

2 回答 2

2

这个源非常适合初学者。我到现在都在使用这个方法: YII 中的简单 RBAC

只需按照给出的说明进行所需的修改即可。

具体示例:

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);
    }
}

只需将它与您的components/UserIdentity.phpconfig/main.php连接起来:

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

就是这样..

检查登录的角色:

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

请注意,evalRoles 是您表中的一列,它将提供帐户的角色(在我提供的链接上,这将是主要部分片段中使用的单词角色)

于 2013-01-08T05:19:40.567 回答
2

在你的 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;
}
}

您可以创建一个新的列名称作为“角色”。将成员类型“admin”或“staff”设置为角色列。

小心那条线。

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

创建一个新的帮助文件。/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;

}

}

并在您的控制器中-> accessRules 功能

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('*'),
        ),
    );
}

并且不要忘记将 'application.helpers.*' 添加到 /config/main.php

'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.helpers.*',
),
于 2013-01-08T05:01:35.753 回答