Php Yii,自定义WebUser属性
好吧,我做得对 用户身份Php Yii,自定义WebUser属性,php,authentication,yii,Php,Authentication,Yii,好吧,我做得对 用户身份 class UserIdentity extends CUserIdentity { const ERROR_NOT_ACTIVE = 111; // private $role; private $_id; /** * @return bool */ public function authenticate() { $oUser = User::model()->fin
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;
}
}
网络用户
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中)的额外查询 在您的用户标识中,而不是:
$this->role = $oUser->role;
这样做:
$this->setState('role',$oUser->role);
您还可以从顶部删除private$role