Created
February 1, 2020 21:08
-
-
Save dersonsena/e6dc2f807d0eaf0bd6f811ad6752eda0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Domains\Auth\Forms; | |
use Yii; | |
use App\Domains\User\Entity\User; | |
use yii\base\Model; | |
/** | |
* LoginForm is the model behind the login form. | |
* | |
* @property User|null $user This property is read-only. | |
* | |
*/ | |
class LoginForm extends Model | |
{ | |
public $email; | |
public $password; | |
public $rememberMe = true; | |
private $user = false; | |
/** | |
* @return array the validation rules. | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['email', 'password'], 'required'], | |
['email', 'email'], | |
['rememberMe', 'boolean'], | |
['password', 'validatePassword'], | |
]; | |
} | |
public function attributeLabels() | |
{ | |
return [ | |
'email' => 'E-mail', | |
'password' => 'Senha', | |
'rememberMe' => 'Lembrar-me neste PC' | |
]; | |
} | |
/** | |
* 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 email or password.'); | |
} | |
} | |
} | |
/** | |
* Logs in a user using the provided email and password. | |
* @return bool 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 [[email]] | |
* | |
* @return User|null | |
*/ | |
public function getUser() | |
{ | |
if ($this->user === false) { | |
$this->user = User::findByUsername($this->email); | |
} | |
return $this->user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment