@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
3final class DiffusionRepositoryURICredentialController
4 extends DiffusionController {
5
6 public function handleRequest(AphrontRequest $request) {
7 $response = $this->loadDiffusionContextForEdit();
8 if ($response) {
9 return $response;
10 }
11
12 $viewer = $this->getViewer();
13 $drequest = $this->getDiffusionRequest();
14 $repository = $drequest->getRepository();
15
16 $id = $request->getURIData('id');
17 $uri = id(new PhabricatorRepositoryURIQuery())
18 ->setViewer($viewer)
19 ->withIDs(array($id))
20 ->withRepositories(array($repository))
21 ->requireCapabilities(
22 array(
23 PhabricatorPolicyCapability::CAN_VIEW,
24 PhabricatorPolicyCapability::CAN_EDIT,
25 ))
26 ->executeOne();
27 if (!$uri) {
28 return new Aphront404Response();
29 }
30
31 $is_builtin = $uri->isBuiltin();
32 $has_credential = (bool)$uri->getCredentialPHID();
33 $view_uri = $uri->getViewURI();
34 $is_remove = ($request->getURIData('action') == 'remove');
35
36 if ($is_builtin) {
37 return $this->newDialog()
38 ->setTitle(pht('Builtin URIs Do Not Use Credentials'))
39 ->appendParagraph(
40 pht(
41 'You can not set a credential for builtin URIs which this '.
42 'server hosts. These URIs are not fetched from or pushed to, '.
43 'and credentials are not required to authenticate any '.
44 'activity against them.'))
45 ->addCancelButton($view_uri);
46 }
47
48 if ($request->isFormPost()) {
49 $xactions = array();
50
51 if ($is_remove) {
52 $new_phid = null;
53 } else {
54 $new_phid = $request->getStr('credentialPHID');
55 }
56
57 $type_credential = PhabricatorRepositoryURITransaction::TYPE_CREDENTIAL;
58
59 $xactions[] = id(new PhabricatorRepositoryURITransaction())
60 ->setTransactionType($type_credential)
61 ->setNewValue($new_phid);
62
63 $editor = id(new DiffusionURIEditor())
64 ->setActor($viewer)
65 ->setContinueOnNoEffect(true)
66 ->setContinueOnMissingFields(true)
67 ->setContentSourceFromRequest($request)
68 ->applyTransactions($uri, $xactions);
69
70 return id(new AphrontRedirectResponse())->setURI($view_uri);
71 }
72
73 $command_engine = $uri->newCommandEngine();
74 $is_supported = $command_engine->isCredentialSupported();
75
76 $body = null;
77 $form = null;
78 $width = AphrontDialogView::WIDTH_DEFAULT;
79 if ($is_remove) {
80 if ($has_credential) {
81 $title = pht('Remove Credential');
82 $body = pht(
83 'This credential will no longer be used to authenticate activity '.
84 'against this URI.');
85 $button = pht('Remove Credential');
86 } else {
87 $title = pht('No Credential');
88 $body = pht(
89 'This URI does not have an associated credential.');
90 $button = null;
91 }
92 } else if (!$is_supported) {
93 $title = pht('Unauthenticated Protocol');
94 $body = pht(
95 'The protocol for this URI ("%s") does not use authentication, so '.
96 'you can not provide a credential.',
97 $command_engine->getDisplayProtocol());
98 $button = null;
99 } else {
100 $effective_uri = $uri->getEffectiveURI();
101
102 $label = $command_engine->getPassphraseCredentialLabel();
103 $credential_type = $command_engine->getPassphraseDefaultCredentialType();
104
105 $provides_type = $command_engine->getPassphraseProvidesCredentialType();
106 $options = id(new PassphraseCredentialQuery())
107 ->setViewer($viewer)
108 ->withIsDestroyed(false)
109 ->withProvidesTypes(array($provides_type))
110 ->execute();
111
112 $control = id(new PassphraseCredentialControl())
113 ->setName('credentialPHID')
114 ->setLabel($label)
115 ->setValue($uri->getCredentialPHID())
116 ->setCredentialType($credential_type)
117 ->setOptions($options);
118
119 $default_user = $effective_uri->getUser();
120 if (strlen($default_user)) {
121 $control->setDefaultUsername($default_user);
122 }
123
124 $form = id(new AphrontFormView())
125 ->setViewer($viewer)
126 ->appendControl($control);
127
128 if ($has_credential) {
129 $title = pht('Update Credential');
130 $button = pht('Update Credential');
131 } else {
132 $title = pht('Set Credential');
133 $button = pht('Set Credential');
134 }
135
136 $width = AphrontDialogView::WIDTH_FORM;
137 }
138
139 $dialog = $this->newDialog()
140 ->setWidth($width)
141 ->setTitle($title)
142 ->addCancelButton($view_uri);
143
144 if ($body) {
145 $dialog->appendParagraph($body);
146 }
147
148 if ($form) {
149 $dialog->appendForm($form);
150 }
151
152 if ($button) {
153 $dialog->addSubmitButton($button);
154 }
155
156 return $dialog;
157 }
158
159}