yii 如何在成功时返回到$user->保存()

72qzrwbm  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(119)

我正在我的Yii 1.1应用程序中增强密码强度。我使用的是haveibeenpowned API。
如果密码被接受,我该怎么做才能再次运行该例程?
如果密码接受,我该怎么做才能返回到$user-〉保存()?
这是我的帮助文件(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 );
    }
axkjgtzd

axkjgtzd1#

而不是在helper类上创建passwordCheck函数。请在Model上创建此函数并写入accessrule,以便在执行$user-〉validate()时可以完成验证

相关问题