我正在 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;
}
我检查了状态是否真的已设置,并在 View 中回显。后来我把这个角色放在 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('*'),
),
);
}
而且我无法通过该用户登录访问这些页面。这是什么问题?
最佳答案
看这里,http://www.yiiframework.com/doc/guide/1.1/en/topics.auth
您需要创建 main 角色并通过用户 ID 分配它。
关于Yii 框架不检查 accessRules 中的角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11365099/