選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ResetPasswordForm.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }