Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/auth/query/PhabricatorAuthSSHKeyQuery.php
12256 views
1
<?php
2
3
final class PhabricatorAuthSSHKeyQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
const AUTHSTRUCT_CACHEKEY = 'ssh.authstruct';
7
8
private $ids;
9
private $phids;
10
private $objectPHIDs;
11
private $keys;
12
private $isActive;
13
14
public static function deleteSSHKeyCache() {
15
$cache = PhabricatorCaches::getMutableCache();
16
$authfile_key = self::AUTHSTRUCT_CACHEKEY;
17
$cache->deleteKey($authfile_key);
18
}
19
20
public function withIDs(array $ids) {
21
$this->ids = $ids;
22
return $this;
23
}
24
25
public function withPHIDs(array $phids) {
26
$this->phids = $phids;
27
return $this;
28
}
29
30
public function withObjectPHIDs(array $object_phids) {
31
$this->objectPHIDs = $object_phids;
32
return $this;
33
}
34
35
public function withKeys(array $keys) {
36
assert_instances_of($keys, 'PhabricatorAuthSSHPublicKey');
37
$this->keys = $keys;
38
return $this;
39
}
40
41
public function withIsActive($active) {
42
$this->isActive = $active;
43
return $this;
44
}
45
46
public function newResultObject() {
47
return new PhabricatorAuthSSHKey();
48
}
49
50
protected function willFilterPage(array $keys) {
51
$object_phids = mpull($keys, 'getObjectPHID');
52
53
$objects = id(new PhabricatorObjectQuery())
54
->setViewer($this->getViewer())
55
->setParentQuery($this)
56
->withPHIDs($object_phids)
57
->execute();
58
$objects = mpull($objects, null, 'getPHID');
59
60
foreach ($keys as $key => $ssh_key) {
61
$object = idx($objects, $ssh_key->getObjectPHID());
62
63
// We must have an object, and that object must be a valid object for
64
// SSH keys.
65
if (!$object || !($object instanceof PhabricatorSSHPublicKeyInterface)) {
66
$this->didRejectResult($ssh_key);
67
unset($keys[$key]);
68
continue;
69
}
70
71
$ssh_key->attachObject($object);
72
}
73
74
return $keys;
75
}
76
77
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
78
$where = parent::buildWhereClauseParts($conn);
79
80
if ($this->ids !== null) {
81
$where[] = qsprintf(
82
$conn,
83
'id IN (%Ld)',
84
$this->ids);
85
}
86
87
if ($this->phids !== null) {
88
$where[] = qsprintf(
89
$conn,
90
'phid IN (%Ls)',
91
$this->phids);
92
}
93
94
if ($this->objectPHIDs !== null) {
95
$where[] = qsprintf(
96
$conn,
97
'objectPHID IN (%Ls)',
98
$this->objectPHIDs);
99
}
100
101
if ($this->keys !== null) {
102
$sql = array();
103
foreach ($this->keys as $key) {
104
$sql[] = qsprintf(
105
$conn,
106
'(keyType = %s AND keyIndex = %s)',
107
$key->getType(),
108
$key->getHash());
109
}
110
$where[] = qsprintf($conn, '%LO', $sql);
111
}
112
113
if ($this->isActive !== null) {
114
if ($this->isActive) {
115
$where[] = qsprintf(
116
$conn,
117
'isActive = %d',
118
1);
119
} else {
120
$where[] = qsprintf(
121
$conn,
122
'isActive IS NULL');
123
}
124
}
125
126
return $where;
127
128
}
129
130
public function getQueryApplicationClass() {
131
return 'PhabricatorAuthApplication';
132
}
133
134
}
135
136