Blame view

thread/modules/user/models/form/ResetPasswordForm.php 1.3 KB
d1f8bd40   Alexey Boroda   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
  <?php
  namespace thread\modules\user\models\form;
  
  use Yii;
  use yii\base\InvalidParamException;
  //
  use thread\modules\user\models\User as UserModel;
  
  /**
   * Password reset form
   */
  class ResetPasswordForm extends CommonForm
  {
      /**
       * @var UserModel
       */
      private $_user;
  
      /**
       * Creates a form model given a token.
       *
       * @param string $token
       * @param array $config name-value pairs that will be used to initialize the object properties
       * @throws \yii\base\InvalidParamException if token is empty or not valid
       */
      public function __construct($token, $config = [])
      {
          if (empty($token) || !is_string($token)) {
              throw new InvalidParamException('Password reset token cannot be blank.');
          }
          $this->_user = UserModel::findByPasswordResetToken($token);
          if (!$this->_user) {
              throw new InvalidParamException('Wrong password reset token.');
          }
          parent::__construct($config);
      }
  
      /**
       * Resets password.
       *
       * @return boolean if password was reset.
       */
      public function setPassword()
      {
          $user = $this->_user;
          $user->setScenario('setPassword');
          $user->setPassword($this->password);
          $user->removePasswordResetToken();
  
          return $user->save(false);
      }
  }