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.
59 lines
1.2 KiB
59 lines
1.2 KiB
12 years ago
|
<?php
|
||
|
|
||
|
namespace app\models;
|
||
|
|
||
12 years ago
|
use Yii;
|
||
12 years ago
|
use yii\base\Model;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* 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;
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* @return array the validation rules.
|
||
|
*/
|
||
12 years ago
|
public function rules()
|
||
|
{
|
||
|
return array(
|
||
12 years ago
|
// username and password are both required
|
||
|
array('username, password', 'required'),
|
||
|
// password is validated by validatePassword()
|
||
12 years ago
|
array('password', 'validatePassword'),
|
||
12 years ago
|
// rememberMe must be a boolean value
|
||
12 years ago
|
array('rememberMe', 'boolean'),
|
||
12 years ago
|
);
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Validates the password.
|
||
|
* This method serves as the inline validation for password.
|
||
|
*/
|
||
12 years ago
|
public function validatePassword()
|
||
|
{
|
||
|
$user = User::findByUsername($this->username);
|
||
12 years ago
|
if (!$user || !$user->validatePassword($this->password)) {
|
||
12 years ago
|
$this->addError('password', 'Incorrect username or password.');
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
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()) {
|
||
|
$user = User::findByUsername($this->username);
|
||
12 years ago
|
Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0);
|
||
12 years ago
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
}
|