Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / diffusion / controller / DiffusionRepositoryURIViewController.php
blobe923ebfc205886a1a1623dd18453d37d93fa778d
1 <?php
3 final class DiffusionRepositoryURIViewController
4 extends DiffusionController {
6 public function handleRequest(AphrontRequest $request) {
7 $response = $this->loadDiffusionContext();
8 if ($response) {
9 return $response;
12 $viewer = $this->getViewer();
13 $drequest = $this->getDiffusionRequest();
14 $repository = $drequest->getRepository();
15 $id = $request->getURIData('id');
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();
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.
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();
38 $title = array(
39 pht('URI'),
40 $repository->getDisplayName(),
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/'));
52 $panel_label = id(new DiffusionRepositoryURIsManagementPanel())
53 ->getManagementPanelLabel();
54 $panel_uri = $repository->getPathURI('manage/uris/');
55 $crumbs->addTextCrumb($panel_label, $panel_uri);
57 $crumbs->addTextCrumb(pht('URI %d', $uri->getID()));
59 $header_text = pht(
60 '%s: URI %d',
61 $repository->getDisplayName(),
62 $uri->getID());
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'));
73 $curtain = $this->buildCurtain($uri);
74 $details = $this->buildPropertySection($uri);
76 $timeline = $this->buildTransactionTimeline(
77 $uri,
78 new PhabricatorRepositoryURITransactionQuery());
79 $timeline->setShouldTerminate(true);
81 $view = id(new PHUITwoColumnView())
82 ->setHeader($header)
83 ->setMainColumn(
84 array(
85 $details,
86 $timeline,
88 ->setCurtain($curtain);
90 return $this->newPage()
91 ->setTitle($title)
92 ->setCrumbs($crumbs)
93 ->appendChild($view);
96 private function buildCurtain(PhabricatorRepositoryURI $uri) {
97 $viewer = $this->getViewer();
98 $repository = $uri->getRepository();
99 $id = $uri->getID();
101 $can_edit = PhabricatorPolicyFilter::hasCapability(
102 $viewer,
103 $uri,
104 PhabricatorPolicyCapability::CAN_EDIT);
106 $curtain = $this->newCurtainView($uri);
108 $edit_uri = $uri->getEditURI();
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));
118 $credential_uri = $repository->getPathURI("uri/credential/{$id}/edit/");
119 $remove_uri = $repository->getPathURI("uri/credential/{$id}/remove/");
120 $has_credential = (bool)$uri->getCredentialPHID();
122 if ($uri->isBuiltin()) {
123 $can_credential = false;
124 } else if (!$uri->newCommandEngine()->isCredentialSupported()) {
125 $can_credential = false;
126 } else {
127 $can_credential = true;
130 $can_update = ($can_edit && $can_credential);
131 $can_remove = ($can_edit && $has_credential);
133 if ($has_credential) {
134 $credential_name = pht('Update Credential');
135 } else {
136 $credential_name = pht('Set Credential');
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));
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));
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';
163 $can_disable = ($can_edit && !$uri->isBuiltin());
165 $disable_uri = $repository->getPathURI("uri/disable/{$id}/");
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));
175 return $curtain;
178 private function buildPropertySection(PhabricatorRepositoryURI $uri) {
179 $viewer = $this->getViewer();
181 $properties = id(new PHUIPropertyListView())
182 ->setUser($viewer);
184 $properties->addProperty(pht('URI'), $uri->getDisplayURI());
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();
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.');
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);
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.');
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();
250 $credential_note = $viewer->renderHandle($credential_phid);
254 $credential_item = id(new PHUIStatusItemView())
255 ->setIcon($credential_icon, $credential_color)
256 ->setTarget(phutil_tag('strong', array(), $credential_label))
257 ->setNote($credential_note);
259 $credential_view = id(new PHUIStatusListView())
260 ->addItem($credential_item);
262 $properties->addProperty(pht('Credential'), $credential_view);
265 $io_type = $uri->getEffectiveIOType();
266 $io_map = PhabricatorRepositoryURI::getIOTypeMap();
267 $io_spec = idx($io_map, $io_type, array());
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');
274 $io_item = id(new PHUIStatusItemView())
275 ->setIcon($io_icon, $io_color)
276 ->setTarget(phutil_tag('strong', array(), $io_label))
277 ->setNote($io_note);
279 $io_view = id(new PHUIStatusListView())
280 ->addItem($io_item);
282 $properties->addProperty(pht('I/O'), $io_view);
285 $display_type = $uri->getEffectiveDisplayType();
286 $display_map = PhabricatorRepositoryURI::getDisplayTypeMap();
287 $display_spec = idx($display_map, $display_type, array());
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');
294 $display_item = id(new PHUIStatusItemView())
295 ->setIcon($display_icon, $display_color)
296 ->setTarget(phutil_tag('strong', array(), $display_label))
297 ->setNote($display_note);
299 $display_view = id(new PHUIStatusListView())
300 ->addItem($display_item);
302 $properties->addProperty(pht('Display'), $display_view);
305 return id(new PHUIObjectBoxView())
306 ->setHeaderText(pht('Details'))
307 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
308 ->appendChild($properties);