<?php
/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the 'login' action of 'SiteController'.
* LoginForm的类。
* LoginForm的是保持数据结构等为
* 用户登录表单数据。它被用于“登录”行动'SiteController'。
*/
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
* 声明验证规则。
* 规则状态,需要用户名和密码,
* 和密码进行身份验证。
*/
public function rules()
{
return array(
// username and password are required username 和password需要必填
array('username, password', 'required'),
// rememberMe needs to be a boolean rememberMe需要一个布尔值
array('rememberMe', 'boolean'),
// password needs to be authenticated 需要验证密码
array('password', 'authenticate'), //密码验证函数在下面authenticate
);
}
/**
* Declares attribute labels.
* 声明属性标签。
*/
public function attributeLabels()
{
return array(
'rememberMe'=>'Remember me next time',
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
* *验证密码。
* 这是'验证'的验证,宣布规则()。
* 这里是操作系统的系统 密码和用户名是写死了的
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password. 用户名或密码错误。');
}
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
* *记录在用户模型中使用给定的用户名和密码。
* @返回布尔值是否登录成功
*/
public function login()
{
//echo "username:".$this->username."<BR>";
//echo "password:".$this->password."<BR>";
//die();
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$result = $this->_identity->authenticate();
//var_dump($result);
}
//正确
///echo UserIdentity::ERROR_NONE;
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
}