PasswordResetModel::requestPasswordReset PHP 메소드

requestPasswordReset() 공개 정적인 메소드

Perform the necessary actions to send a password reset mail
public static requestPasswordReset ( $user_name_or_email, $captcha ) : boolean
$user_name_or_email string Username or user's email
$captcha string Captcha string
리턴 boolean success status
    public static function requestPasswordReset($user_name_or_email, $captcha)
    {
        if (!CaptchaModel::checkCaptcha($captcha)) {
            Session::add('feedback_negative', Text::get('FEEDBACK_CAPTCHA_WRONG'));
            return false;
        }
        if (empty($user_name_or_email)) {
            Session::add('feedback_negative', Text::get('FEEDBACK_USERNAME_EMAIL_FIELD_EMPTY'));
            return false;
        }
        // check if that username exists
        $result = UserModel::getUserDataByUserNameOrEmail($user_name_or_email);
        if (!$result) {
            Session::add('feedback_negative', Text::get('FEEDBACK_USER_DOES_NOT_EXIST'));
            return false;
        }
        // generate integer-timestamp (to see when exactly the user (or an attacker) requested the password reset mail)
        // generate random hash for email password reset verification (40 char string)
        $temporary_timestamp = time();
        $user_password_reset_hash = sha1(uniqid(mt_rand(), true));
        // set token (= a random hash string and a timestamp) into database ...
        $token_set = self::setPasswordResetDatabaseToken($result->user_name, $user_password_reset_hash, $temporary_timestamp);
        if (!$token_set) {
            return false;
        }
        // ... and send a mail to the user, containing a link with username and token hash string
        $mail_sent = self::sendPasswordResetMail($result->user_name, $user_password_reset_hash, $result->user_email);
        if ($mail_sent) {
            return true;
        }
        // default return
        return false;
    }

Usage Example

예제 #1
0
 /**
  * The request-password-reset action
  * POST-request after form submit
  */
 public function requestPasswordReset_action()
 {
     PasswordResetModel::requestPasswordReset(Request::post('user_name_or_email'), Request::post('captcha'));
     Redirect::to('login/index');
 }