dpzff20644 2015-12-22 04:44
浏览 50
已采纳

将重置密码移动到后端,Yii2

I have user authorization on backend only (for admins) and I am trying to move password reset function to backend.

\backend\views\site\login.php

<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>

            //fields for username and password

            <div class="form-group">
                <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
            </div>

        <?php ActiveForm::end(); ?>

        <div style="color:#999;margin:1em 0">
               If you forgot your password you can <?= Html::a('reset it', ['request-password-reset']) ?>.
           </div>

\backend\controllers\SiteController.php

public function actionLogin()
{
    if (!\Yii::$app->user->isGuest) {
        return $this->goHome();
    }

    $model = new LoginForm();
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
        return $this->goBack();
    } else {
       //enters here instead
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}
public function actionRequestPasswordReset()
    {
       //not entering here
        $model = new PasswordResetRequestForm(); //placed in \common\models
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendEmail()) {
                Yii::$app->session->setFlash('success', 'Check your email for further instructions.');

                return $this->goHome();
            } else {
                Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
            }
        }

        return $this->render('requestPasswordResetToken', [
            'model' => $model,
        ]);
    }

The problem is when I click reset it the site redirects me to login.php again, so actionRequestPasswordReset() is not fired. Im new to Yii and would appreciate any help.

  • 写回答

1条回答 默认 最新

  • duai3681 2015-12-22 07:01
    关注

    Update your controller's Access Control Filter to permit users who haven't logged in to access requestPasswordReset:

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['request-password-reset'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题