[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/auth/query/ -> PhabricatorExternalAccountQuery.php (source)

   1  <?php
   2  
   3  /**
   4   * NOTE: When loading ExternalAccounts for use in an authentication context
   5   * (that is, you're going to act as the account or link identities or anything
   6   * like that) you should require CAN_EDIT capability even if you aren't actually
   7   * editing the ExternalAccount.
   8   *
   9   * ExternalAccounts have a permissive CAN_VIEW policy (like users) because they
  10   * interact directly with objects and can leave comments, sign documents, etc.
  11   * However, CAN_EDIT is restricted to users who own the accounts.
  12   */
  13  final class PhabricatorExternalAccountQuery
  14    extends PhabricatorCursorPagedPolicyAwareQuery {
  15  
  16    private $ids;
  17    private $phids;
  18    private $accountTypes;
  19    private $accountDomains;
  20    private $accountIDs;
  21    private $userPHIDs;
  22    private $needImages;
  23    private $accountSecrets;
  24  
  25    public function withUserPHIDs(array $user_phids) {
  26      $this->userPHIDs = $user_phids;
  27      return $this;
  28    }
  29  
  30    public function withAccountIDs(array $account_ids) {
  31      $this->accountIDs = $account_ids;
  32      return $this;
  33    }
  34  
  35    public function withAccountDomains(array $account_domains) {
  36      $this->accountDomains = $account_domains;
  37      return $this;
  38    }
  39  
  40    public function withAccountTypes(array $account_types) {
  41      $this->accountTypes = $account_types;
  42      return $this;
  43    }
  44  
  45    public function withPHIDs(array $phids) {
  46      $this->phids = $phids;
  47      return $this;
  48    }
  49  
  50    public function withIDs($ids) {
  51      $this->ids = $ids;
  52      return $this;
  53    }
  54  
  55    public function withAccountSecrets(array $secrets) {
  56      $this->accountSecrets = $secrets;
  57      return $this;
  58    }
  59  
  60    public function needImages($need) {
  61      $this->needImages = $need;
  62      return $this;
  63    }
  64  
  65    public function loadPage() {
  66      $table = new PhabricatorExternalAccount();
  67      $conn_r = $table->establishConnection('r');
  68  
  69      $data = queryfx_all(
  70        $conn_r,
  71        'SELECT * FROM %T %Q %Q %Q',
  72        $table->getTableName(),
  73        $this->buildWhereClause($conn_r),
  74        $this->buildOrderClause($conn_r),
  75        $this->buildLimitClause($conn_r));
  76  
  77      return $table->loadAllFromArray($data);
  78    }
  79  
  80    public function willFilterPage(array $accounts) {
  81      if ($this->needImages) {
  82        $file_phids = mpull($accounts, 'getProfileImagePHID');
  83        $file_phids = array_filter($file_phids);
  84  
  85        if ($file_phids) {
  86          // NOTE: We use the omnipotent viewer here because these files are
  87          // usually created during registration and can't be associated with
  88          // the correct policies, since the relevant user account does not exist
  89          // yet. In effect, if you can see an ExternalAccount, you can see its
  90          // profile image.
  91          $files = id(new PhabricatorFileQuery())
  92            ->setViewer(PhabricatorUser::getOmnipotentUser())
  93            ->withPHIDs($file_phids)
  94            ->execute();
  95          $files = mpull($files, null, 'getPHID');
  96        } else {
  97          $files = array();
  98        }
  99  
 100        $default_file = null;
 101        foreach ($accounts as $account) {
 102          $image_phid = $account->getProfileImagePHID();
 103          if ($image_phid && isset($files[$image_phid])) {
 104            $account->attachProfileImageFile($files[$image_phid]);
 105          } else {
 106            if ($default_file === null) {
 107              $default_file = PhabricatorFile::loadBuiltin(
 108                $this->getViewer(),
 109                'profile.png');
 110            }
 111            $account->attachProfileImageFile($default_file);
 112          }
 113        }
 114      }
 115  
 116      return $accounts;
 117    }
 118  
 119    protected function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 120      $where = array();
 121  
 122      $where[] = $this->buildPagingClause($conn_r);
 123  
 124      if ($this->ids) {
 125        $where[] = qsprintf(
 126          $conn_r,
 127          'id IN (%Ld)',
 128          $this->ids);
 129      }
 130  
 131      if ($this->phids) {
 132        $where[] = qsprintf(
 133          $conn_r,
 134          'phid IN (%Ls)',
 135          $this->phids);
 136      }
 137  
 138      if ($this->accountTypes) {
 139        $where[] = qsprintf(
 140          $conn_r,
 141          'accountType IN (%Ls)',
 142          $this->accountTypes);
 143      }
 144  
 145      if ($this->accountDomains) {
 146        $where[] = qsprintf(
 147          $conn_r,
 148          'accountDomain IN (%Ls)',
 149          $this->accountDomains);
 150      }
 151  
 152      if ($this->accountIDs) {
 153        $where[] = qsprintf(
 154          $conn_r,
 155          'accountID IN (%Ls)',
 156          $this->accountIDs);
 157      }
 158  
 159      if ($this->userPHIDs) {
 160        $where[] = qsprintf(
 161          $conn_r,
 162          'userPHID IN (%Ls)',
 163          $this->userPHIDs);
 164      }
 165  
 166      if ($this->accountSecrets) {
 167        $where[] = qsprintf(
 168          $conn_r,
 169          'accountSecret IN (%Ls)',
 170          $this->accountSecrets);
 171      }
 172  
 173      return $this->formatWhereClause($where);
 174    }
 175  
 176    public function getQueryApplicationClass() {
 177      return 'PhabricatorPeopleApplication';
 178    }
 179  
 180    /**
 181     * Attempts to find an external account and if none exists creates a new
 182     * external account with a shiny new ID and PHID.
 183     *
 184     * NOTE: This function assumes the first item in various query parameters is
 185     * the correct value to use in creating a new external account.
 186     */
 187    public function loadOneOrCreate() {
 188      $account = $this->executeOne();
 189      if (!$account) {
 190        $account = new PhabricatorExternalAccount();
 191        if ($this->accountIDs) {
 192          $account->setAccountID(reset($this->accountIDs));
 193        }
 194        if ($this->accountTypes) {
 195          $account->setAccountType(reset($this->accountTypes));
 196        }
 197        if ($this->accountDomains) {
 198          $account->setAccountDomain(reset($this->accountDomains));
 199        }
 200        if ($this->accountSecrets) {
 201          $account->setAccountSecret(reset($this->accountSecrets));
 202        }
 203        if ($this->userPHIDs) {
 204          $account->setUserPHID(reset($this->userPHIDs));
 205        }
 206        $account->save();
 207      }
 208      return $account;
 209    }
 210  
 211  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1