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.

ResetPasswordForm.php 1.6KB

8 jaren geleden
8 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }