ResetPasswordForm.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace frontend\models;
  3. use yii\base\Model;
  4. use yii\base\InvalidParamException;
  5. use common\models\User;
  6. /**
  7. * Password reset form
  8. */
  9. class ResetPasswordForm extends Model
  10. {
  11. public $password;
  12. /**
  13. * @var \common\models\User
  14. */
  15. private $_user;
  16. /**
  17. * Creates a form model given a token.
  18. *
  19. * @param string $token
  20. * @param array $config name-value pairs that will be used to initialize the object properties
  21. * @throws \yii\base\InvalidParamException if token is empty or not valid
  22. */
  23. public function __construct($token, $config = [])
  24. {
  25. if (empty($token) || !is_string($token)) {
  26. throw new InvalidParamException('Password reset token cannot be blank.');
  27. }
  28. $this->_user = User::findByPasswordResetToken($token);
  29. if (!$this->_user) {
  30. throw new InvalidParamException('Wrong password reset token.');
  31. }
  32. parent::__construct($config);
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function rules()
  38. {
  39. return [
  40. ['password', 'required'],
  41. ['password', 'string', 'min' => 6],
  42. ];
  43. }
  44. /**
  45. * Resets password.
  46. *
  47. * @return bool if password was reset.
  48. */
  49. public function resetPassword()
  50. {
  51. $user = $this->_user;
  52. $user->setPassword($this->password);
  53. $user->removePasswordResetToken();
  54. return $user->save(false);
  55. }
  56. }