2016-01-02 19:13:18 +05:30
|
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
|
namespace api\models\authentication;
|
2016-01-02 19:13:18 +05:30
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
|
use api\models\base\ApiForm;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
use api\traits\AccountFinder;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
use common\models\Account;
|
2016-01-02 19:13:18 +05:30
|
|
|
|
use Yii;
|
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
|
class LoginForm extends ApiForm {
|
2016-05-11 01:10:06 +05:30
|
|
|
|
use AccountFinder;
|
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
|
|
|
|
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-05-11 01:10:06 +05:30
|
|
|
|
public function getLogin() {
|
|
|
|
|
return $this->login;
|
|
|
|
|
}
|
|
|
|
|
|
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-05-24 01:12:50 +05:30
|
|
|
|
if ($this->rememberMe) {
|
|
|
|
|
// TODO: здесь нужно записать какую-то
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
|
if ($account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY) {
|
|
|
|
|
$account->setPassword($this->password);
|
|
|
|
|
$account->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $account->getJWT();
|
2016-01-02 19:13:18 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|