AFAIK我做对了
的UserIdentity
class UserIdentity extends CUserIdentity
{
const ERROR_NOT_ACTIVE = 111;
//
private $role;
private $_id;
/**
* @return bool
*/
public function authenticate()
{
$oUser = User::model()->find('LOWER(login) = ?', array(strtolower($this->username)));
if ($oUser === null) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
} else {
// wrong password
if (!$oUser->validatePassword($this->password)) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
} // user not activated by admin
elseif ($oUser->active) {
$this->_id = $oUser->id;
$this->role = $oUser->role;
$this->username = $oUser->login;
$this->errorCode = self::ERROR_NONE;
} // user valid and activated
else {
$this->errorCode = self::ERROR_NOT_ACTIVE;
}
}
return $this->errorCode == self::ERROR_NONE;
}
/**
* @return mixed|string
*/
public function getID()
{
return $this->_id;
}
public function getRole()
{
return $this->role;
}
}
WEBUSER
class WebUser extends CWebUser
{
/**
* @return boolean
*/
public function isAdmin()
{
var_dump($this->getState('role'));
return ($this->getState('role') === User::ROLE_ADMIN);
}
}
配置
'components' => array(
'user' => array(
// enable cookie-based authentication
'class' => 'WebUser',
但在WebUser中根本没有角色属性。获得它的任何其他变体都会产生错误。问题是如何从UserIdentity向WebUser提供任何数据(登录后一次),但不对db(在WebUser中)使用其他查询。
答案 0 :(得分:1)
在您的UserIdentity中而不是:
$this->role = $oUser->role;
这样做:
$this->setState('role',$oUser->role);
您也可以从顶部删除private $role
。