Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

73 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. {
  12. public $password;
  13. /**
  14. * @var \common\models\User
  15. */
  16. private $_user;
  17. /**
  18. * Creates a form model given a token.
  19. *
  20. * @param string $token
  21. * @param array $config name-value pairs that will be used to initialize the object properties
  22. * @throws \yii\base\InvalidParamException if token is empty or not valid
  23. */
  24. public function __construct($token, $config = [])
  25. {
  26. if (empty($token) || !is_string($token)) {
  27. throw new InvalidParamException('Password reset token cannot be blank.');
  28. }
  29. $this->_user = User::findByPasswordResetToken($token);
  30. if (!$this->_user) {
  31. throw new InvalidParamException('Wrong password reset token.');
  32. }
  33. parent::__construct($config);
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. ['password', 'required'],
  42. ['password', 'string', 'min' => 6],
  43. ];
  44. }
  45. /**
  46. * Resets password.
  47. *
  48. * @return boolean if password was reset.
  49. */
  50. public function resetPassword()
  51. {
  52. $user = $this->_user;
  53. $user->setPassword($this->password);
  54. $user->removePasswordResetToken();
  55. return $user->save();
  56. }
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'password' => 'Mot de passe',
  61. ];
  62. }
  63. }