~ubuntu-branches/ubuntu/wily/phabricator/wily-proposed

« back to all changes in this revision

Viewing changes to phabricator/src/infrastructure/query/policy/PhabricatorPolicyAwareQuery.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-08-03 23:28:35 UTC
  • mfrom: (0.39.1) (0.38.1) (0.27.3) (2.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20150803232835-qnomusa964oxnywb
Tags: 0~git20150803-1
* New snapshot release (closes: #789760)
* renamed arcanist bash-completion file (closes: #791632)
* depends on same version for libphutil (closes: #794462)
* added php5-mysqlnd alternative depends (closes: #792136)

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
  private $workspace = array();
36
36
  private $inFlightPHIDs = array();
37
37
  private $policyFilteredPHIDs = array();
38
 
  private $canUseApplication;
39
38
 
40
39
  /**
41
40
   * Should we continue or throw an exception when a query result is filtered
257
256
      }
258
257
 
259
258
      if ($visible) {
260
 
        $this->putObjectsInWorkspace($this->getWorkspaceMapForPage($visible));
261
259
        $visible = $this->didFilterPage($visible);
262
260
      }
263
261
 
338
336
  }
339
337
 
340
338
  protected function didRejectResult(PhabricatorPolicyInterface $object) {
 
339
    // Some objects (like commits) may be rejected because related objects
 
340
    // (like repositories) can not be loaded. In some cases, we may need these
 
341
    // related objects to determine the object policy, so it's expected that
 
342
    // we may occasionally be unable to determine the policy.
 
343
 
 
344
    try {
 
345
      $policy = $object->getPolicy(PhabricatorPolicyCapability::CAN_VIEW);
 
346
    } catch (Exception $ex) {
 
347
      $policy = null;
 
348
    }
 
349
 
 
350
    // Mark this object as filtered so handles can render "Restricted" instead
 
351
    // of "Unknown".
 
352
    $phid = $object->getPHID();
 
353
    $this->addPolicyFilteredPHIDs(array($phid => $phid));
 
354
 
341
355
    $this->getPolicyFilter()->rejectObject(
342
356
      $object,
343
 
      $object->getPolicy(PhabricatorPolicyCapability::CAN_VIEW),
 
357
      $policy,
344
358
      PhabricatorPolicyCapability::CAN_VIEW);
345
359
  }
346
360
 
391
405
   *
392
406
   * **Fully enrich objects pulled from the workspace.** After pulling objects
393
407
   * from the workspace, you still need to load and attach any additional
394
 
   * content the query requests. Otherwise, a query might return objects without
395
 
   * requested content.
 
408
   * content the query requests. Otherwise, a query might return objects
 
409
   * without requested content.
396
410
   *
397
411
   * Generally, you do not need to update the workspace yourself: it is
398
412
   * automatically populated as a side effect of objects surviving policy
404
418
   * @task workspace
405
419
   */
406
420
  public function putObjectsInWorkspace(array $objects) {
 
421
    $parent = $this->getParentQuery();
 
422
    if ($parent) {
 
423
      $parent->putObjectsInWorkspace($objects);
 
424
      return $this;
 
425
    }
 
426
 
407
427
    assert_instances_of($objects, 'PhabricatorPolicyInterface');
408
428
 
409
 
    $viewer_phid = $this->getViewer()->getPHID();
 
429
    $viewer_fragment = $this->getViewer()->getCacheFragment();
410
430
 
411
431
    // The workspace is scoped per viewer to prevent accidental contamination.
412
 
    if (empty($this->workspace[$viewer_phid])) {
413
 
      $this->workspace[$viewer_phid] = array();
 
432
    if (empty($this->workspace[$viewer_fragment])) {
 
433
      $this->workspace[$viewer_fragment] = array();
414
434
    }
415
435
 
416
 
    $this->workspace[$viewer_phid] += $objects;
 
436
    $this->workspace[$viewer_fragment] += $objects;
417
437
 
418
438
    return $this;
419
439
  }
430
450
   * @task workspace
431
451
   */
432
452
  public function getObjectsFromWorkspace(array $phids) {
433
 
    $viewer_phid = $this->getViewer()->getPHID();
 
453
    $parent = $this->getParentQuery();
 
454
    if ($parent) {
 
455
      return $parent->getObjectsFromWorkspace($phids);
 
456
    }
 
457
 
 
458
    $viewer_fragment = $this->getViewer()->getCacheFragment();
434
459
 
435
460
    $results = array();
436
461
    foreach ($phids as $key => $phid) {
437
 
      if (isset($this->workspace[$viewer_phid][$phid])) {
438
 
        $results[$phid] = $this->workspace[$viewer_phid][$phid];
 
462
      if (isset($this->workspace[$viewer_fragment][$phid])) {
 
463
        $results[$phid] = $this->workspace[$viewer_fragment][$phid];
439
464
        unset($phids[$key]);
440
465
      }
441
466
    }
442
467
 
443
 
    if ($phids && $this->getParentQuery()) {
444
 
      $results += $this->getParentQuery()->getObjectsFromWorkspace($phids);
445
 
    }
446
 
 
447
468
    return $results;
448
469
  }
449
470
 
450
471
 
451
472
  /**
452
 
   * Convert a result page to a `<phid, PhabricatorPolicyInterface>` map.
453
 
   *
454
 
   * @param list<PhabricatorPolicyInterface> Objects.
455
 
   * @return map<phid, PhabricatorPolicyInterface> Map of objects which can
456
 
   *   be put into the workspace.
457
 
   * @task workspace
458
 
   */
459
 
  protected function getWorkspaceMapForPage(array $results) {
460
 
    $map = array();
461
 
    foreach ($results as $result) {
462
 
      $phid = $result->getPHID();
463
 
      if ($phid !== null) {
464
 
        $map[$phid] = $result;
465
 
      }
466
 
    }
467
 
 
468
 
    return $map;
469
 
  }
470
 
 
471
 
 
472
 
  /**
473
473
   * Mark PHIDs as in flight.
474
474
   *
475
475
   * PHIDs which are "in flight" are actively being queried for. Using this
663
663
   *   execute the query.
664
664
   */
665
665
  public function canViewerUseQueryApplication() {
666
 
    if ($this->canUseApplication === null) {
667
 
      $class = $this->getQueryApplicationClass();
668
 
      if (!$class) {
669
 
        $this->canUseApplication = true;
670
 
      } else {
671
 
        $result = id(new PhabricatorApplicationQuery())
672
 
          ->setViewer($this->getViewer())
673
 
          ->withClasses(array($class))
674
 
          ->execute();
675
 
 
676
 
        $this->canUseApplication = (bool)$result;
677
 
      }
 
666
    $class = $this->getQueryApplicationClass();
 
667
    if (!$class) {
 
668
      return true;
678
669
    }
679
670
 
680
 
    return $this->canUseApplication;
 
671
    $viewer = $this->getViewer();
 
672
    return PhabricatorApplication::isClassInstalledForViewer($class, $viewer);
681
673
  }
682
674
 
683
675
}