ctms/ctms-home/models/ResetPasswordForm.php
2025-04-10 23:19:13 +08:00

73 lines
1.6 KiB
PHP
Executable File

<?php
# @Author: fm453
# @Date: 1970-01-01T08:00:00+08:00
# @Email: fm43@hiluker.com
# @Last modified by: fm453
# @Last modified time: 2021-09-14T19:25:39+08:00
namespace frontend\models;
use yii\base\Model;
use yii\base\InvalidParamException;
use common\models\Member;
/**
* Password reset form
*/
class ResetPasswordForm extends Model
{
public $password;
/**
* @var \common\models\Member
*/
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('密码不可设置为空白.');
}
$this->_user = User::findByPasswordResetToken($token);
if (!$this->_user) {
throw new InvalidParamException('密码重置凭证不对.');
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* Resets password.
*
* @return bool if password was reset.
*/
public function resetPassword()
{
$user = $this->_user;
$user->setPassword($this->password);
$user->removePasswordResetToken();
return $user->save(false);
}
}