2015-01-26 130 views
2

我已經看過其他類似的問題,雖然這一個(Yii2 user identity loss after page redirection)幾乎提出了同樣的問題,但沒有解決方案適用於我的情況。yii2登錄重定向後失去用戶身份

我已經創建了一個新的身份類,我實現了所有必要的方法並實現了IdentityInterface。請注意,它不會擴展ActiveRecord,而是擴展我自己的基礎Model類,該類不是從AR派生的。

一切似乎工作正常,我可以登錄並進行正確的身份驗證。我已經追蹤了代碼,我可以看到IdentityInterface :: validatePassword()和User :: login()被調用後,我的身份類在Yii :: $ app-> user中正確設置。然而,一旦登錄成功並且用戶被重定向,Yii :: $ app-> user將包含一個空的IdentityInterface,而不是緊接在重定向之前的那個。

return $this->goHome(); 

到:

在SiteController :: actionLogin()

,我已經從修改的代碼

$tmp = $this->goHome(); 
// echo '<pre>';var_dump(Yii::$app->user);echo '</pre>';exit; 
return($tmp); 

和我可以看到的Yii :: $ APP->用戶仍然具有正確的身份直到return()語句。

誰能告訴我爲什麼我失去了我的身份?我追蹤了我能想到的所有東西:yii \ web \ Controller,yii \ base \ Controller,yii \ web \ Response,SiteController,yii \ web \ User等等......

任何幫助都是不勝感激。謝謝!

+0

忘了提及我沒有看到存儲在Yii :: $ app-> session中的身份的任何信息。所以不確定通常在請求之間如何維護身份。我使用標準的基於主動記錄的身份來追蹤所有相同的代碼,即使登錄重定向後,它也能正常工作。但我再次看不到什麼不同:身份在那裏,直到SiteController :: actionLogin()返回,在這種情況下,它也在那裏。 – Laz 2015-01-26 18:08:57

+0

是否有可能activerecord明確地做某事來保留請求之間的身份(我將不得不在我的代碼中複製)? – Laz 2015-01-26 18:47:57

+0

您是使用Yii默認身份類還是實現自己的?如果你實現了自己的發佈代碼。否則發佈您的模型的代碼。 – 2015-01-26 23:09:06

回答

-1

我希望這可以幫助你

<?php 

namespace app\models; 

use Yii; 

class User extends \yii\db\ActiveRecord implements  \yii\web\IdentityInterface { 

private $_notification; 

public static function tableName() 
{ 
    return 'users'; 
} 

public function setNotification($n) { 
    Yii::$app->session['user_' .$this->id . '_notification'] = $n; 
} 

public function getNotification() { 
    $n = Yii::$app->session['user_' .$this->id . '_notification']; 
    Yii::$app->session['user_' .$this->id . '_notification'] = NULL; 
    return $n; 
} 

/** 
* @inheritdoc 
*/ 
public function rules() 
{ 
    return [ 
     [['email', 'password', 'first_name'], 'required'], 
     [['role'], 'integer'], 
     [['email', 'first_name'], 'string', 'max' => 128], 
     [['password'], 'string', 'max' => 32] 
    ]; 
} 

/** 
* @inheritdoc 
*/ 
public function attributeLabels() 
{ 
    return [ 
     'id' => 'ID', 
     'email' => 'E-mail', 
     'password' => 'Пароль', 
     'first_name' => 'Имя', 
     'role' => 'Роль', 
    ]; 
} 

public static function findByEmail($email) { 
    return self::find()->where(['email' => $email])->one(); 
} 

public function validatePassword($password) { 
    if ($this->password == md5($password)) { 
     return true; 
    } 
    return false; 
} 

public static function findIdentity($id) 
{ 
    return static::findOne($id); 
} 

public static function findIdentityByAccessToken($token, $type = null) 
{ 
    return static::findOne(['access_token' => $token]); 
} 

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

public function getAuthKey() 
{ 
    return $this->authkey; 
} 

public function validateAuthKey($authKey) 
{ 
    return $this->authkey === $authKey; 
} 

} 
+1

請加解釋 – tod 2015-02-18 09:01:46

+0

我在我的模型中使用Active Record:class User extends \ yii \ db \ ActiveRecord implements \ yii \ web \ IdentityInterface。 我認爲模型丟失會話參數後重定向而不保存。 通過這種方式,我在會話Yii :: $ app-> session ['user_'。$ this-> id中推送了一些變量。 '_notification'] = $ n。 在我的應用程序中,我應該在顯示後清除通知。 我清除它的吸氣 - Yii :: $ app-> session ['user_'。$ this-> id。 '_notification'] = NULL; – 2015-02-18 10:53:52

