Commit 5b36503c by Alexander Makarov

One less SQL query for login sequence in basic and advanced apps

parent b06caa29
...@@ -13,6 +13,7 @@ class LoginForm extends Model ...@@ -13,6 +13,7 @@ class LoginForm extends Model
public $username; public $username;
public $password; public $password;
public $rememberMe = true; public $rememberMe = true;
private $_user;
/** /**
* @return array the validation rules. * @return array the validation rules.
...@@ -35,7 +36,7 @@ class LoginForm extends Model ...@@ -35,7 +36,7 @@ class LoginForm extends Model
*/ */
public function validatePassword() public function validatePassword()
{ {
$user = User::findByUsername($this->username); $user = $this->getUserByUsername($this->username);
if (!$user || !$user->validatePassword($this->password)) { if (!$user || !$user->validatePassword($this->password)) {
$this->addError('password', 'Incorrect username or password.'); $this->addError('password', 'Incorrect username or password.');
} }
...@@ -48,11 +49,25 @@ class LoginForm extends Model ...@@ -48,11 +49,25 @@ class LoginForm extends Model
public function login() public function login()
{ {
if ($this->validate()) { if ($this->validate()) {
$user = User::findByUsername($this->username); $user = $this->getUserByUsername($this->username);
Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0); Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0);
return true; return true;
} else { } else {
return false; return false;
} }
} }
/**
* Finds user by username
*
* @param string $username
* @return User|null
*/
private function getUserByUsername($username)
{
if ($this->_user === null) {
$this->_user = User::findByUsername($username);
}
return $this->_user;
}
} }
...@@ -13,6 +13,7 @@ class LoginForm extends Model ...@@ -13,6 +13,7 @@ class LoginForm extends Model
public $username; public $username;
public $password; public $password;
public $rememberMe = true; public $rememberMe = true;
private $_user;
/** /**
* @return array the validation rules. * @return array the validation rules.
...@@ -35,7 +36,7 @@ class LoginForm extends Model ...@@ -35,7 +36,7 @@ class LoginForm extends Model
*/ */
public function validatePassword() public function validatePassword()
{ {
$user = User::findByUsername($this->username); $user = $this->getUserByUsername($this->username);
if (!$user || !$user->validatePassword($this->password)) { if (!$user || !$user->validatePassword($this->password)) {
$this->addError('password', 'Incorrect username or password.'); $this->addError('password', 'Incorrect username or password.');
} }
...@@ -48,11 +49,25 @@ class LoginForm extends Model ...@@ -48,11 +49,25 @@ class LoginForm extends Model
public function login() public function login()
{ {
if ($this->validate()) { if ($this->validate()) {
$user = User::findByUsername($this->username); $user = $this->getUserByUsername($this->username);
Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0); Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0);
return true; return true;
} else { } else {
return false; return false;
} }
} }
/**
* Finds user by username
*
* @param string $username
* @return User|null
*/
private function getUserByUsername($username)
{
if ($this->_user === null) {
$this->_user = User::findByUsername($username);
}
return $this->_user;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment