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