[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/policy/query/ -> PhabricatorPolicyQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorPolicyQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $object;
   7    private $phids;
   8  
   9    public function setObject(PhabricatorPolicyInterface $object) {
  10      $this->object = $object;
  11      return $this;
  12    }
  13  
  14    public function withPHIDs(array $phids) {
  15      $this->phids = $phids;
  16      return $this;
  17    }
  18  
  19    public static function loadPolicies(
  20      PhabricatorUser $viewer,
  21      PhabricatorPolicyInterface $object) {
  22  
  23      $results = array();
  24  
  25      $map = array();
  26      foreach ($object->getCapabilities() as $capability) {
  27        $map[$capability] = $object->getPolicy($capability);
  28      }
  29  
  30      $policies = id(new PhabricatorPolicyQuery())
  31        ->setViewer($viewer)
  32        ->withPHIDs($map)
  33        ->execute();
  34  
  35      foreach ($map as $capability => $phid) {
  36        $results[$capability] = $policies[$phid];
  37      }
  38  
  39      return $results;
  40    }
  41  
  42    public static function renderPolicyDescriptions(
  43      PhabricatorUser $viewer,
  44      PhabricatorPolicyInterface $object,
  45      $icon = false) {
  46  
  47      $policies = self::loadPolicies($viewer, $object);
  48  
  49      foreach ($policies as $capability => $policy) {
  50        $policies[$capability] = $policy->renderDescription($icon);
  51      }
  52  
  53      return $policies;
  54    }
  55  
  56    public function loadPage() {
  57      if ($this->object && $this->phids) {
  58        throw new Exception(
  59          'You can not issue a policy query with both setObject() and '.
  60          'setPHIDs().');
  61      } else if ($this->object) {
  62        $phids = $this->loadObjectPolicyPHIDs();
  63      } else {
  64        $phids = $this->phids;
  65      }
  66  
  67      $phids = array_fuse($phids);
  68  
  69      $results = array();
  70  
  71      // First, load global policies.
  72      foreach ($this->getGlobalPolicies() as $phid => $policy) {
  73        if (isset($phids[$phid])) {
  74          $results[$phid] = $policy;
  75          unset($phids[$phid]);
  76        }
  77      }
  78  
  79      // If we still need policies, we're going to have to fetch data. Bucket
  80      // the remaining policies into rule-based policies and handle-based
  81      // policies.
  82      if ($phids) {
  83        $rule_policies = array();
  84        $handle_policies = array();
  85        foreach ($phids as $phid) {
  86          $phid_type = phid_get_type($phid);
  87          if ($phid_type == PhabricatorPolicyPHIDTypePolicy::TYPECONST) {
  88            $rule_policies[$phid] = $phid;
  89          } else {
  90            $handle_policies[$phid] = $phid;
  91          }
  92        }
  93  
  94        if ($handle_policies) {
  95          $handles = id(new PhabricatorHandleQuery())
  96            ->setViewer($this->getViewer())
  97            ->withPHIDs($handle_policies)
  98            ->execute();
  99          foreach ($handle_policies as $phid) {
 100            $results[$phid] = PhabricatorPolicy::newFromPolicyAndHandle(
 101              $phid,
 102              $handles[$phid]);
 103          }
 104        }
 105  
 106        if ($rule_policies) {
 107          $rules = id(new PhabricatorPolicy())->loadAllWhere(
 108            'phid IN (%Ls)',
 109            $rule_policies);
 110          $results += mpull($rules, null, 'getPHID');
 111        }
 112      }
 113  
 114      $results = msort($results, 'getSortKey');
 115  
 116      return $results;
 117    }
 118  
 119    public static function isGlobalPolicy($policy) {
 120      $globalPolicies = self::getGlobalPolicies();
 121  
 122      if (isset($globalPolicies[$policy])) {
 123        return true;
 124      }
 125  
 126      return false;
 127    }
 128  
 129    public static function getGlobalPolicy($policy) {
 130      if (!self::isGlobalPolicy($policy)) {
 131        throw new Exception("Policy '{$policy}' is not a global policy!");
 132      }
 133      return idx(self::getGlobalPolicies(), $policy);
 134    }
 135  
 136    private static function getGlobalPolicies() {
 137      static $constants = array(
 138        PhabricatorPolicies::POLICY_PUBLIC,
 139        PhabricatorPolicies::POLICY_USER,
 140        PhabricatorPolicies::POLICY_ADMIN,
 141        PhabricatorPolicies::POLICY_NOONE,
 142      );
 143  
 144      $results = array();
 145      foreach ($constants as $constant) {
 146        $results[$constant] = id(new PhabricatorPolicy())
 147          ->setType(PhabricatorPolicyType::TYPE_GLOBAL)
 148          ->setPHID($constant)
 149          ->setName(self::getGlobalPolicyName($constant))
 150          ->setShortName(self::getGlobalPolicyShortName($constant))
 151          ->makeEphemeral();
 152      }
 153  
 154      return $results;
 155    }
 156  
 157    private static function getGlobalPolicyName($policy) {
 158      switch ($policy) {
 159        case PhabricatorPolicies::POLICY_PUBLIC:
 160          return pht('Public (No Login Required)');
 161        case PhabricatorPolicies::POLICY_USER:
 162          return pht('All Users');
 163        case PhabricatorPolicies::POLICY_ADMIN:
 164          return pht('Administrators');
 165        case PhabricatorPolicies::POLICY_NOONE:
 166          return pht('No One');
 167        default:
 168          return pht('Unknown Policy');
 169      }
 170    }
 171  
 172    private static function getGlobalPolicyShortName($policy) {
 173      switch ($policy) {
 174        case PhabricatorPolicies::POLICY_PUBLIC:
 175          return pht('Public');
 176        default:
 177          return null;
 178      }
 179    }
 180  
 181    private function loadObjectPolicyPHIDs() {
 182      $phids = array();
 183      $viewer = $this->getViewer();
 184  
 185      if ($viewer->getPHID()) {
 186        $projects = id(new PhabricatorProjectQuery())
 187          ->setViewer($viewer)
 188          ->withMemberPHIDs(array($viewer->getPHID()))
 189          ->execute();
 190        foreach ($projects as $project) {
 191          $phids[] = $project->getPHID();
 192        }
 193  
 194        // Include the "current viewer" policy. This improves consistency, but
 195        // is also useful for creating private instances of normally-shared object
 196        // types, like repositories.
 197        $phids[] = $viewer->getPHID();
 198      }
 199  
 200      $capabilities = $this->object->getCapabilities();
 201      foreach ($capabilities as $capability) {
 202        $policy = $this->object->getPolicy($capability);
 203        if (!$policy) {
 204          continue;
 205        }
 206        $phids[] = $policy;
 207      }
 208  
 209      // If this install doesn't have "Public" enabled, don't include it as an
 210      // option unless the object already has a "Public" policy. In this case we
 211      // retain the policy but enforce it as though it was "All Users".
 212      $show_public = PhabricatorEnv::getEnvConfig('policy.allow-public');
 213      foreach ($this->getGlobalPolicies() as $phid => $policy) {
 214        if ($phid == PhabricatorPolicies::POLICY_PUBLIC) {
 215          if (!$show_public) {
 216            continue;
 217          }
 218        }
 219        $phids[] = $phid;
 220      }
 221  
 222      return $phids;
 223    }
 224  
 225    protected function shouldDisablePolicyFiltering() {
 226      // Policy filtering of policies is currently perilous and not required by
 227      // the application.
 228      return true;
 229    }
 230  
 231    public function getQueryApplicationClass() {
 232      return 'PhabricatorPolicyApplication';
 233    }
 234  
 235  }


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