1 <?php
2
3 4 5 6 7
8 class UserLogin extends CFormModel
9 {
10 public $username;
11 public $password;
12 public $rememberMe;
13
14 15 16 17 18
19 public function rules()
20 {
21 return array(
22
23 array('username, password', 'required'),
24
25 array('rememberMe', 'boolean'),
26
27 array('password', 'authenticate'),
28 );
29 }
30
31 32 33
34 public function attributeLabels()
35 {
36 return array(
37 'rememberMe'=>UserModule::t("Remember me next time"),
38 'username'=>UserModule::t("username or email"),
39 'password'=>UserModule::t("password"),
40 );
41 }
42
43 44 45 46
47 public function authenticate($attribute,$params)
48 {
49 if(!$this->hasErrors())
50 {
51 $identity=new UserIdentity($this->username,$this->password);
52 $identity->authenticate();
53 switch($identity->errorCode)
54 {
55 case UserIdentity::ERROR_NONE:
56 $duration=$this->rememberMe ? Yii::app()->controller->module->rememberMeTime : 0;
57 Yii::app()->user->login($identity,$duration);
58 break;
59 case UserIdentity::ERROR_EMAIL_INVALID:
60 $this->addError("username",UserModule::t("Email is incorrect."));
61 break;
62 case UserIdentity::ERROR_USERNAME_INVALID:
63 $this->addError("username",UserModule::t("Username is incorrect."));
64 break;
65 case UserIdentity::ERROR_STATUS_NOTACTIV:
66 $this->addError("status",UserModule::t("You account is not activated."));
67 break;
68 case UserIdentity::ERROR_STATUS_BAN:
69 $this->addError("status",UserModule::t("You account is blocked."));
70 break;
71 case UserIdentity::ERROR_PASSWORD_INVALID:
72 $this->addError("password",UserModule::t("Password is incorrect."));
73 break;
74 }
75 }
76 }
77 }
78