protected function loadPage()
 {
     $table = new PhabricatorAuthInvite();
     $conn_r = $table->establishConnection('r');
     $data = queryfx_all($conn_r, 'SELECT * FROM %T %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r));
     $invites = $table->loadAllFromArray($data);
     // If the objects were loaded via verification code, set a flag to make
     // sure the viewer can see them.
     if ($this->verificationCodes !== null) {
         foreach ($invites as $invite) {
             $invite->setViewerHasVerificationCode(true);
         }
     }
     return $invites;
 }
 private function handleLoggedInInvite(PhabricatorAuthInvite $invite, PhabricatorUser $viewer, PhabricatorUserEmail $email = null)
 {
     if ($email && $email->getUserPHID() !== $viewer->getPHID()) {
         $other_user = $this->loadUserForEmail($email);
         if ($email->getIsVerified()) {
             throw id(new PhabricatorAuthInviteAccountException(pht('Wrong Account'), pht('You are logged in as %s, but the email address you just ' . 'clicked a link from is already verified and associated ' . 'with another account (%s). Switch accounts, then try again.', phutil_tag('strong', array(), $viewer->getUsername()), phutil_tag('strong', array(), $other_user->getName()))))->setSubmitButtonText(pht('Log Out'))->setSubmitButtonURI($this->getLogoutURI())->setCancelButtonURI('/');
         } else {
             if ($email->getIsPrimary()) {
                 // NOTE: We never steal primary addresses from other accounts, even
                 // if they are unverified. This would leave the other account with
                 // no address. Users can use password recovery to access the other
                 // account if they really control the address.
                 throw id(new PhabricatorAuthInviteAccountException(pht('Wrong Acount'), pht('You are logged in as %s, but the email address you just ' . 'clicked a link from is already the primary email address ' . 'for another account (%s). Switch accounts, then try again.', phutil_tag('strong', array(), $viewer->getUsername()), phutil_tag('strong', array(), $other_user->getName()))))->setSubmitButtonText(pht('Log Out'))->setSubmitButtonURI($this->getLogoutURI())->setCancelButtonURI('/');
             } else {
                 if (!$this->shouldVerify()) {
                     throw id(new PhabricatorAuthInviteVerifyException(pht('Verify Email'), pht('You are logged in as %s, but the email address (%s) you just ' . 'clicked a link from is already associated with another ' . 'account (%s). You can log out to switch accounts, or verify ' . 'the address and attach it to your current account. Attach ' . 'email address %s to user account %s?', phutil_tag('strong', array(), $viewer->getUsername()), phutil_tag('strong', array(), $invite->getEmailAddress()), phutil_tag('strong', array(), $other_user->getName()), phutil_tag('strong', array(), $invite->getEmailAddress()), phutil_tag('strong', array(), $viewer->getUsername()))))->setSubmitButtonText(pht('Verify %s', $invite->getEmailAddress()))->setCancelButtonText(pht('Log Out'))->setCancelButtonURI($this->getLogoutURI());
                 }
             }
         }
     }
     if (!$email) {
         $email = id(new PhabricatorUserEmail())->setAddress($invite->getEmailAddress())->setIsVerified(0)->setIsPrimary(0);
     }
     if (!$email->getIsVerified()) {
         // We're doing this check here so that we can verify the address if
         // it's already attached to the viewer's account, just not verified.
         if (!$this->shouldVerify()) {
             throw id(new PhabricatorAuthInviteVerifyException(pht('Verify Email'), pht('Verify this email address (%s) and attach it to your ' . 'account (%s)?', phutil_tag('strong', array(), $invite->getEmailAddress()), phutil_tag('strong', array(), $viewer->getUsername()))))->setSubmitButtonText(pht('Verify %s', $invite->getEmailAddress()))->setCancelButtonURI('/');
         }
         $editor = id(new PhabricatorUserEditor())->setActor($viewer);
         // If this is a new email, add it to the user's account.
         if (!$email->getUserPHID()) {
             $editor->addEmail($viewer, $email);
         }
         // If another user added this email (but has not verified it),
         // take it from them.
         $editor->reassignEmail($viewer, $email);
         $editor->verifyEmail($viewer, $email);
     }
     $invite->setAcceptedByPHID($viewer->getPHID());
     $invite->save();
     // If we make it here, the user was already logged in with the email
     // address attached to their account and verified, or we attached it to
     // their account (if it was not already attached) and verified it.
     throw new PhabricatorAuthInviteRegisteredException();
 }
 protected function renderInviteHeader(PhabricatorAuthInvite $invite)
 {
     $viewer = $this->getViewer();
     $invite_author = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withPHIDs(array($invite->getAuthorPHID()))->needProfileImage(true)->executeOne();
     // If we can't load the author for some reason, just drop this message.
     // We lose the value of contextualizing things without author details.
     if (!$invite_author) {
         return null;
     }
     $invite_item = id(new PHUIObjectItemView())->setHeader(pht('Welcome to Phabricator!'))->setImageURI($invite_author->getProfileImageURI())->addAttribute(pht('%s has invited you to join Phabricator.', $invite_author->getFullName()));
     $invite_list = id(new PHUIObjectItemListView())->addItem($invite_item)->setFlush(true);
     return id(new PHUIBoxView())->addMargin(PHUI::MARGIN_LARGE)->appendChild($invite_list);
 }