@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 PhabricatorCursorPagedPolicyAwareQuery<PassphraseCredential>
5 */
6final class PassphraseCredentialQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $credentialTypes;
12 private $providesTypes;
13 private $isDestroyed;
14 private $allowConduit;
15 private $nameContains;
16
17 private $needSecrets;
18
19 public function withIDs(array $ids) {
20 $this->ids = $ids;
21 return $this;
22 }
23
24 public function withPHIDs(array $phids) {
25 $this->phids = $phids;
26 return $this;
27 }
28
29 public function withCredentialTypes(array $credential_types) {
30 $this->credentialTypes = $credential_types;
31 return $this;
32 }
33
34 public function withProvidesTypes(array $provides_types) {
35 $this->providesTypes = $provides_types;
36 return $this;
37 }
38
39 public function withIsDestroyed($destroyed) {
40 $this->isDestroyed = $destroyed;
41 return $this;
42 }
43
44 public function withAllowConduit($allow_conduit) {
45 $this->allowConduit = $allow_conduit;
46 return $this;
47 }
48
49 public function withNameContains($name_contains) {
50 $this->nameContains = $name_contains;
51 return $this;
52 }
53
54 public function needSecrets($need_secrets) {
55 $this->needSecrets = $need_secrets;
56 return $this;
57 }
58
59 public function newResultObject() {
60 return new PassphraseCredential();
61 }
62
63 protected function willFilterPage(array $page) {
64 if ($this->needSecrets) {
65 $secret_ids = mpull($page, 'getSecretID');
66 $secret_ids = array_filter($secret_ids);
67
68 $secrets = array();
69 if ($secret_ids) {
70 $secret_objects = id(new PassphraseSecret())->loadAllWhere(
71 'id IN (%Ld)',
72 $secret_ids);
73 foreach ($secret_objects as $secret) {
74 $secret_data = $secret->getSecretData();
75 $secrets[$secret->getID()] = new PhutilOpaqueEnvelope($secret_data);
76 }
77 }
78
79 foreach ($page as $key => $credential) {
80 $secret_id = $credential->getSecretID();
81 if (!$secret_id) {
82 $credential->attachSecret(null);
83 } else if (isset($secrets[$secret_id])) {
84 $credential->attachSecret($secrets[$secret_id]);
85 } else {
86 unset($page[$key]);
87 }
88 }
89 }
90
91 foreach ($page as $key => $credential) {
92 $type = PassphraseCredentialType::getTypeByConstant(
93 $credential->getCredentialType());
94 if (!$type) {
95 unset($page[$key]);
96 continue;
97 }
98
99 $credential->attachImplementation(clone $type);
100 }
101
102 return $page;
103 }
104
105 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
106 $where = parent::buildWhereClauseParts($conn);
107
108 if ($this->ids !== null) {
109 $where[] = qsprintf(
110 $conn,
111 'c.id IN (%Ld)',
112 $this->ids);
113 }
114
115 if ($this->phids !== null) {
116 $where[] = qsprintf(
117 $conn,
118 'c.phid IN (%Ls)',
119 $this->phids);
120 }
121
122 if ($this->credentialTypes !== null) {
123 $where[] = qsprintf(
124 $conn,
125 'c.credentialType in (%Ls)',
126 $this->credentialTypes);
127 }
128
129 if ($this->providesTypes !== null) {
130 $where[] = qsprintf(
131 $conn,
132 'c.providesType IN (%Ls)',
133 $this->providesTypes);
134 }
135
136 if ($this->isDestroyed !== null) {
137 $where[] = qsprintf(
138 $conn,
139 'c.isDestroyed = %d',
140 (int)$this->isDestroyed);
141 }
142
143 if ($this->allowConduit !== null) {
144 $where[] = qsprintf(
145 $conn,
146 'c.allowConduit = %d',
147 (int)$this->allowConduit);
148 }
149
150 if (phutil_nonempty_string($this->nameContains)) {
151 $where[] = qsprintf(
152 $conn,
153 'LOWER(c.name) LIKE %~',
154 phutil_utf8_strtolower($this->nameContains));
155 }
156
157 return $where;
158 }
159
160 public function getQueryApplicationClass() {
161 return PhabricatorPassphraseApplication::class;
162 }
163
164 protected function getPrimaryTableAlias() {
165 return 'c';
166 }
167
168}