我正在Yii框架中创建某种管理面板,我在登录时设置状态就像这样
public function authenticate()
{
$record=AdminTbl::model()->findByAttributes(array('usr'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->pwd!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('roles','main');
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
我检查了状态是否真的设置,在视图中回显。后来我把这个角色放在accessrules()
中public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','create','update','admin','delete'),
'roles'=>array('main'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
我登录时无法访问这些页面。问题是什么?
答案 0 :(得分:2)
看看这里,http://www.yiiframework.com/doc/guide/1.1/en/topics.auth
您需要创建主角色并通过用户ID分配。
答案 1 :(得分:1)
如果您需要简单的基于角色的访问控制而无需长RBAC流程,那么本文仅适合您