EliteConf
 All Data Structures Namespaces Files Functions Variables Pages
LoginForm.php
Go to the documentation of this file.
1 <?php
2 
8 class LoginForm extends CFormModel
9 {
10  public $username;
11  public $password;
12  public $rememberMe;
13 
14  private $_identity;
15 
21  public function rules()
22  {
23  return array(
24  // username and password are required
25  array('username, password', 'required'),
26  // rememberMe needs to be a boolean
27  array('rememberMe', 'boolean'),
28  // password needs to be authenticated
29  array('password', 'authenticate'),
30  );
31  }
32 
36  public function attributeLabels()
37  {
38  return array(
39  'rememberMe'=>'Remember me next time',
40  );
41  }
42 
47  public function authenticate($attribute,$params)
48  {
49  if(!$this->hasErrors())
50  {
51  $this->_identity=new UserIdentity($this->username,$this->password);
52  if(!$this->_identity->authenticate())
53  $this->addError('password','Incorrect username or password.');
54  }
55  }
56 
61  public function login()
62  {
63  if($this->_identity===null)
64  {
65  $this->_identity=new UserIdentity($this->username,$this->password);
66  $this->_identity->authenticate();
67  }
68  if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
69  {
70  $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
71  Yii::app()->user->login($this->_identity,$duration);
72  return true;
73  }
74  else
75  return false;
76  }
77 }