@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 311 lines 9.8 kB view raw
1<?php 2 3final class DiffusionRepositoryURIViewController 4 extends DiffusionController { 5 6 public function handleRequest(AphrontRequest $request) { 7 $response = $this->loadDiffusionContext(); 8 if ($response) { 9 return $response; 10 } 11 12 $viewer = $this->getViewer(); 13 $drequest = $this->getDiffusionRequest(); 14 $repository = $drequest->getRepository(); 15 $id = $request->getURIData('id'); 16 17 $uri = id(new PhabricatorRepositoryURIQuery()) 18 ->setViewer($viewer) 19 ->withIDs(array($id)) 20 ->withRepositories(array($repository)) 21 ->executeOne(); 22 if (!$uri) { 23 return new Aphront404Response(); 24 } 25 26 // For display, access the URI by loading it through the repository. This 27 // may adjust builtin URIs for repository configuration, so we may end up 28 // with a different view of builtin URIs than we'd see if we loaded them 29 // directly from the database. See T12884. 30 31 $repository_uris = $repository->getURIs(); 32 $repository_uris = mpull($repository_uris, null, 'getID'); 33 $uri = idx($repository_uris, $uri->getID()); 34 if (!$uri) { 35 return new Aphront404Response(); 36 } 37 38 $title = array( 39 pht('URI'), 40 $repository->getDisplayName(), 41 ); 42 43 $crumbs = $this->buildApplicationCrumbs(); 44 $crumbs->setBorder(true); 45 $crumbs->addTextCrumb( 46 $repository->getDisplayName(), 47 $repository->getURI()); 48 $crumbs->addTextCrumb( 49 pht('Manage'), 50 $repository->getPathURI('manage/')); 51 52 $panel_label = id(new DiffusionRepositoryURIsManagementPanel()) 53 ->getManagementPanelLabel(); 54 $panel_uri = $repository->getPathURI('manage/uris/'); 55 $crumbs->addTextCrumb($panel_label, $panel_uri); 56 57 $crumbs->addTextCrumb(pht('URI %d', $uri->getID())); 58 59 $header_text = pht( 60 '%s: URI %d', 61 $repository->getDisplayName(), 62 $uri->getID()); 63 64 $header = id(new PHUIHeaderView()) 65 ->setHeader($header_text) 66 ->setHeaderIcon('fa-pencil'); 67 if ($uri->getIsDisabled()) { 68 $header->setStatus('fa-ban', 'dark', pht('Disabled')); 69 } else { 70 $header->setStatus('fa-check', 'bluegrey', pht('Active')); 71 } 72 73 $curtain = $this->buildCurtain($uri); 74 $details = $this->buildPropertySection($uri); 75 76 $timeline = $this->buildTransactionTimeline( 77 $uri, 78 new PhabricatorRepositoryURITransactionQuery()); 79 $timeline->setShouldTerminate(true); 80 81 $view = id(new PHUITwoColumnView()) 82 ->setHeader($header) 83 ->setMainColumn( 84 array( 85 $details, 86 $timeline, 87 )) 88 ->setCurtain($curtain); 89 90 return $this->newPage() 91 ->setTitle($title) 92 ->setCrumbs($crumbs) 93 ->appendChild($view); 94 } 95 96 private function buildCurtain(PhabricatorRepositoryURI $uri) { 97 $viewer = $this->getViewer(); 98 $repository = $uri->getRepository(); 99 $id = $uri->getID(); 100 101 $can_edit = PhabricatorPolicyFilter::hasCapability( 102 $viewer, 103 $uri, 104 PhabricatorPolicyCapability::CAN_EDIT); 105 106 $curtain = $this->newCurtainView($uri); 107 108 $edit_uri = $uri->getEditURI(); 109 110 $curtain->addAction( 111 id(new PhabricatorActionView()) 112 ->setIcon('fa-pencil') 113 ->setName(pht('Edit URI')) 114 ->setHref($edit_uri) 115 ->setWorkflow(!$can_edit) 116 ->setDisabled(!$can_edit)); 117 118 $credential_uri = $repository->getPathURI("uri/credential/{$id}/edit/"); 119 $remove_uri = $repository->getPathURI("uri/credential/{$id}/remove/"); 120 $has_credential = (bool)$uri->getCredentialPHID(); 121 122 if ($uri->isBuiltin()) { 123 $can_credential = false; 124 } else if (!$uri->newCommandEngine()->isCredentialSupported()) { 125 $can_credential = false; 126 } else { 127 $can_credential = true; 128 } 129 130 $can_update = ($can_edit && $can_credential); 131 $can_remove = ($can_edit && $has_credential); 132 133 if ($has_credential) { 134 $credential_name = pht('Update Credential'); 135 } else { 136 $credential_name = pht('Set Credential'); 137 } 138 139 $curtain->addAction( 140 id(new PhabricatorActionView()) 141 ->setIcon('fa-key') 142 ->setName($credential_name) 143 ->setHref($credential_uri) 144 ->setWorkflow(true) 145 ->setDisabled(!$can_edit)); 146 147 $curtain->addAction( 148 id(new PhabricatorActionView()) 149 ->setIcon('fa-times') 150 ->setName(pht('Remove Credential')) 151 ->setHref($remove_uri) 152 ->setWorkflow(true) 153 ->setDisabled(!$can_remove)); 154 155 if ($uri->getIsDisabled()) { 156 $disable_name = pht('Enable URI'); 157 $disable_icon = 'fa-check'; 158 } else { 159 $disable_name = pht('Disable URI'); 160 $disable_icon = 'fa-ban'; 161 } 162 163 $can_disable = ($can_edit && !$uri->isBuiltin()); 164 165 $disable_uri = $repository->getPathURI("uri/disable/{$id}/"); 166 167 $curtain->addAction( 168 id(new PhabricatorActionView()) 169 ->setIcon($disable_icon) 170 ->setName($disable_name) 171 ->setHref($disable_uri) 172 ->setWorkflow(true) 173 ->setDisabled(!$can_disable)); 174 175 return $curtain; 176 } 177 178 private function buildPropertySection(PhabricatorRepositoryURI $uri) { 179 $viewer = $this->getViewer(); 180 181 $properties = id(new PHUIPropertyListView()) 182 ->setViewer($viewer); 183 184 $properties->addProperty(pht('URI'), $uri->getDisplayURI()); 185 186 $credential_phid = $uri->getCredentialPHID(); 187 $command_engine = $uri->newCommandEngine(); 188 $is_optional = $command_engine->isCredentialOptional(); 189 $is_supported = $command_engine->isCredentialSupported(); 190 $is_builtin = $uri->isBuiltin(); 191 192 if ($is_builtin) { 193 $credential_icon = 'fa-circle-o'; 194 $credential_color = 'grey'; 195 $credential_label = pht('Builtin'); 196 $credential_note = pht('Builtin URIs do not use credentials.'); 197 } else if (!$is_supported) { 198 $credential_icon = 'fa-circle-o'; 199 $credential_color = 'grey'; 200 $credential_label = pht('Not Supported'); 201 $credential_note = pht('This protocol does not support authentication.'); 202 } else if (!$credential_phid) { 203 if ($is_optional) { 204 $credential_icon = 'fa-circle-o'; 205 $credential_color = 'green'; 206 $credential_label = pht('No Credential'); 207 $credential_note = pht('Configured for anonymous access.'); 208 } else { 209 $credential_icon = 'fa-times'; 210 $credential_color = 'red'; 211 $credential_label = pht('Required'); 212 $credential_note = pht('Credential required but not configured.'); 213 } 214 } else { 215 // Don't raise a policy exception if we can't see the credential. 216 $credentials = id(new PassphraseCredentialQuery()) 217 ->setViewer($viewer) 218 ->withPHIDs(array($credential_phid)) 219 ->execute(); 220 $credential = head($credentials); 221 222 if (!$credential) { 223 $handles = $viewer->loadHandles(array($credential_phid)); 224 $handle = $handles[$credential_phid]; 225 if ($handle->getPolicyFiltered()) { 226 $credential_icon = 'fa-lock'; 227 $credential_color = 'grey'; 228 $credential_label = pht('Restricted'); 229 $credential_note = pht( 230 'You do not have permission to view the configured '. 231 'credential.'); 232 } else { 233 $credential_icon = 'fa-times'; 234 $credential_color = 'red'; 235 $credential_label = pht('Invalid'); 236 $credential_note = pht('Configured credential is invalid.'); 237 } 238 } else { 239 $provides = $credential->getProvidesType(); 240 $needs = $command_engine->getPassphraseProvidesCredentialType(); 241 if ($provides != $needs) { 242 $credential_icon = 'fa-times'; 243 $credential_color = 'red'; 244 $credential_label = pht('Wrong Type'); 245 } else { 246 $credential_icon = 'fa-check'; 247 $credential_color = 'green'; 248 $credential_label = $command_engine->getPassphraseCredentialLabel(); 249 } 250 $credential_note = $viewer->renderHandle($credential_phid); 251 } 252 } 253 254 $credential_item = id(new PHUIStatusItemView()) 255 ->setIcon($credential_icon, $credential_color) 256 ->setTarget(phutil_tag('strong', array(), $credential_label)) 257 ->setNote($credential_note); 258 259 $credential_view = id(new PHUIStatusListView()) 260 ->addItem($credential_item); 261 262 $properties->addProperty(pht('Credential'), $credential_view); 263 264 265 $io_type = $uri->getEffectiveIOType(); 266 $io_map = PhabricatorRepositoryURI::getIOTypeMap(); 267 $io_spec = idx($io_map, $io_type, array()); 268 269 $io_icon = idx($io_spec, 'icon'); 270 $io_color = idx($io_spec, 'color'); 271 $io_label = idx($io_spec, 'label', $io_type); 272 $io_note = idx($io_spec, 'note'); 273 274 $io_item = id(new PHUIStatusItemView()) 275 ->setIcon($io_icon, $io_color) 276 ->setTarget(phutil_tag('strong', array(), $io_label)) 277 ->setNote($io_note); 278 279 $io_view = id(new PHUIStatusListView()) 280 ->addItem($io_item); 281 282 $properties->addProperty(pht('I/O'), $io_view); 283 284 285 $display_type = $uri->getEffectiveDisplayType(); 286 $display_map = PhabricatorRepositoryURI::getDisplayTypeMap(); 287 $display_spec = idx($display_map, $display_type, array()); 288 289 $display_icon = idx($display_spec, 'icon'); 290 $display_color = idx($display_spec, 'color'); 291 $display_label = idx($display_spec, 'label', $display_type); 292 $display_note = idx($display_spec, 'note'); 293 294 $display_item = id(new PHUIStatusItemView()) 295 ->setIcon($display_icon, $display_color) 296 ->setTarget(phutil_tag('strong', array(), $display_label)) 297 ->setNote($display_note); 298 299 $display_view = id(new PHUIStatusListView()) 300 ->addItem($display_item); 301 302 $properties->addProperty(pht('Display'), $display_view); 303 304 305 return id(new PHUIObjectBoxView()) 306 ->setHeaderText(pht('Details')) 307 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 308 ->appendChild($properties); 309 } 310 311}