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.

51 lines
1023 B

12 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\models;
12 years ago
use Yii;
12 years ago
use yii\base\Model;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class LoginForm extends Model
{
public $username;
public $password;
12 years ago
public $rememberMe = true;
12 years ago
public function rules()
{
return array(
array('username', 'required'),
array('password', 'required'),
array('password', 'validatePassword'),
12 years ago
array('rememberMe', 'boolean'),
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
public function login()
{
if ($this->validate()) {
$user = User::findByUsername($this->username);
Yii::$app->getUser()->login($user, $this->rememberMe ? 3600*24*30 : 0);
12 years ago
return true;
} else {
return false;
}
}
12 years ago
}