Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / controller / DiffusionRepositoryURICredentialController.php
blob96b97673cea82be7b3ea4cc1e0be17c8c0f2e8b3
1 <?php
3 final class DiffusionRepositoryURICredentialController
4 extends DiffusionController {
6 public function handleRequest(AphrontRequest $request) {
7 $response = $this->loadDiffusionContextForEdit();
8 if ($response) {
9 return $response;
12 $viewer = $this->getViewer();
13 $drequest = $this->getDiffusionRequest();
14 $repository = $drequest->getRepository();
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,
26 ->executeOne();
27 if (!$uri) {
28 return new Aphront404Response();
31 $is_builtin = $uri->isBuiltin();
32 $has_credential = (bool)$uri->getCredentialPHID();
33 $view_uri = $uri->getViewURI();
34 $is_remove = ($request->getURIData('action') == 'remove');
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 Phabricator '.
42 'hosts and serves. Phabricator does not fetch from these URIs or '.
43 'push to these URIs, and does not need credentials to '.
44 'authenticate any activity against them.'))
45 ->addCancelButton($view_uri);
48 if ($request->isFormPost()) {
49 $xactions = array();
51 if ($is_remove) {
52 $new_phid = null;
53 } else {
54 $new_phid = $request->getStr('credentialPHID');
57 $type_credential = PhabricatorRepositoryURITransaction::TYPE_CREDENTIAL;
59 $xactions[] = id(new PhabricatorRepositoryURITransaction())
60 ->setTransactionType($type_credential)
61 ->setNewValue($new_phid);
63 $editor = id(new DiffusionURIEditor())
64 ->setActor($viewer)
65 ->setContinueOnNoEffect(true)
66 ->setContinueOnMissingFields(true)
67 ->setContentSourceFromRequest($request)
68 ->applyTransactions($uri, $xactions);
70 return id(new AphrontRedirectResponse())->setURI($view_uri);
73 $command_engine = $uri->newCommandEngine();
74 $is_supported = $command_engine->isCredentialSupported();
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;
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();
102 $label = $command_engine->getPassphraseCredentialLabel();
103 $credential_type = $command_engine->getPassphraseDefaultCredentialType();
105 $provides_type = $command_engine->getPassphraseProvidesCredentialType();
106 $options = id(new PassphraseCredentialQuery())
107 ->setViewer($viewer)
108 ->withIsDestroyed(false)
109 ->withProvidesTypes(array($provides_type))
110 ->execute();
112 $control = id(new PassphraseCredentialControl())
113 ->setName('credentialPHID')
114 ->setLabel($label)
115 ->setValue($uri->getCredentialPHID())
116 ->setCredentialType($credential_type)
117 ->setOptions($options);
119 $default_user = $effective_uri->getUser();
120 if (strlen($default_user)) {
121 $control->setDefaultUsername($default_user);
124 $form = id(new AphrontFormView())
125 ->setViewer($viewer)
126 ->appendControl($control);
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');
136 $width = AphrontDialogView::WIDTH_FORM;
139 $dialog = $this->newDialog()
140 ->setWidth($width)
141 ->setTitle($title)
142 ->addCancelButton($view_uri);
144 if ($body) {
145 $dialog->appendParagraph($body);
148 if ($form) {
149 $dialog->appendForm($form);
152 if ($button) {
153 $dialog->addSubmitButton($button);
156 return $dialog;