2016-01-02 19:13:18 +05:30
|
|
|
|
<?php
|
2016-01-03 05:48:37 +05:30
|
|
|
|
namespace api\models;
|
2016-01-02 19:13:18 +05:30
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
|
use common\models\Account;
|
2016-01-02 19:13:18 +05:30
|
|
|
|
use Yii;
|
|
|
|
|
|
2016-01-21 02:44:29 +05:30
|
|
|
|
class LoginForm extends BaseApiForm {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
|
|
public $login;
|
2016-01-02 19:13:18 +05:30
|
|
|
|
public $password;
|
|
|
|
|
public $rememberMe = true;
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
private $_account;
|
2016-01-02 19:13:18 +05:30
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public function rules() {
|
2016-01-02 19:13:18 +05:30
|
|
|
|
return [
|
2016-01-15 14:51:27 +05:30
|
|
|
|
['login', 'required', 'message' => 'error.login_required'],
|
|
|
|
|
['login', 'validateLogin'],
|
|
|
|
|
|
|
|
|
|
['password', 'required', 'when' => function(self $model) {
|
|
|
|
|
return !$model->hasErrors();
|
|
|
|
|
}, 'message' => 'error.password_required'],
|
2016-01-02 19:13:18 +05:30
|
|
|
|
['password', 'validatePassword'],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-02-28 03:20:49 +05:30
|
|
|
|
['login', 'validateActivity'],
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
['rememberMe', 'boolean'],
|
2016-01-02 19:13:18 +05:30
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public function validateLogin($attribute) {
|
2016-01-02 19:13:18 +05:30
|
|
|
|
if (!$this->hasErrors()) {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
if (!$this->getAccount()) {
|
|
|
|
|
$this->addError($attribute, 'error.' . $attribute . '_not_exist');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validatePassword($attribute) {
|
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
|
if (!$account || !$account->validatePassword($this->password)) {
|
|
|
|
|
$this->addError($attribute, 'error.' . $attribute . '_incorrect');
|
2016-01-02 19:13:18 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-28 03:20:49 +05:30
|
|
|
|
public function validateActivity($attribute) {
|
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
|
if ($account->status !== Account::STATUS_ACTIVE) {
|
|
|
|
|
$this->addError($attribute, 'error.account_not_activated');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-02 19:13:18 +05:30
|
|
|
|
/**
|
2016-02-23 03:52:04 +05:30
|
|
|
|
* @return bool|string JWT с информацией об аккаунте
|
2016-01-02 19:13:18 +05:30
|
|
|
|
*/
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public function login() {
|
|
|
|
|
if (!$this->validate()) {
|
2016-01-02 19:13:18 +05:30
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-02-23 03:52:04 +05:30
|
|
|
|
return $this->getAccount()->getJWT();
|
2016-01-02 19:13:18 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-03 05:48:37 +05:30
|
|
|
|
* @return Account|null
|
2016-01-02 19:13:18 +05:30
|
|
|
|
*/
|
2016-01-15 14:51:27 +05:30
|
|
|
|
protected function getAccount() {
|
|
|
|
|
if ($this->_account === NULL) {
|
|
|
|
|
$attribute = strpos($this->login, '@') ? 'email' : 'username';
|
|
|
|
|
$this->_account = Account::findOne([$attribute => $this->login]);
|
2016-01-02 19:13:18 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
return $this->_account;
|
2016-01-02 19:13:18 +05:30
|
|
|
|
}
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-01-02 19:13:18 +05:30
|
|
|
|
}
|