UserIdentity.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * UserIdentity represents the data needed to identity a user.
  4. * It contains the authentication method that checks if the provided
  5. * data can identity the user.
  6. */
  7. class UserIdentity extends CUserIdentity
  8. {
  9. public $_id;
  10. /**
  11. * Authenticates a user.
  12. * The example implementation makes sure if the username and password
  13. * are both 'demo'.
  14. * In practical applications, this should be changed to authenticate
  15. * against some persistent user identity storage (e.g. database).
  16. * @return boolean whether authentication succeeds.
  17. */
  18. public function authenticate()
  19. {
  20. $criteria = new EMongoCriteria;
  21. $criteria->email = $this->username;
  22. $user = User::model()->find($criteria);
  23. if (!$user){
  24. $this->errorCode=self::ERROR_USERNAME_INVALID;
  25. $this->errorMessage = "用户不存在";
  26. } else if ($user->status == 0){
  27. $this->errorCode=self::ERROR_USERNAME_INVALID;
  28. $this->errorMessage = "用户未激活";
  29. } else if ($user->status == -1){
  30. $this->errorCode=self::ERROR_USERNAME_INVALID;
  31. $this->errorMessage = "用户不存在";
  32. } else if ($user->pass !== md5($this->password)){
  33. $this->errorCode=self::ERROR_PASSWORD_INVALID;
  34. $this->errorMessage = "密码错误";
  35. } else {
  36. $this->_id = $user->_id;
  37. $this->errorCode=self::ERROR_NONE;
  38. }
  39. return !$this->errorCode;
  40. }
  41. public function getId()
  42. {
  43. return $this->_id;
  44. }
  45. }