UserModel::confirmEmail PHP Method

confirmEmail() public method

Users respond to confirmation emails by clicking a link that takes them here.
public confirmEmail ( array | object $User, string $EmailKey ) : boolean
$User array | object The user confirming their email.
$EmailKey string The token that was emailed to the user.
return boolean Returns **true** if the email was confirmed.
    public function confirmEmail($User, $EmailKey)
    {
        $Attributes = val('Attributes', $User);
        $StoredEmailKey = val('EmailKey', $Attributes);
        $UserID = val('UserID', $User);
        if (!$StoredEmailKey || $EmailKey != $StoredEmailKey) {
            $this->Validation->addValidationResult('EmailKey', '@' . t('Couldn\'t confirm email.', 'We couldn\'t confirm your email. Check the link in the email we sent you or try sending another confirmation email.'));
            return false;
        }
        $confirmRoleIDs = RoleModel::getDefaultRoles(RoleModel::TYPE_UNCONFIRMED);
        $defaultRoles = RoleModel::getDefaultRoles(RoleModel::TYPE_MEMBER);
        // Update the user's roles.
        $UserRoles = $this->getRoles($UserID);
        $UserRoleIDs = [];
        while ($UserRole = $UserRoles->nextRow(DATASET_TYPE_ARRAY)) {
            $UserRoleIDs[] = $UserRole['RoleID'];
        }
        // Sanitize result roles
        $Roles = array_diff($UserRoleIDs, $confirmRoleIDs);
        if (!sizeof($Roles)) {
            $Roles = $defaultRoles;
        }
        $this->EventArguments['ConfirmUserID'] = $UserID;
        $this->EventArguments['ConfirmUserRoles'] =& $Roles;
        $this->fireEvent('BeforeConfirmEmail');
        $this->saveRoles($UserID, $Roles, false);
        // Remove the email confirmation attributes.
        $this->saveAttribute($UserID, ['EmailKey' => null]);
        $this->setField($UserID, 'Confirmed', 1);
        return true;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Confirm email address is valid via sent code.
  *
  * @access public
  * @since 2.0.0
  *
  * @param int $UserID
  * @param string $EmailKey Authenticate with unique, 1-time code sent via email.
  */
 public function emailConfirm($UserID, $EmailKey = '')
 {
     $User = $this->UserModel->getID($UserID);
     if (!$User) {
         throw notFoundException('User');
     }
     $EmailConfirmed = $this->UserModel->confirmEmail($User, $EmailKey);
     $this->Form->setValidationResults($this->UserModel->validationResults());
     if ($EmailConfirmed) {
         $UserID = val('UserID', $User);
         Gdn::session()->start($UserID);
     }
     $this->setData('EmailConfirmed', $EmailConfirmed);
     $this->setData('Email', $User->Email);
     $this->render();
 }
UserModel