public final function execute()
 {
     if (!$this->viewer) {
         throw new Exception("Call setViewer() before execute()!");
     }
     $results = array();
     $filter = new PhabricatorPolicyFilter();
     $filter->setViewer($this->viewer);
     $filter->setCapability(PhabricatorPolicyCapability::CAN_VIEW);
     $filter->raisePolicyExceptions($this->raisePolicyExceptions);
     do {
         $page = $this->loadPage();
         $visible = $filter->apply($page);
         foreach ($visible as $key => $result) {
             $results[$key] = $result;
             if ($this->getLimit() && count($results) >= $this->getLimit()) {
                 break 2;
             }
         }
         if (!$this->getLimit() || count($page) < $this->getLimit()) {
             break;
         }
         $this->nextPage($page);
     } while (true);
     return $results;
 }
 public static function requireCapability(PhabricatorUser $user, PhabricatorPolicyInterface $object, $capability)
 {
     $filter = new PhabricatorPolicyFilter();
     $filter->setViewer($user);
     $filter->requireCapabilities(array($capability));
     $filter->raisePolicyExceptions(true);
     $filter->apply(array($object));
 }
 private function getPolicyFilter()
 {
     $filter = new PhabricatorPolicyFilter();
     $filter->setViewer($this->viewer);
     $capabilities = $this->getRequiredCapabilities();
     $filter->requireCapabilities($capabilities);
     $filter->raisePolicyExceptions($this->shouldRaisePolicyExceptions());
     return $filter;
 }
 /**
  * Execute the query, loading all visible results.
  *
  * @return list<PhabricatorPolicyInterface> Result objects.
  * @task exec
  */
 public final function execute()
 {
     if (!$this->viewer) {
         throw new Exception("Call setViewer() before execute()!");
     }
     $results = array();
     $filter = new PhabricatorPolicyFilter();
     $filter->setViewer($this->viewer);
     if (!$this->capabilities) {
         $capabilities = array(PhabricatorPolicyCapability::CAN_VIEW);
     } else {
         $capabilities = $this->capabilities;
     }
     $filter->requireCapabilities($capabilities);
     $filter->raisePolicyExceptions($this->raisePolicyExceptions);
     $offset = (int) $this->getOffset();
     $limit = (int) $this->getLimit();
     $count = 0;
     if ($limit) {
         $need = $offset + $limit;
     } else {
         $need = 0;
     }
     $this->willExecute();
     do {
         if ($need) {
             $this->rawResultLimit = min($need - $count, 1024);
         } else {
             $this->rawResultLimit = 0;
         }
         $page = $this->loadPage();
         $visible = $filter->apply($page);
         foreach ($visible as $key => $result) {
             ++$count;
             // If we have an offset, we just ignore that many results and start
             // storing them only once we've hit the offset. This reduces memory
             // requirements for large offsets, compared to storing them all and
             // slicing them away later.
             if ($count > $offset) {
                 $results[$key] = $result;
             }
             if ($need && $count >= $need) {
                 // If we have all the rows we need, break out of the paging query.
                 break 2;
             }
         }
         if (!$this->rawResultLimit) {
             // If we don't have a load count, we loaded all the results. We do
             // not need to load another page.
             break;
         }
         if (count($page) < $this->rawResultLimit) {
             // If we have a load count but the unfiltered results contained fewer
             // objects, we know this was the last page of objects; we do not need
             // to load another page because we can deduce it would be empty.
             break;
         }
         $this->nextPage($page);
     } while (true);
     return $results;
 }