【问题标题】:How to return to $user->save() on success成功后如何返回 $user->save()
【发布时间】:2021-04-19 08:13:13
【问题描述】:

我正在研究我的 Yii 1.1 应用程序中的密码强度。我正在使用 haveibeenpwned API。

如果密码被接受,我该怎么做才能再次运行例程?

如果密码接受,我该怎么做才能返回$user->save()?

这是我的帮助文件(PasswordHelper.php):

public static function passwordCheck($password) 
{

if ($password) {
    // sha1 hash of new password
    $hash = sha1($password, false);
    // character 0-4 of new password
    $prefix = strtoupper(substr($hash, 0, 5));
    // character 5-39 of new password
    $suffix = strtoupper(substr($hash, 5, 35));
    // API url
    $url = "https://api.pwnedpasswords.com/range/" . $prefix;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($ch, CURLOPT_TIMEOUT, 500);

    $result = curl_exec($ch);
    // Change the result from string to array, split it by every line
    $result = explode("\n", $result);

    foreach ($result as $r) {
       $r = explode(":", $r);
       if ($r[0] == $suffix) {
           return Yii::app()->systemMsg->raiseError(Yii::t('validators', 'NOT_APPROVED_PASSWORD'));
       }
   }
   curl_close($ch);
   }
  }
}

这是控制器文件的一部分:

if ($user->newPassword) {
        $password = $user->newPassword;
        PasswordHelper::passwordCheck($password);
    }

    if ( $user->save() ) {
       Yii::app()->systemMsg->raiseSuccess( Yii::t( 'validators', 'SAVE_SUCCESS' ) );
       $redirectUrl = Yii::app()->input->get( 'return', $this->module->returnUrl );
       $this->redirect( $redirectUrl );
    }
    else {
      Yii::app()->systemMsg->raiseError( Yii::t( 'validators', 'SAVE_ERROR' ) );
      $this->setModel( $user );
      $this->actionEdit( $user->id );
    }

【问题讨论】:

    标签: php model-view-controller yii passwords helper


    【解决方案1】:

    而不是在帮助类上创建 passwordCheck 函数。请务必在模型上创建此函数并编写访问规则,以便在执行 $user->validate()

    时完成验证

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多