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-05-30 05:14:17 +05:30
|
|
|
use api\models\AccountIdentity;
|
2016-03-20 05:03:49 +05:30
|
|
|
use api\models\base\ApiForm;
|
2017-01-23 16:52:20 +05:30
|
|
|
use api\validators\TotpValidator;
|
2016-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error as E;
|
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-05-30 05:14:17 +05:30
|
|
|
/**
|
|
|
|
* @method AccountIdentity|null getAccount()
|
|
|
|
*/
|
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;
|
2017-01-23 16:52:20 +05:30
|
|
|
public $token;
|
2016-05-30 05:14:17 +05:30
|
|
|
public $rememberMe = false;
|
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-06-17 02:02:23 +05:30
|
|
|
['login', 'required', 'message' => E::LOGIN_REQUIRED],
|
2016-01-15 14:51:27 +05:30
|
|
|
['login', 'validateLogin'],
|
|
|
|
|
|
|
|
['password', 'required', 'when' => function(self $model) {
|
|
|
|
return !$model->hasErrors();
|
2016-06-17 02:02:23 +05:30
|
|
|
}, 'message' => E::PASSWORD_REQUIRED],
|
2016-01-02 19:13:18 +05:30
|
|
|
['password', 'validatePassword'],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2017-01-23 16:52:20 +05:30
|
|
|
['token', 'required', 'when' => function(self $model) {
|
|
|
|
return !$model->hasErrors() && $model->getAccount()->is_otp_enabled;
|
|
|
|
}, 'message' => E::OTP_TOKEN_REQUIRED],
|
|
|
|
['token', 'validateTotpToken'],
|
|
|
|
|
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-05-30 05:14:17 +05:30
|
|
|
if ($this->getAccount() === null) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::LOGIN_NOT_EXIST);
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validatePassword($attribute) {
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
$account = $this->getAccount();
|
2016-05-30 05:14:17 +05:30
|
|
|
if ($account === null || !$account->validatePassword($this->password)) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::PASSWORD_INCORRECT);
|
2016-01-02 19:13:18 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:20 +05:30
|
|
|
public function validateTotpToken($attribute) {
|
|
|
|
if ($this->hasErrors()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
if (!$account->is_otp_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new TotpValidator(['account' => $account]);
|
2017-02-22 03:50:43 +05:30
|
|
|
$validator->window = 1;
|
2017-01-24 02:20:13 +05:30
|
|
|
$validator->validateAttribute($this, $attribute);
|
2017-01-23 16:52:20 +05:30
|
|
|
}
|
|
|
|
|
2016-02-28 03:20:49 +05:30
|
|
|
public function validateActivity($attribute) {
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
$account = $this->getAccount();
|
2016-08-22 02:39:14 +05:30
|
|
|
if ($account->status === Account::STATUS_BANNED) {
|
|
|
|
$this->addError($attribute, E::ACCOUNT_BANNED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($account->status === Account::STATUS_REGISTERED) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::ACCOUNT_NOT_ACTIVATED);
|
2016-02-28 03:20:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
public function getLogin() {
|
|
|
|
return $this->login;
|
|
|
|
}
|
|
|
|
|
2016-01-02 19:13:18 +05:30
|
|
|
/**
|
2016-05-30 05:14:17 +05:30
|
|
|
* @return \api\components\User\LoginResult|bool
|
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
|
|
|
$account = $this->getAccount();
|
2016-11-02 02:34:10 +05:30
|
|
|
if ($account->password_hash_strategy !== Account::PASS_HASH_STRATEGY_YII2) {
|
2016-05-24 01:12:50 +05:30
|
|
|
$account->setPassword($this->password);
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
|
2016-07-17 22:24:33 +05:30
|
|
|
return Yii::$app->user->login($account, $this->rememberMe);
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAccountClassName() {
|
|
|
|
return AccountIdentity::class;
|
2016-01-02 19:13:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|