0
0

ResetPasswordForm.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * @inheritdoc
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'username' => '验证码',
  51. 'name' => '姓名',
  52. 'email' => '电子邮件',
  53. 'password' => '密码',
  54. 'body' => '内容',
  55. ];
  56. }
  57. /**
  58. * Resets password.
  59. *
  60. * @return bool if password was reset.
  61. */
  62. public function resetPassword()
  63. {
  64. $user = $this->_user;
  65. $user->setPassword($this->password);
  66. $user->removePasswordResetToken();
  67. return $user->save(false);
  68. }
  69. }