LoginForm.php 1020 B

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