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.

49 lines
924 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;
public function rules()
{
return array(
array('username', 'required'),
array('password', 'required'),
array('password', 'validatePassword'),
);
}
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);
return true;
} else {
return false;
}
}
12 years ago
}