Last active
August 29, 2015 14:03
-
-
Save kiaplayer/51c3cb83a55ba28e94b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace backend\models; | |
use Yii; | |
use yii\base\Model; | |
/** | |
* Форма логина для backend | |
*/ | |
class LoginForm extends Model | |
{ | |
public $username; | |
public $password; | |
public $rememberMe = true; | |
private $_user = false; | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['username', 'password'], 'required'], | |
['rememberMe', 'boolean'], | |
['password', 'validatePassword'], | |
]; | |
} | |
/** | |
* Проверка пароля | |
* This method serves as the inline validation for password. | |
*/ | |
public function validatePassword() | |
{ | |
if (!$this->hasErrors()) { | |
$user = $this->getUser(); | |
if (!$user || !$user->validatePassword($this->password)) { | |
$this->addError('password', 'Incorrect username or password.'); | |
} | |
} | |
} | |
/** | |
* Аутентификация пользователя | |
* @return boolean результат аутентификации | |
*/ | |
public function login() | |
{ | |
if ($this->validate()) { | |
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Получение пользователя | |
* @return BackendUser|null | |
*/ | |
public function getUser() | |
{ | |
if ($this->_user === false) { | |
$this->_user = BackendUser::findOne([ | |
'username' => $this->username, | |
'is_active' => true, | |
]); | |
} | |
return $this->_user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment