ResetPasswordForm.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace frontend\models\form;
  9. use Yii;
  10. use common\models\User;
  11. use yii\base\InvalidParamException;
  12. use yii\base\Model;
  13. /**
  14. * Password reset form
  15. */
  16. class ResetPasswordForm extends Model
  17. {
  18. public $password;
  19. /**
  20. * @var \common\models\User
  21. */
  22. private $_user;
  23. /**
  24. * Creates a form model given a token.
  25. *
  26. * @param string $token
  27. * @param array $config name-value pairs that will be used to initialize the object properties
  28. * @throws \yii\base\InvalidParamException if token is empty or not valid
  29. */
  30. public function __construct($token, $config = [])
  31. {
  32. if (empty($token) || ! is_string($token)) {
  33. throw new InvalidParamException('Password reset token cannot be blank.');
  34. }
  35. $this->_user = User::findByPasswordResetToken($token);
  36. if (! $this->_user) {
  37. throw new InvalidParamException('Wrong password reset token.');
  38. }
  39. parent::__construct($config);
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function rules()
  45. {
  46. return [
  47. ['password', 'required'],
  48. ['password', 'string', 'min' => 6],
  49. ];
  50. }
  51. /**
  52. * @inheritdoc
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'password' => Yii::t('app', 'Password'),
  58. ];
  59. }
  60. /**
  61. * Resets password.
  62. *
  63. * @return boolean if password was reset.
  64. */
  65. public function resetPassword()
  66. {
  67. $user = $this->_user;
  68. $user->password = $this->password;
  69. $user->repassword = $this->password;
  70. $user->removePasswordResetToken();
  71. return $user->save(false);
  72. }
  73. }