2015-04-02 23 views
0

正確的我工作在yii 2.0試圖修改登錄系統。它適用於用戶只是手動編碼的數組。相反,我想從數據庫中完成這項工作。Yii 2.0從數據庫登錄不起作用

我會告訴你的初始模型的樣子:

<?php 

namespace app\models; 

class User extends \yii\base\Object implements \yii\web\IdentityInterface 
{ 
public $id; 
public $username; 
public $password; 
public $authKey; 
public $accessToken; 

private static $users = [ 
    '100' => [ 
     'id' => '100', 
     'username' => 'admin', 
     'password' => 'admin', 
     'authKey' => 'test100key', 
     'accessToken' => '100-token', 
    ], 
    '101' => [ 
     'id' => '101', 
     'username' => 'demo', 
     'password' => 'demo', 
     'authKey' => 'test101key', 
     'accessToken' => '101-token', 
    ], 
]; 

/** 
* @inheritdoc 
*/ 
public static function findIdentity($id) 
{ 
    return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; 
} 

/** 
* @inheritdoc 
*/ 
public static function findIdentityByAccessToken($token, $type = null) 
{ 
    foreach (self::$users as $user) { 
     if ($user['accessToken'] === $token) { 
      return new static($user); 
     } 
    } 

    return null; 
} 

/** 
* Finds user by username 
* 
* @param string  $username 
* @return static|null 
*/ 
public static function findByUsername($username) 
{ 
    foreach (self::$users as $user) { 
     if (strcasecmp($user['username'], $username) === 0) { 
      return new static($user); 
     } 
    } 

    return null; 
} 

/** 
* @inheritdoc 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* @inheritdoc 
*/ 
public function getAuthKey() 
{ 
    return $this->authKey; 
} 

/** 
* @inheritdoc 
*/ 
public function validateAuthKey($authKey) 
{ 
    return $this->authKey === $authKey; 
} 

/** 
* Validates password 
* 
* @param string $password password to validate 
* @return boolean if password provided is valid for current user 
*/ 
public function validatePassword($password) 
{ 
    return $this->password === $password; 
} 
} 

好所以你可以看到它正在對手工編碼陣列名爲$用戶。所以我做了一個名爲「用戶」的表,並將列標識爲用戶名密碼authkey和accessToken。

希望它會做同樣的事情用一個數據庫表,當我嘗試登錄,但是我收到一個錯誤。這是我的新代碼

<?php 

namespace app\models; 

/** 
* This is the model class for table "Cases". 
* 
* @property integer $id 
* @property string $username 
* @property string $password 
* @property string $authkey 
* @property string $accessToken 
*/ 
class User extends \yii\db\ActiveRecord 
{ 


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

public function rules() 
{ 
    return [ 
     [['id','username','password','authkey','accessToken'], 'required'], 
     [['id'], 'integer'],  
    ]; 
} 

/** 
* @inheritdoc 
*/ 
public function attributeLabels() 
{ 
    return [ 
     'id' => 'id', 
     'username' => 'username', 
     'password' => 'password', 
     'authkey' => 'authkey', 
     'accessToken' => 'accessToken', 
    ]; 
} 

























/** 
* @inheritdoc 
*/ 
public static function findIdentity($id) 
{ 
    return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; 
} 

/** 
* @inheritdoc 
*/ 
public static function findIdentityByAccessToken($token, $type = null) 
{ 
    foreach (self::$users as $user) { 
     if ($user['accessToken'] === $token) { 
      return new static($user); 
     } 
    } 

    return null; 
} 

/** 
* Finds user by username 
* 
* @param string  $username 
* @return static|null 
*/ 
public static function findByUsername($username) 
{ 
    foreach (self::$Users as $user) { 
     if (strcasecmp($user['username'], $username) === 0) { 
      return new static($user); 
     } 
    } 

    return null; 
} 

/** 
* @inheritdoc 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* @inheritdoc 
*/ 
public function getAuthKey() 
{ 
    return $this->authKey; 
} 

/** 
* @inheritdoc 
*/ 
public function validateAuthKey($authKey) 
{ 
    return $this->authKey === $authKey; 
} 

/** 
* Validates password 
* 
* @param string $password password to validate 
* @return boolean if password provided is valid for current user 
*/ 
public function validatePassword($password) 
{ 
    return $this->password === $password; 
} 
} 

錯誤消息我收到的時候我嘗試登錄是「訪問未聲明的靜態屬性:應用程序\模型\用戶:: $用戶」。

如果您需要查看我的LoginForm模型和控制器操作,我會在這裏下載它們。

public function actionLogin() 
{ 
    if (!\Yii::$app->user->isGuest) { 
     return $this->goHome(); 
    } 

    $model = new LoginForm(); 
    if ($model->load(Yii::$app->request->post()) && $model->login()) { 
     return $this->goBack(); 
    } else { 
     return $this->render('login', [ 
      'model' => $model, 
     ]); 
    } 
} 

和LoginForm的模式是:

<?php 

namespace app\models; 

use Yii; 
use yii\base\Model; 

