mint

LoginForm.php

Jun 23rd, 2012
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * LoginForm class.
  5.  * LoginForm is the data structure for keeping
  6.  * user login form data. It is used by the 'login' action of 'SiteController'.
  7.  */
  8. class LoginForm extends CFormModel
  9. {
  10.     public $username;
  11.     public $password;
  12.     public $rememberMe=false;
  13.  
  14.     private $_identity;
  15.  
  16.     /**
  17.      * Declares the validation rules.
  18.      * The rules state that username and password are required,
  19.      * and password needs to be authenticated.
  20.      */
  21.     public function rules()
  22.     {
  23.         return array(
  24.             // username and password are required
  25.             array('username, password', 'required', 'on'=>'login'),
  26.             // rememberMe needs to be a boolean
  27.             array('rememberMe', 'boolean'),
  28.             // password needs to be authenticated
  29.             array('password', 'authenticate'),
  30.             array('username, password', 'safe'),
  31.         );
  32.     }
  33.  
  34.     /**
  35.      * Declares attribute labels.
  36.      */
  37.     public function attributeLabels()
  38.     {
  39.         return array(
  40.             'rememberMe'=>'Remember me next time',
  41.         );
  42.     }
  43.  
  44.     /**
  45.      * Authenticates the password.
  46.      * This is the 'authenticate' validator as declared in rules().
  47.      */
  48.     public function authenticate($attribute,$params)
  49.     {
  50.        
  51.                         if(!$this->hasErrors())
  52.                 {
  53.                         $this->_identity=new UserIdentity($this->username,$this->password);
  54.                         if(!$this->_identity->authenticate())
  55.                                 $this->addError('password','Incorrect username or password.');
  56.                 }
  57.  
  58.                
  59.  
  60.  
  61.     }
  62.     /**
  63.      * Logs in the user using the given username and password in the model.
  64.      * @return boolean whether login is successful
  65.      */
  66.     public function login()
  67.     {
  68.         if($this->_identity===null)
  69.         {
  70.             $this->_identity=new UserIdentity($this->username,$this->password);
  71.             $this->_identity->authenticate();
  72.         }
  73.         if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
  74.         {
  75.             $duration=$this->rememberMe ? 0 : 0;
  76.             Yii::app()->user->login($this->_identity,$duration);
  77.             return true;
  78.         }
  79.         else
  80.             return false;
  81.     }
  82. }
RAW Paste Data