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;
|
|
|
|
use yii\base\Model;
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
class LoginForm extends Model {
|
|
|
|
|
|
|
|
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 formName() {
|
|
|
|
return '';
|
|
|
|
}
|
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
|
|
|
|
|
|
|
['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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs in a user using the provided username and password.
|
|
|
|
*
|
|
|
|
* @return boolean whether the user is logged in successfully
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
return Yii::$app->user->login($this->getAccount(), $this->rememberMe ? 3600 * 24 * 30 : 0);
|
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
|
|
|
}
|