You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6KB

  1. <?php
  2. namespace frontend\models;
  3. use common\models\User;
  4. use yii\base\InvalidParamException;
  5. use yii\base\Model;
  6. use Yii;
  7. /**
  8. * Password reset form
  9. */
  10. class ResetPasswordForm extends Model {
  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. if (empty($token) || !is_string($token)) {
  25. throw new InvalidParamException('Password reset token cannot be blank.');
  26. }
  27. $this->_user = User::findByPasswordResetToken($token);
  28. if (!$this->_user) {
  29. throw new InvalidParamException('Wrong password reset token.');
  30. }
  31. parent::__construct($config);
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function rules() {
  37. return [
  38. ['password', 'required'],
  39. ['password', 'string', 'min' => 6],
  40. ];
  41. }
  42. /**
  43. * Resets password.
  44. *
  45. * @return boolean if password was reset.
  46. */
  47. public function resetPassword() {
  48. $user = $this->_user;
  49. $user->setPassword($this->password);
  50. $user->removePasswordResetToken();
  51. return $user->save();
  52. }
  53. public function attributeLabels() {
  54. return [
  55. 'password' => 'Mot de passe',
  56. ];
  57. }
  58. }