@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 PhabricatorAuthSSHKeyEditController
4 extends PhabricatorAuthSSHKeyController {
5
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $this->getViewer();
8 $id = $request->getURIData('id');
9
10 if ($id) {
11 $key = id(new PhabricatorAuthSSHKeyQuery())
12 ->setViewer($viewer)
13 ->withIDs(array($id))
14 ->requireCapabilities(
15 array(
16 PhabricatorPolicyCapability::CAN_VIEW,
17 PhabricatorPolicyCapability::CAN_EDIT,
18 ))
19 ->executeOne();
20 if (!$key) {
21 return new Aphront404Response();
22 }
23
24 $is_new = false;
25 } else {
26 $key = $this->newKeyForObjectPHID($request->getStr('objectPHID'));
27 if (!$key) {
28 return new Aphront404Response();
29 }
30 $is_new = true;
31 }
32
33 $cancel_uri = $key->getObject()->getSSHPublicKeyManagementURI($viewer);
34
35 if ($key->getIsTrusted()) {
36 $id = $key->getID();
37
38 return $this->newDialog()
39 ->setTitle(pht('Can Not Edit Trusted Key'))
40 ->appendParagraph(
41 pht(
42 'This key is trusted. Trusted keys can not be edited. '.
43 'Use %s to revoke trust before editing the key.',
44 phutil_tag(
45 'tt',
46 array(),
47 "bin/almanac untrust-key --id {$id}")))
48 ->addCancelButton($cancel_uri, pht('Okay'));
49 }
50
51 $token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(
52 $viewer,
53 $request,
54 $cancel_uri);
55
56 $v_name = $key->getName();
57 $e_name = phutil_nonempty_string($v_name) ? null : true;
58
59 $v_key = $key->getEntireKey();
60 $e_key = strlen($v_key) ? null : true;
61
62 $validation_exception = null;
63 if ($request->isFormPost()) {
64 $type_create = PhabricatorTransactions::TYPE_CREATE;
65 $type_name = PhabricatorAuthSSHKeyTransaction::TYPE_NAME;
66 $type_key = PhabricatorAuthSSHKeyTransaction::TYPE_KEY;
67
68 $e_name = null;
69 $e_key = null;
70
71 $v_name = $request->getStr('name');
72 $v_key = $request->getStr('key');
73
74 $xactions = array();
75
76 if (!$key->getID()) {
77 $xactions[] = id(new PhabricatorAuthSSHKeyTransaction())
78 ->setTransactionType(PhabricatorTransactions::TYPE_CREATE);
79 }
80
81 $xactions[] = id(new PhabricatorAuthSSHKeyTransaction())
82 ->setTransactionType($type_name)
83 ->setNewValue($v_name);
84
85 $xactions[] = id(new PhabricatorAuthSSHKeyTransaction())
86 ->setTransactionType($type_key)
87 ->setNewValue($v_key);
88
89 $editor = id(new PhabricatorAuthSSHKeyEditor())
90 ->setActor($viewer)
91 ->setContentSourceFromRequest($request)
92 ->setContinueOnNoEffect(true);
93
94 try {
95 $editor->applyTransactions($key, $xactions);
96 return id(new AphrontRedirectResponse())->setURI($cancel_uri);
97 } catch (PhabricatorApplicationTransactionValidationException $ex) {
98 $validation_exception = $ex;
99 $e_name = $ex->getShortMessage($type_name);
100 $e_key = $ex->getShortMessage($type_key);
101 }
102 }
103
104 $form = id(new AphrontFormView())
105 ->setUser($viewer)
106 ->appendChild(
107 id(new AphrontFormTextControl())
108 ->setLabel(pht('Name'))
109 ->setName('name')
110 ->setError($e_name)
111 ->setValue($v_name))
112 ->appendChild(
113 id(new AphrontFormTextAreaControl())
114 ->setLabel(pht('Public Key'))
115 ->setName('key')
116 ->setValue($v_key)
117 ->setError($e_key));
118
119 if ($is_new) {
120 $title = pht('Upload SSH Public Key');
121 $save_button = pht('Upload Public Key');
122 $form->addHiddenInput('objectPHID', $key->getObject()->getPHID());
123 } else {
124 $title = pht('Edit SSH Public Key');
125 $save_button = pht('Save Changes');
126 }
127
128 return $this->newDialog()
129 ->setTitle($title)
130 ->setWidth(AphrontDialogView::WIDTH_FORM)
131 ->setValidationException($validation_exception)
132 ->appendForm($form)
133 ->addSubmitButton($save_button)
134 ->addCancelButton($cancel_uri);
135 }
136
137}