Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / drydock / query / DrydockAuthorizationQuery.php
blobd6436fc93a378765ec94d28329f5b1bca9930c09
1 <?php
3 final class DrydockAuthorizationQuery extends DrydockQuery {
5 private $ids;
6 private $phids;
7 private $blueprintPHIDs;
8 private $objectPHIDs;
9 private $blueprintStates;
10 private $objectStates;
12 public static function isFullyAuthorized(
13 $object_phid,
14 array $blueprint_phids) {
16 if (!$blueprint_phids) {
17 return true;
20 $authorizations = id(new self())
21 ->setViewer(PhabricatorUser::getOmnipotentUser())
22 ->withObjectPHIDs(array($object_phid))
23 ->withBlueprintPHIDs($blueprint_phids)
24 ->execute();
25 $authorizations = mpull($authorizations, null, 'getBlueprintPHID');
27 foreach ($blueprint_phids as $phid) {
28 $authorization = idx($authorizations, $phid);
29 if (!$authorization) {
30 return false;
33 if (!$authorization->isAuthorized()) {
34 return false;
38 return true;
41 public function withIDs(array $ids) {
42 $this->ids = $ids;
43 return $this;
46 public function withPHIDs(array $phids) {
47 $this->phids = $phids;
48 return $this;
51 public function withBlueprintPHIDs(array $phids) {
52 $this->blueprintPHIDs = $phids;
53 return $this;
56 public function withObjectPHIDs(array $phids) {
57 $this->objectPHIDs = $phids;
58 return $this;
61 public function withBlueprintStates(array $states) {
62 $this->blueprintStates = $states;
63 return $this;
66 public function withObjectStates(array $states) {
67 $this->objectStates = $states;
68 return $this;
71 public function newResultObject() {
72 return new DrydockAuthorization();
75 protected function loadPage() {
76 return $this->loadStandardPage($this->newResultObject());
79 protected function willFilterPage(array $authorizations) {
80 $blueprint_phids = mpull($authorizations, 'getBlueprintPHID');
81 if ($blueprint_phids) {
82 $blueprints = id(new DrydockBlueprintQuery())
83 ->setViewer($this->getViewer())
84 ->setParentQuery($this)
85 ->withPHIDs($blueprint_phids)
86 ->execute();
87 $blueprints = mpull($blueprints, null, 'getPHID');
88 } else {
89 $blueprints = array();
92 foreach ($authorizations as $key => $authorization) {
93 $blueprint = idx($blueprints, $authorization->getBlueprintPHID());
94 if (!$blueprint) {
95 $this->didRejectResult($authorization);
96 unset($authorizations[$key]);
97 continue;
99 $authorization->attachBlueprint($blueprint);
102 $object_phids = mpull($authorizations, 'getObjectPHID');
103 if ($object_phids) {
104 $objects = id(new PhabricatorObjectQuery())
105 ->setViewer($this->getViewer())
106 ->setParentQuery($this)
107 ->withPHIDs($object_phids)
108 ->execute();
109 $objects = mpull($objects, null, 'getPHID');
110 } else {
111 $objects = array();
114 foreach ($authorizations as $key => $authorization) {
115 $object = idx($objects, $authorization->getObjectPHID());
116 if (!$object) {
117 $this->didRejectResult($authorization);
118 unset($authorizations[$key]);
119 continue;
121 $authorization->attachObject($object);
124 return $authorizations;
127 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
128 $where = parent::buildWhereClauseParts($conn);
130 if ($this->ids !== null) {
131 $where[] = qsprintf(
132 $conn,
133 'id IN (%Ld)',
134 $this->ids);
137 if ($this->phids !== null) {
138 $where[] = qsprintf(
139 $conn,
140 'phid IN (%Ls)',
141 $this->phids);
144 if ($this->blueprintPHIDs !== null) {
145 $where[] = qsprintf(
146 $conn,
147 'blueprintPHID IN (%Ls)',
148 $this->blueprintPHIDs);
151 if ($this->objectPHIDs !== null) {
152 $where[] = qsprintf(
153 $conn,
154 'objectPHID IN (%Ls)',
155 $this->objectPHIDs);
158 if ($this->blueprintStates !== null) {
159 $where[] = qsprintf(
160 $conn,
161 'blueprintAuthorizationState IN (%Ls)',
162 $this->blueprintStates);
165 if ($this->objectStates !== null) {
166 $where[] = qsprintf(
167 $conn,
168 'objectAuthorizationState IN (%Ls)',
169 $this->objectStates);
172 return $where;