accounts/api/modules/login/models/AuthenticationForm.php

65 lines
1.7 KiB
PHP

<?php
namespace api\modules\login\models;
use common\models\Account;
use Yii;
use yii\base\Model;
class AuthenticationForm extends Model {
public $email;
public $password;
public $rememberMe = true;
private $_user;
public function rules() {
return [
['email', 'required', 'message' => 'error.email_required'],
['email', 'email', 'message' => 'error.email_invalid'],
['email', 'exist', 'targetClass' => Account::class, 'skipOnError' => true, 'message' => 'error.email_not_exist'],
['password', 'required', 'when' => function(self $model) {
return !$model->hasErrors();
}, 'message' => 'error.password_required'],
['password', 'validatePassword'],
['rememberMe', 'boolean'],
];
}
public function validatePassword($attribute) {
if (!$this->hasErrors()) {
$account = $this->getAccount();
if (!$account || !$account->validatePassword($this->password)) {
$this->addError($attribute, 'error.password_incorrect');
}
}
}
/**
* 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 false;
}
return Yii::$app->user->login($this->getAccount(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
/**
* @return Account|null
*/
protected function getAccount() {
if ($this->_user === NULL) {
$this->_user = Account::findByEmail($this->email);
}
return $this->_user;
}
}