/** 
* LoginForm is the model behind the login form. 
*/ 
class LoginForm extends Model 
{ 
public $username; 
public $password; 
public $rememberMe = true; 

private $_user = false; 


/** 
* @return array the validation rules. 
*/ 
public function rules() 
{ 
    return [ 
     // username and password are both required 
     [['username', 'password'], 'required'], 
     // rememberMe must be a boolean value 
     ['rememberMe', 'boolean'], 
     // password is validated by validatePassword() 
     ['password', 'validatePassword'], 
    ]; 
} 

/** 
* Validates the password. 
* This method serves as the inline validation for password. 
* 
* @param string $attribute the attribute currently being validated 
* @param array $params the additional name-value pairs given in the rule 
*/ 
public function validatePassword($attribute, $params) 
{ 
    if (!$this->hasErrors()) { 
     $user = $this->getUser(); 

     if (!$user || !$user->validatePassword($this->password)) { 
      $this->addError($attribute, 'Incorrect username or 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 = User::findByUsername($this->username); 
    } 

    return $this->_user; 
} 
} 

任何一個可以指教一下我應該做的,我再一次得到的錯誤是:

訪問未申報的靜態屬性:應用程序\型號\用戶:: $用戶

並且這個錯誤對應於這段代碼

foreach (self::$Users as $user) { 
     if (strcasecmp($user['username'], $username) === 0) { 
      return new static($user); 
     } 
    } 

回答

0

在新的User類改變這個方法:

public static function findIdentity($id) 
    { 
     return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); 
    } 
    /** 
    * @inheritdoc 
    */ 
    public static function findIdentityByAccessToken($token, $type = null) 
    { 
     throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); 
    } 
    /** 
    * Finds user by username 
    * 
    * @param string $username 
    * @return static|null 
    */ 
    public static function findByUsername($username) 
    { 
     return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); 
    } 

見多Yii2高級模板 - https://github.com/yiisoft/yii2-app-advanced/blob/master/common/models/User.php#L61

還是拿這個類(從Yii2高級模板)在您的應用程序。

0

爲用戶模型使用一個對象而不是三個不同的對象。

class User extends ActiveRecord implements IdentityInterface 
{ 
    const LOGIN_SCENARIO = 'login'; 
    const CREATE_SCENARIO = 'create'; 
    const UPDATE_SCENARIO = 'update'; 

    /** 
    * Table name 
    * 
    * @return string 
    */ 
    public static function tableName() 
    { 
     return 'user'; 
    } 

    /** 
    * Primary key 
    */ 
    public static function primaryKey() 
    { 
     return ['id']; 
    } 

    /** 
    * Set attribute labels 
    * 
    * @return array 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'login' => Yii::t('app', 'Login'), 
      'password' => Yii::t('app', 'Password') 
     ]; 
    } 

    /** 
    * Rules 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      [ 
       [ 
        'login', 
        'password' 
       ], 
       'required', 
       'on' => self::LOGIN_SCENARIO 
      ], 
      [ 
       'password', 
       'validatePassword', 
       'on' => self::LOGIN_SCENARIO 
      ], 
      [ 
       [ 
        'role', 
        'login', 
        'confirm', 
        'password', 
       ], 
       'required', 
       'on' => self::CREATE_SCENARIO 
      ], 
      [ 
       [ 
        'role', 
        'login', 
       ], 
       'required', 
       'on' => self::UPDATE_SCENARIO 
      ], 
      [ 
       [ 
        'name', 
        'status', 
        'password', 
        'create_dt', 
        'update_dt' 
       ], 
       'safe', 
      ], 
     ]; 
    } 

    /** 
    * Password validation 
    */ 
    public function validatePassword($attribute) 
    { 
     // Validate pass 
    } 

    /** 
    * @param null $id 
    * 
    * @return bool|mixed 
    */ 
    public function saveUser($id = null) 
    { 
     /** @var self $user */ 
     $user = $this->findIdentity($id) ? $this->findIdentity($id) : $this; 
     $user->setScenario($this->scenario); 
     // Make Save 
    } 

    /** 
    * @param $id 
    * 
    * @throws \Exception 
    */ 
    public function deleteUser($id) 
    { 
     /** @var self $user */ 
     if($user = $this->findIdentity($id)) { 
      // Make Delete 
     } 
    } 

    /** 
    * Finds an identity by the given ID. 
    * 
    * @param string|integer $id the ID to be looked for 
    * 
    * @return IdentityInterface the identity object that matches the given ID. 
    * Null should be returned if such an identity cannot be found 
    * or the identity is not in an active state (disabled, deleted, etc.) 
    */ 
    public static function findIdentity($id) 
    { 
     return static::findOne($id); 
    } 

    /** 
    * Returns an ID that can uniquely identify a user identity. 
    * @return string|integer an ID that uniquely identifies a user identity. 
    */ 
    public function getId() 
    { 
     return $this->getPrimaryKey(); 
    } 
} 

而一個用戶模型與用戶一起工作,與不同的場景,可以創建,刪除,請登錄等

而在HTML,用於登錄表單,把這樣的代碼:

<?php $model = new namespace\User() ?> 
<?php $form = ActiveForm::begin(); ?> 
<?= $form->field($model, 'login') ?> 
<?= $form->field($model, 'password')->passwordInput() ?> 

用於登錄,在控制器中,輸入如下代碼:

/** 
    * Login action 
    * 
    * @return mixed 
    */ 
    public function actionLogin() 
    { 
     $user = new namespace\User(); 
     $user->setScenario(namespace\User::LOGIN_SCENARIO); 
     $user->load(Yii::$app->request->post()); 
     if($user->validate()) { 
      // Make Login 
      return true; 
     } 
     return false; 
    }