2016-02-06 2 views
1

Я только что установил Yii2 Basic и хочу отобразить ранее существующую форму входа в db. Как я могу это сделать? У меня есть db ready, а также изменились настройки подключения db. Какие изменения мне нужно сделать в модели? Это мой выход Войти Модель:Подключить форму регистрации YY2 к DB

<?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); 
     } 
     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; 
    } 
} 

Это мой Пользователь Модель:

<?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; 
    } 
} 

Как изменить это, чтобы достичь соединения входа из БД?

ответ

1

Вам нужно изменить в User.php модели (в основном тремя методами findIdentity, findByUsername, validatePassword). Ниже ссылка, безусловно, поможет вам пройти через это. Link 1, Link 2 и Link 3

<?php 

namespace app\models; 
use app\models\Users; // generated using Gii; user master table in db 

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

    /** 
    * @inheritdoc 
    */ 
    public static function findIdentity($id) 
    { 
     $user = Users::findOne(['id'=>$id]); 
     if(!$user) 
     { 
      return null; 
     } 
     else 
     { 
      $user_info = 
       [ 
        'id' => $user->id, 
        'username' => $user->username, 
        'first_name' => $user->first_name, 
        'last_name' => $user->last_name, 
        'email' => $user->email,         
        'image'=>$user->image,     
        // assign other attributes too if needed 
       ]; 
      return new static($user_info); 
     } 
    } 

    /** 
    * Finds user by username 
    * 
    * @param string  $username 
    * @return static|null 
    */ 
    public static function findByUsername($username) 
    { 
     $user = Users::findOne(['id'=>$id]); 
     if(!$user) 
     { 
      return null; 
     } 
     else 
     { 
      $user_info = 
       [ 
        'id' => $user->id, 
        'username' => $user->username, 
        'first_name' => $user->first_name, 
        'last_name' => $user->last_name, 
        'email' => $user->email,         
        'image'=>$user->image,     
        // assign other attributes, too if needed 
       ]; 
      return new static($user_info); 
     } 

     return null; 
    } 

    /** 
    * 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; // apply any encryption algo here to $password, if you have used any 
    } 

    // ... 
    // ... other functions here 
} 
Смежные вопросы