2013-05-30 38 views
2

我開始在已驗證用戶和訪客用戶之間創建區別。但總是當我識別用戶並且它成功登錄時,它仍然返回true,當我使用Yii :: app() - > user-> isGuest。這裏是我正在使用的代碼:Yii身份驗證錯誤:登錄成功後,Yii :: app() - > user-> isGuest()總是返回true

if (Yii::app()->user->isGuest){ 
      echo 'Welcome back Guest'; 
    } else { 
      echo 'Welcome back '.Yii::app()->user->name; 
    } 

我總是得到'歡迎回來客',無論我是否已登錄(成功)或沒有。如果我再次登錄,我收到了此消息。這裏是用戶識別碼。

class UserIdentity extends CUserIdentity { 

    /** 
    * Authenticates a user. 
    */ 
    private $_id; 

    public function authenticate() { 
     $user = Users::model()->findByAttributes(array('username' => $this->username)); 
     if ($user === null) 
      $this->errorCode = self::ERROR_USERNAME_INVALID; 
     else if ($user->password !== $this->password) 
      $this->errorCode = self::ERROR_PASSWORD_INVALID; 
     else { 
      $this->_id = $user->id; 
      $this->setState('title', $this->username); 
      $this->setState('id', $user->id); 
      $this->errorCode = self::ERROR_NONE; 
     } 
     return !$this->errorCode; 
    } 

    public function getId() { 
     return $this->_id; 
    } 

} 

請幫我擺脫這個問題。

+0

用戶是否真的在某處登錄? –

+0

是的,當我用於登錄Yii :: app() - > user-> name時返回名稱,但是當我使用Yii :: app() - > user-> id時,它返回null。 –

+0

嘗試使用Yii :: app() - > user-> getId();並檢查使用var_dump是什麼結果爲Yii :: app() - > user-> isGuest – Ninad

回答

3

警予認證您應該解決LoginForm

/** 
* Authenticates the password. 
* This is the 'authenticate' validator as declared in rules(). 
*/ 
public function authenticate($attribute, $params) 
{ 
    // we only want to authenticate when no input errors 
    if (! $this->hasErrors()) { 
     $identity = new UserIdentity($this->email, $this->password); 
     $identity->authenticate(); 
     switch ($identity->errorCode) { 
      case UserIdentity::ERROR_NONE: 
       $duration = ($this->rememberMe) 
        ? 3600*24*14 // 14 days 
        : 0; // login till the user closes the browser 
       Yii::app()->user->login($identity, $duration); 
       break; 

      default: 
       // UserIdentity::ERROR_USERNAME_INVALID 
       // UserIdentity::ERROR_PASSWORD_INVALID 
       // UserIdentity::ERROR_MEMBER_NOT_APPOVED 
       $this->addError('', Yii::t('auth', 
        'Incorrect username/password combination.')); 
       break; 
     } 
    } 
} 


class UsersController extends Controller 
{ 
    /** 
    * Displays the login page 
    */ 
    public function actionLogin() 
    { 
     $model = new LoginForm; 

     if (isset($_POST['LoginForm'])) { 
      $model->attributes = $_POST['LoginForm']; 

      $valid = $model->validate(); 
      if ($valid) { 
       $this->redirect(Yii::app()->user->returnUrl); 
      } 
     } 
     $this->render('login', array('model' => $model)); 
    } 
} 

你能告訴你的配置?

+0

**瓦列裏Viktorovsky **我試過你建議我的代碼。唯一的問題是,登錄後請求它重定向到家裏,但選擇了顯示註銷菜單作爲Yii :: app() - > user-> isGuest ()返回true。另外當我打印Yii :: app() - > user-> name時,它顯示用戶名,現在該怎麼辦?:( –

+0

也許你的瀏覽器禁用了cookies這項功能? –

+0

感謝** Valery Viktorovsky **我得到了解決方案,非常感謝。 –

0

試試這個,看看使用Not運算

if (!Yii::app()->user->isGuest){ 
      echo 'Welcome back Guest'; 
    } else { 
      echo 'Welcome back '.Yii::app()->user->name; 
    } 
+0

Ninad我使用了這段代碼,但它不符合我的要求。因爲仍然Yii :: app() - > user- > id返回null。 –

0

你應該做到這一點(!):

$this->setState('id', $user[0]->id); 

的setState不應該被用於 'ID'。爲了返回'id',你已經重寫了getId()方法。

如果你需要設置它,改變它是這樣的:

$this->_id = $user[0]->id; 

希望這有助於。

Regards,