2

嘗試這樣

  1. 實施implements \yii\web\IdentityInterface爲您的Active 記錄和\yii\web\IdentityInterface實現所有方法。
  2. 在您的班級中聲明$ users爲靜態變量private static $users = [];
  3. 修改findByUsername()功能獲取用戶詳細信息表單用戶名。 在配置文件中添加

    '用戶'=> [ 'identityClass'=> '應用程序\模型\活性記錄類', 'enableAutoLogin'=>真, ],

    class User extends \yii\db\ActiveRecord          
    implements\yii\web\IdentityInterface { 
    
    private $_user = false; 
    public $rememberMe = true; 
    /* 
    * static variable 
    */ 
    private static $users = []; 
    
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() { 
        return 'user'; 
    } 
    
    /** 
    * @inheritdoc 
    */ 
    public function rules() { 
        return [ 
         [['username', 'email', 'password', 'loginType'], 'required'], 
    
        ]; 
    } 
    
    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() { 
        return [ 
         'id' => Yii::t('app', 'ID'), 
         'username' => Yii::t('app', 'Username'), 
    
         'email' => Yii::t('app', 'Email'), 
         'password' => Yii::t('app', 'Password'), 
    
        ]; 
    } 
    
    /** 
    * Logs in a user using the provided username and password. 
    * @return boolean whether the user is logged in successfully 
    */ 
    public function login() { 
        if ($this->validate()) { 
         return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); 
        } else { 
         return false; 
        } 
    } 
    
    /** 
    * Finds user by [[username]] 
    * 
    * @return User|null 
    */ 
    public function getUser() { 
        if ($this->_user === false) { 
         $this->_user = self::findByUsername($this->username); 
        } 
    
        return $this->_user; 
    } 
    
    public function validatePassword($password) { 
        if ($this->password == md5($password)) { 
         return true; 
        } 
        return false; 
    } 
    
    public static function findIdentity($id) { 
        return static::findOne($id); 
    } 
    
    public static function findIdentityByAccessToken($token, $type = null) { 
        return static::findOne(['access_token' => $token]); 
    } 
    
    public function getId() { 
        return $this->id; 
    } 
    
    public function getAuthKey() { 
        return $this->authkey; 
    } 
    
    public function validateAuthKey($authKey) { 
        return $this->authkey === $authKey; 
    } 
    
    public static function findByUsername($username) { 
        /* 
        * Get Record from Usertable 
        */ 
        $user = User::findOne(["username" => $username]); 
        if (!empty($user)) { 
         /* Create array with your active record like below */ 
         $identity_use = ['id' => $user['id'], 
          'username' => $user['username'], 
          'password' => $user['password'], 
          'authKey' => '', 
          'accessToken' =>'',]; 
         return new static($identity_use); 
        } 
        return null; 
    } 
    

    }

1

我有同樣的問題。我有我的自定義類實現yii \ web \ IdentityInterface。我確保會話已啓用,enableAutologin也是如此,但我根本沒有運氣。

最後,我意識到在config/web中。php有一個設置'user'=>''identityClass'=>'app \ models \ User','enableAutoLogin'=> true]

當然,identityClass是基本yii應用程序的默認值。將此值設置爲我的自定義類後,身份最終保持不變。

1

同樣的問題,3天后找到解決方案,我工作。 我的用戶模型爲:

namespace app\models; 

use Yii; 
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface 
{ 
    public $username; 

    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'tbl_users'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['name', 'email', 'password'], 'required'], 
      [['email'], 'unique'], 
      [['role', 'status'], 'integer'], 
      [['created', 'last_update'], 'safe'], 
      [['name', 'email', 'password'], 'string', 'max' => 250] 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => 'ID', 
      'name' => 'Name', 
      'email' => 'Email', 
      'password' => 'Password', 
      'role' => 'Role', 
      'status' => 'Status', 
      'created' => 'Created', 
      'last_update' => 'Last Update', 
     ]; 
    } 
    public function getAuthKey() { 

    } 

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

    public function validateAuthKey($authKey) { 

    } 

    public static function findIdentity($id) { 

    } 

    public static function findIdentityByAccessToken($token, $type = null) { 

    } 
    public static function findByEmail($email) 
    { 
     return static::findOne(array('email'=>$email)); 
    } 

    public function validatePassword($password) 
    { 
     return $this->password === $password; 
    } 
} 

更改:

public static function findIdentity($id) { 

} 

到:

public static function findIdentity($id) { 
    return self::findOne(array('id'=>$id)); 
} 

它爲我工作。

+0

更改find​​Identity函數以實際返回一個User實例解決了我的問題 – Kimutai 2018-01-02 11:44:37