Blame view

protected/modules/admin/models/ChangePasswordForm.php 1.6 KB
a1684257   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  <?php
  class ChangePasswordForm extends CFormModel
  {
      public $currentPassword;
      public $password;
      public $passwordVerify;
  
      public function rules()
      {
          return array(
              array('currentPassword, password, passwordVerify', 'required'),
              array('currentPassword, password, passwordVerify', 'length', 'max' => 64),
  
              array('currentPassword', 'authenticate'),
  
              array('passwordVerify', 'compare', 'compareAttribute' => 'password', 'message' => 'Пароли не совпадают'),
          );
      }
  
      public function authenticate($attribute, $params)
      {
          $identity = new AdminUserIdentity(Yii::app()->user->name, $this->currentPassword);
          if (!$identity->authenticate()) {
              switch ($identity->errorCode) {
                  case CUserIdentity::ERROR_PASSWORD_INVALID:
                      $this->addError('currentPassword', 'Неверный пароль');
                      break;
                  default:
                      $this->addError('currentPassword', 'Чтото пошло нетак');
                      break;
              }
          }
      }
  
      public function attributeLabels()
      {
          return array(
              'currentPassword' => 'Текущий пароль',
  
              'password' => 'Пароль',
              'passwordVerify' => 'Подтверждение',
          );
      }
  
      public function changePassword()
      {
          /** @var $model AdminUser */
          $model = AdminUser::model()->findByPk(Yii::app()->user->name);
          $model->setPasswordHash($this->password);
          return $model->save();
      }
  
  
  }