LoginForm.php
1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
Yii::import('gii.components.UserIdentity');
class LoginForm extends CFormModel
{
public $password;
private $_identity;
public function rules()
{
return array(
array('password', 'required'),
array('password', 'authenticate'),
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
$this->_identity=new UserIdentity('yiier',$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect password.');
}
/**
* Logs in the user using the given password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity('yiier',$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
Yii::app()->user->login($this->_identity);
return true;
}
else
return false;
}
}