2014-01-15 73 views
0

據我所知我做對Yii的,定製WEBUSER財產

的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提供任何數據,但不對數據庫使用額外查詢(在WebUser中)。

回答

1

在你的UserIdentity,而不是這樣的:

$this->role = $oUser->role; 

這樣做:

$this->setState('role',$oUser->role); 

您也可以從頂部取出private $role