ResetPasswordForm.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 backend\models\form;
  9. use yii;
  10. use yii\base\Event;
  11. use yii\db\BaseActiveRecord;
  12. use yii\base\InvalidParamException;
  13. use common\models\AdminUser;
  14. /**
  15. * Password reset form
  16. */
  17. class ResetPasswordForm extends \yii\base\Model
  18. {
  19. public $password;
  20. private $_user;
  21. public function __construct($token, $config = [])
  22. {
  23. if (empty($token) || ! is_string($token)) {
  24. throw new InvalidParamException('Password reset token cannot be blank.');
  25. }
  26. $this->_user = AdminUser::findByPasswordResetToken($token);
  27. if (! $this->_user) {
  28. throw new InvalidParamException('Wrong password reset token.');
  29. }
  30. parent::__construct($config);
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function rules()
  36. {
  37. return [
  38. ['password', 'required'],
  39. ['password', 'string', 'min' => 6],
  40. ];
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'password' => yii::t('app', 'Password'),
  49. ];
  50. }
  51. /**
  52. * Resets password.
  53. *
  54. * @return boolean if password was reset.
  55. */
  56. public function resetPassword()
  57. {
  58. $user = $this->_user;
  59. $user->setPassword($this->password);
  60. $user->removePasswordResetToken();
  61. Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_UPDATE);
  62. return $user->save(false);
  63. }
  64. }