UserModel::passwordRequest PHP 메소드

passwordRequest() 공개 메소드

Send forgot password email.
public passwordRequest ( string $Email ) : boolean
$Email string
리턴 boolean
    public function passwordRequest($Email)
    {
        if (!$Email) {
            return false;
        }
        $Users = $this->getWhere(['Email' => $Email])->resultObject();
        if (count($Users) == 0) {
            // Check for the username.
            $Users = $this->getWhere(['Name' => $Email])->resultObject();
        }
        $this->EventArguments['Users'] =& $Users;
        $this->EventArguments['Email'] = $Email;
        $this->fireEvent('BeforePasswordRequest');
        if (count($Users) == 0) {
            $this->Validation->addValidationResult('Name', "Couldn't find an account associated with that email/username.");
            return false;
        }
        $NoEmail = true;
        foreach ($Users as $User) {
            if (!$User->Email) {
                continue;
            }
            $Email = new Gdn_Email();
            // Instantiate in loop to clear previous settings
            $PasswordResetKey = betterRandomString(20, 'Aa0');
            $PasswordResetExpires = strtotime('+1 hour');
            $this->saveAttribute($User->UserID, 'PasswordResetKey', $PasswordResetKey);
            $this->saveAttribute($User->UserID, 'PasswordResetExpires', $PasswordResetExpires);
            $AppTitle = c('Garden.Title');
            $Email->subject('[' . $AppTitle . '] ' . t('Reset Your Password'));
            $Email->to($User->Email);
            $emailTemplate = $Email->getEmailTemplate()->setTitle(t('Reset Your Password'))->setMessage(sprintf(t('We\'ve received a request to change your password.'), $AppTitle))->setButton(externalUrl('/entry/passwordreset/' . $User->UserID . '/' . $PasswordResetKey), t('Change My Password'));
            $Email->setEmailTemplate($emailTemplate);
            try {
                $Email->send();
            } catch (Exception $e) {
                if (debug()) {
                    throw $e;
                }
            }
            $NoEmail = false;
        }
        if ($NoEmail) {
            $this->Validation->addValidationResult('Name', 'There is no email address associated with that account.');
            return false;
        }
        return true;
    }

Usage Example

예제 #1
0
 /**
  * Request password reset.
  *
  * @access public
  * @since 2.0.0
  */
 public function passwordRequest()
 {
     Gdn::locale()->setTranslation('Email', t(UserModel::signinLabelCode()));
     if ($this->Form->isPostBack() === true) {
         $this->Form->validateRule('Email', 'ValidateRequired');
         if ($this->Form->errorCount() == 0) {
             try {
                 $Email = $this->Form->getFormValue('Email');
                 if (!$this->UserModel->passwordRequest($Email)) {
                     $this->Form->setValidationResults($this->UserModel->validationResults());
                     Logger::event('password_reset_failure', Logger::INFO, 'Can\'t find account associated with email/username {Input}.', array('Input' => $Email));
                 }
             } catch (Exception $ex) {
                 $this->Form->addError($ex->getMessage());
             }
             if ($this->Form->errorCount() == 0) {
                 $this->Form->addError('Success!');
                 $this->View = 'passwordrequestsent';
                 Logger::event('password_reset_request', Logger::INFO, '{Input} has been sent a password reset email.', array('Input' => $Email));
             }
         } else {
             if ($this->Form->errorCount() == 0) {
                 $this->Form->addError("Couldn't find an account associated with that email/username.");
                 Logger::event('password_reset_failure', Logger::INFO, 'Can\'t find account associated with email/username {Input}.', array('Input' => $this->Form->getValue('Email')));
             }
         }
     }
     $this->render();
 }
UserModel