我是yii的新手。我已经用yii-tutorials在yii中创建角色了。但是我没有得到如何在yii中创造角色。意味着我想创建两个角色admin
& staff
我希望给予他们不同的特权
我在用户表中创建了一个role
,但我不知道如何创建角色并为其分配权限,请帮助我们
提前致谢
答案 0 :(得分:2)
在你的同事/ 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.*',
),
答案 1 :(得分:2)
这个来源非常适合初学者..我到目前为止使用这种方法: Simple RBAC in YII
在进行所需修改时,请按照给出的说明进行操作。
具体例子:
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);
}
}
只需将其与组件/ UserIdentity.php 和 config / main.php 连接:
'components' => array(
// ...
'user' => array(
'class' => 'WebUser',
),
就是这样..
检查登录的角色:
Yii::app->checkAccess("roles");
where checkAccess is the name of your function in WebUser...
请注意,evalRoles是表格中的一列,它将提供帐户的角色(在我提供的链接上,即主要部分代码段中使用的角色一词)