Path: blob/master/src/applications/auth/controller/PhabricatorAuthSSHKeyEditController.php
12256 views
<?php12final class PhabricatorAuthSSHKeyEditController3extends PhabricatorAuthSSHKeyController {45public function handleRequest(AphrontRequest $request) {6$viewer = $this->getViewer();7$id = $request->getURIData('id');89if ($id) {10$key = id(new PhabricatorAuthSSHKeyQuery())11->setViewer($viewer)12->withIDs(array($id))13->requireCapabilities(14array(15PhabricatorPolicyCapability::CAN_VIEW,16PhabricatorPolicyCapability::CAN_EDIT,17))18->executeOne();19if (!$key) {20return new Aphront404Response();21}2223$is_new = false;24} else {25$key = $this->newKeyForObjectPHID($request->getStr('objectPHID'));26if (!$key) {27return new Aphront404Response();28}29$is_new = true;30}3132$cancel_uri = $key->getObject()->getSSHPublicKeyManagementURI($viewer);3334if ($key->getIsTrusted()) {35$id = $key->getID();3637return $this->newDialog()38->setTitle(pht('Can Not Edit Trusted Key'))39->appendParagraph(40pht(41'This key is trusted. Trusted keys can not be edited. '.42'Use %s to revoke trust before editing the key.',43phutil_tag(44'tt',45array(),46"bin/almanac untrust-key --id {$id}")))47->addCancelButton($cancel_uri, pht('Okay'));48}4950$token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(51$viewer,52$request,53$cancel_uri);5455$v_name = $key->getName();56$e_name = $v_name !== null && strlen($v_name) ? null : true;5758$v_key = $key->getEntireKey();59$e_key = $v_key !== null && strlen($v_key) ? null : true;6061$validation_exception = null;62if ($request->isFormPost()) {63$type_create = PhabricatorTransactions::TYPE_CREATE;64$type_name = PhabricatorAuthSSHKeyTransaction::TYPE_NAME;65$type_key = PhabricatorAuthSSHKeyTransaction::TYPE_KEY;6667$e_name = null;68$e_key = null;6970$v_name = $request->getStr('name');71$v_key = $request->getStr('key');7273$xactions = array();7475if (!$key->getID()) {76$xactions[] = id(new PhabricatorAuthSSHKeyTransaction())77->setTransactionType(PhabricatorTransactions::TYPE_CREATE);78}7980$xactions[] = id(new PhabricatorAuthSSHKeyTransaction())81->setTransactionType($type_name)82->setNewValue($v_name);8384$xactions[] = id(new PhabricatorAuthSSHKeyTransaction())85->setTransactionType($type_key)86->setNewValue($v_key);8788$editor = id(new PhabricatorAuthSSHKeyEditor())89->setActor($viewer)90->setContentSourceFromRequest($request)91->setContinueOnNoEffect(true);9293try {94$editor->applyTransactions($key, $xactions);95return id(new AphrontRedirectResponse())->setURI($cancel_uri);96} catch (PhabricatorApplicationTransactionValidationException $ex) {97$validation_exception = $ex;98$e_name = $ex->getShortMessage($type_name);99$e_key = $ex->getShortMessage($type_key);100}101}102103$form = id(new AphrontFormView())104->setUser($viewer)105->appendChild(106id(new AphrontFormTextControl())107->setLabel(pht('Name'))108->setName('name')109->setError($e_name)110->setValue($v_name))111->appendChild(112id(new AphrontFormTextAreaControl())113->setLabel(pht('Public Key'))114->setName('key')115->setValue($v_key)116->setError($e_key));117118if ($is_new) {119$title = pht('Upload SSH Public Key');120$save_button = pht('Upload Public Key');121$form->addHiddenInput('objectPHID', $key->getObject()->getPHID());122} else {123$title = pht('Edit SSH Public Key');124$save_button = pht('Save Changes');125}126127return $this->newDialog()128->setTitle($title)129->setWidth(AphrontDialogView::WIDTH_FORM)130->setValidationException($validation_exception)131->appendForm($form)132->addSubmitButton($save_button)133->addCancelButton($cancel_uri);134}135136}137138139