我试图在Yii2的高级模板中最后一次更改密码后经过一定天数后更改用户密码。
流程应如下: - 用户登录 - 登录成功后计划自密码更改后的天数 - 如果超过n天存在表格更改密码 - 更改密码表格需要当前密码,新密码和虚拟密码以及提交按钮 - 验证密码要求并保存密码
Sofar我使用标准的SiteController逻辑登录用户。在actionLogin部分中,我已经包含了测试elpased days数量并触发更改密码的逻辑。 将显示changePasswordForm,但在完成时,不会根据当前密码检查旧密码。控制权被传递回操作Login,变量$ intervalDagen保持为NULL,因此用户返回索引页面和消息  Kan niet nogmaals inloggen。' ('无法再次登录')显示。
我首先尝试在操作Login中包含所有逻辑但是同样失败,因此我尝试触发actionChangepassword。
所以,我的问题是模型changePasswordForm中的验证检查永远不会被执行,并且$ intervalDagen的值不会被保留。 正如您所看到的,我还没有实现实际更改密码的代码,因为我还无法获得新密码的值。
SiteController:
public function actionLogin()
{
static $intervalDagen;
echo '<p>$intervalDagen<br /></p>';
var_dump($intervalDagen);
if (!Yii::$app->user->isGuest && $intervalDagen <= 60) {
Yii::$app->session->setFlash('failure', 'Kan niet nogmaals inloggen.');
return $this->goHome();
}
$model = new GebruikerLoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login())
{
$epoch = Yii::$app->user->identity->updated_at;
$dt = new \DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
$laatstBijgewerkt = strtotime($dt->format('d-m-Y H:i:s'));
$vandaag = strtotime(date('d-m-Y H:i:s'));
$intervalSeconden = $vandaag - $laatstBijgewerkt;
$intervalDagen = intval($intervalSeconden / 86400);
if ($intervalDagen > 60)
{
$modeluser = Gebruiker::find()->where([
'username'=>Yii::$app->user->identity->username
])->one();
$changePasswordc = Yii::$app->createController('site');
$passwordChanged = $changePasswordc[0]->actionChangepassword($modeluser);
echo $passwordChanged;
if ($passwordChanged)
{
}
else{
}
}
//return $this->goBack();
return $this->goHome();
}
else
{
return $this->render('login', [
'model' => $model,
]);
}
}
model changePasswordForm:
<?php
/**
* Created by PhpStorm.
* User: Ynze
* Date: 23-1-2017
* Time: 21:08
*/
namespace common\models;
use Yii;
use yii\base\Model;
use kartik\password\StrengthValidator;
class ChangePasswordForm extends Model{
public $oldpass;
public $newpass;
public $repeatnewpass;
public function rules(){
return [
[['oldpass','newpass','repeatnewpass'],'required'],
['oldpass','findPasswords'],
['repeatnewpass','compare','compareAttribute'=>'newpass'],
[['newpass'], StrengthValidator::className(), 'preset'=>'normal', 'userAttribute'=>'username']
];
}
public function findPasswords($attribute, $params){
$user = Gebruiker::find()->where([
'username'=>Yii::$app->user->identity->username
])->one();
$password = $user->password;
if (!$user->validatePassword($attribute))
$this->addError($attribute,'Huidig wachtwoord is onjuist');
}
public function attributeLabels(){
return [
'oldpass'=>'Huidig wachtwoord',
'newpass'=>'Nieuw wachtwoord',
'repeatnewpass'=>'Herhaal nieuw wachtwoord',
];
}
/* hier de functies om het huidige wachtwoord te controleren en de nieuwe vervolgens op te slaan */
public function checkSetPassword()
{
return true;
}
}
查看changePassword:
<?php
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\password\PasswordInput;
$this->title = 'Wijzig wachtwoord';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-changepassword">
<div class="col-sm-offset-0 col-sm-10">
<h2><?= Html::encode($this->title) ?></h2>
<p>Vul de volgende velden in om het wachtwoord te wijzigen :</p>
<?php $form = ActiveForm::begin([
'id'=>'changepassword-form',
'type' => ActiveForm::TYPE_VERTICAL,
'fieldConfig'=>[
'template'=>"{label}\n<div class=\"col-lg-3\">
{input}</div>\n<div class=\"col-lg-5\">
{error}</div>",
'labelOptions'=>['class'=>'col-lg-2 control-label'],
],
]); ?>
<div class="form-group">
<div class="col-sm-offset-0 col-sm-12">
<div class="row">
<?= $form->field($model,'oldpass',['inputOptions'=>[
'placeholder'=>'Huidig wachtwoord'
]])->passwordInput() ?>
</div>
<!--
< ?= $form->field($model,'newpass',['inputOptions'=>[
'placeholder'=>'New Password'
]])->passwordInput() ?>
-->
<p />
<div class="row">
<?= $form->field($model, 'newpass', ['inputOptions'=>[
'placeholder'=>'Nieuw wachtwoord'
]])->widget(PasswordInput::classname(), [
'pluginOptions' =>
[
'showMeter' => true,
'toggleMask' => false
]
])
?>
</div>
<p />
<div class="row">
<?= $form->field($model,'repeatnewpass',['inputOptions'=>[
'placeholder'=>'Herhaal nieuwe wachtwoord'
]])->passwordInput() ?>
</div>
</div>
</div>
<p />
<div class="form-group">
<div class="col-lg-offset-0 col-lg-12">
<?= Html::submitButton('Wijzig wachtwoord',[
'class'=>'btn btn-primary'
]) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>