database - Connect Yii2 Login form to DB -
i installed yii2 basic , want map pre-existing login form db. how can this? have db ready changed db connection settings. changes need in model? exiting login model:
<?php namespace app\models; use yii; use yii\base\model; /** * loginform model behind login form. */ class loginform extends model { public $username; public $password; public $rememberme = true; private $_user = false; /** * @return array validation rules. */ public function rules() { return [ // username , password both required [['username', 'password'], 'required'], // rememberme must boolean value ['rememberme', 'boolean'], // password validated validatepassword() ['password', 'validatepassword'], ]; } /** * validates password. * method serves inline validation password. * * @param string $attribute attribute being validated * @param array $params additional name-value pairs given in 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 user using provided username , password. * @return boolean whether user logged in */ public function login() { if ($this->validate()) { return yii::$app->user->login($this->getuser(), $this->rememberme ? 3600*24*30 : 0); } return false; } /** * finds user [[username]] * * @return user|null */ public function getuser() { if ($this->_user === false) { $this->_user = user::findbyusername($this->username); } return $this->_user; } } this user model:
<?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 $user) { if ($user['accesstoken'] === $token) { return new static($user); } } return null; } /** * finds user username * * @param string $username * @return static|null */ public static function findbyusername($username) { foreach (self::$users $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 validate * @return boolean if password provided valid current user */ public function validatepassword($password) { return $this->password === $password; } } how change achieve login connection db?
you need change in user.php model (mainly 3 methods findidentity, findbyusername, validatepassword). below link surely go through this. 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 if needed ]; return new static($user_info); } } /** * finds user 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, if needed ]; return new static($user_info); } return null; } /** * validates password * * @param string $password password validate * @return boolean if password provided valid current user */ public function validatepassword($password) { return $this->password === $password; // apply encryption algo here $password, if have used } // ... // ... other functions here }
Comments
Post a Comment