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