ResetPasswordForm.php 1.24 KB
Newer Older
1
<?php
2
namespace frontend\models;
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

use common\models\User;
use yii\base\InvalidParamException;
use yii\base\Model;
use Yii;

/**
 * Password reset form
 */
class ResetPasswordForm extends Model
{
	public $password;

	/**
	 * @var \common\models\User
	 */
	private $_user;

	/**
22
	 * Creates a form model given a token.
23 24
	 *
	 * @param string $token
25
	 * @param array $config name-value pairs that will be used to initialize the object properties
26 27
	 * @throws \yii\base\InvalidParamException if token is empty or not valid
	 */
28
	public function __construct($token, $config = [])
29 30 31 32
	{
		if (empty($token) || !is_string($token)) {
			throw new InvalidParamException('Password reset token cannot be blank.');
		}
33
		$this->_user = User::findByPasswordResetToken($token);
34 35 36
		if (!$this->_user) {
			throw new InvalidParamException('Wrong password reset token.');
		}
37
		parent::__construct($config);
38 39 40
	}

	/**
41
	 * @inheritdoc
42 43 44 45 46 47 48 49 50 51 52
	 */
	public function rules()
	{
		return [
			['password', 'required'],
			['password', 'string', 'min' => 6],
		];
	}

	/**
	 * Resets password.
53
	 *
54 55 56 57 58
	 * @return boolean if password was reset.
	 */
	public function resetPassword()
	{
		$user = $this->_user;
59 60 61
		$user->password = $this->password;
		$user->removePasswordResetToken();
		return $user->save();
62
	}
63
}