123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace frontend\models;
- use yii\base\Model;
- use yii\base\InvalidParamException;
- use common\models\User;
- /**
- * Password reset form
- */
- class ResetPasswordForm extends Model
- {
- public $password;
- /**
- * @var \common\models\User
- */
- private $_user;
- /**
- * Creates a form model given a token.
- *
- * @param string $token
- * @param array $config name-value pairs that will be used to initialize the object properties
- * @throws \yii\base\InvalidParamException if token is empty or not valid
- */
- public function __construct($token, $config = [])
- {
- if (empty($token) || !is_string($token)) {
- throw new InvalidParamException('Password reset token cannot be blank.');
- }
- $this->_user = User::findByPasswordResetToken($token);
- if (!$this->_user) {
- throw new InvalidParamException('Wrong password reset token.');
- }
- parent::__construct($config);
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- ['password', 'required'],
- ['password', 'string', 'min' => 6],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'username' => '验证码',
- 'name' => '姓名',
- 'email' => '电子邮件',
- 'password' => '密码',
- 'body' => '内容',
- ];
- }
- /**
- * Resets password.
- *
- * @return bool if password was reset.
- */
- public function resetPassword()
- {
- $user = $this->_user;
- $user->setPassword($this->password);
- $user->removePasswordResetToken();
- return $user->save(false);
- }
- }
|