@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * @extends DrydockQuery<DrydockAuthorization>
5 */
6final class DrydockAuthorizationQuery extends DrydockQuery {
7
8 private $ids;
9 private $phids;
10 private $blueprintPHIDs;
11 private $objectPHIDs;
12 private $blueprintStates;
13 private $objectStates;
14
15 public static function isFullyAuthorized(
16 $object_phid,
17 array $blueprint_phids) {
18
19 if (!$blueprint_phids) {
20 return true;
21 }
22
23 $authorizations = id(new self())
24 ->setViewer(PhabricatorUser::getOmnipotentUser())
25 ->withObjectPHIDs(array($object_phid))
26 ->withBlueprintPHIDs($blueprint_phids)
27 ->execute();
28 $authorizations = mpull($authorizations, null, 'getBlueprintPHID');
29
30 foreach ($blueprint_phids as $phid) {
31 $authorization = idx($authorizations, $phid);
32 if (!$authorization) {
33 return false;
34 }
35
36 if (!$authorization->isAuthorized()) {
37 return false;
38 }
39 }
40
41 return true;
42 }
43
44 public function withIDs(array $ids) {
45 $this->ids = $ids;
46 return $this;
47 }
48
49 public function withPHIDs(array $phids) {
50 $this->phids = $phids;
51 return $this;
52 }
53
54 public function withBlueprintPHIDs(array $phids) {
55 $this->blueprintPHIDs = $phids;
56 return $this;
57 }
58
59 public function withObjectPHIDs(array $phids) {
60 $this->objectPHIDs = $phids;
61 return $this;
62 }
63
64 public function withBlueprintStates(array $states) {
65 $this->blueprintStates = $states;
66 return $this;
67 }
68
69 public function withObjectStates(array $states) {
70 $this->objectStates = $states;
71 return $this;
72 }
73
74 public function newResultObject() {
75 return new DrydockAuthorization();
76 }
77
78 protected function willFilterPage(array $authorizations) {
79 $blueprint_phids = mpull($authorizations, 'getBlueprintPHID');
80 if ($blueprint_phids) {
81 $blueprints = id(new DrydockBlueprintQuery())
82 ->setViewer($this->getViewer())
83 ->setParentQuery($this)
84 ->withPHIDs($blueprint_phids)
85 ->execute();
86 $blueprints = mpull($blueprints, null, 'getPHID');
87 } else {
88 $blueprints = array();
89 }
90
91 foreach ($authorizations as $key => $authorization) {
92 $blueprint = idx($blueprints, $authorization->getBlueprintPHID());
93 if (!$blueprint) {
94 $this->didRejectResult($authorization);
95 unset($authorizations[$key]);
96 continue;
97 }
98 $authorization->attachBlueprint($blueprint);
99 }
100
101 $object_phids = mpull($authorizations, 'getObjectPHID');
102 if ($object_phids) {
103 $objects = id(new PhabricatorObjectQuery())
104 ->setViewer($this->getViewer())
105 ->setParentQuery($this)
106 ->withPHIDs($object_phids)
107 ->execute();
108 $objects = mpull($objects, null, 'getPHID');
109 } else {
110 $objects = array();
111 }
112
113 foreach ($authorizations as $key => $authorization) {
114 $object = idx($objects, $authorization->getObjectPHID());
115 if (!$object) {
116 $this->didRejectResult($authorization);
117 unset($authorizations[$key]);
118 continue;
119 }
120 $authorization->attachObject($object);
121 }
122
123 return $authorizations;
124 }
125
126 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
127 $where = parent::buildWhereClauseParts($conn);
128
129 if ($this->ids !== null) {
130 $where[] = qsprintf(
131 $conn,
132 'id IN (%Ld)',
133 $this->ids);
134 }
135
136 if ($this->phids !== null) {
137 $where[] = qsprintf(
138 $conn,
139 'phid IN (%Ls)',
140 $this->phids);
141 }
142
143 if ($this->blueprintPHIDs !== null) {
144 $where[] = qsprintf(
145 $conn,
146 'blueprintPHID IN (%Ls)',
147 $this->blueprintPHIDs);
148 }
149
150 if ($this->objectPHIDs !== null) {
151 $where[] = qsprintf(
152 $conn,
153 'objectPHID IN (%Ls)',
154 $this->objectPHIDs);
155 }
156
157 if ($this->blueprintStates !== null) {
158 $where[] = qsprintf(
159 $conn,
160 'blueprintAuthorizationState IN (%Ls)',
161 $this->blueprintStates);
162 }
163
164 if ($this->objectStates !== null) {
165 $where[] = qsprintf(
166 $conn,
167 'objectAuthorizationState IN (%Ls)',
168 $this->objectStates);
169 }
170
171 return $where;
172 }
173
174}