Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.4 KiB

12 years ago
<?php
namespace app\models;
12 years ago
use Yii;
12 years ago
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
12 years ago
*/
class LoginForm extends Model
{
public $username;
public $password;
12 years ago
public $rememberMe = true;
11 years ago
private $_user = false;
12 years ago
/**
* @return array the validation rules.
*/
12 years ago
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// password is validated by validatePassword()
11 years ago
['password', 'validatePassword'],
// rememberMe must be a boolean value
11 years ago
['rememberMe', 'boolean'],
];
12 years ago
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*/
12 years ago
public function validatePassword()
{
11 years ago
$user = $this->getUser();
12 years ago
if (!$user || !$user->validatePassword($this->password)) {
12 years ago
$this->addError('password', 'Incorrect username or password.');
}
}
12 years ago
/**
* Logs in a user using the provided username and password.
* @return boolean whether the user is logged in successfully
*/
12 years ago
public function login()
{
if ($this->validate()) {
11 years ago
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
12 years ago
} else {
return false;
}
}
/**
11 years ago
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
11 years ago
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
11 years ago
return $this->_user;
}
}