1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-30 18:52:42 +01:00
phorge-phorge/src/applications/almanac/controller/AlmanacInterfaceEditController.php
epriestley 3e516e40f3 Build AlmanacInterface
Summary: Ref T5833. An interface is an IP (maybe v4, maybe v6) and port on a specified network (public internet, VPN, NAT block, etc).

Test Plan: See screenshots.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T5833

Differential Revision: https://secure.phabricator.com/D10718
2014-10-17 05:04:24 -07:00

155 lines
4.3 KiB
PHP

<?php
final class AlmanacInterfaceEditController
extends AlmanacDeviceController {
public function handleRequest(AphrontRequest $request) {
$viewer = $request->getViewer();
$id = $request->getURIData('id');
if ($id) {
$interface = id(new AlmanacInterfaceQuery())
->setViewer($viewer)
->withIDs(array($id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$interface) {
return new Aphront404Response();
}
$device = $interface->getDevice();
$is_new = false;
$title = pht('Edit Interface');
$save_button = pht('Save Changes');
} else {
$device = id(new AlmanacDeviceQuery())
->setViewer($viewer)
->withIDs(array($request->getStr('deviceID')))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$device) {
return new Aphront404Response();
}
$interface = AlmanacInterface::initializeNewInterface();
$is_new = true;
$title = pht('Create Interface');
$save_button = pht('Create Interface');
}
$device_uri = $device->getURI();
$cancel_uri = $device_uri;
$v_network = $interface->getNetworkPHID();
$v_address = $interface->getAddress();
$e_address = true;
$v_port = $interface->getPort();
$validation_exception = null;
if ($request->isFormPost()) {
$v_network = $request->getStr('networkPHID');
$v_address = $request->getStr('address');
$v_port = $request->getStr('port');
$type_interface = AlmanacDeviceTransaction::TYPE_INTERFACE;
$address = AlmanacAddress::newFromParts($v_network, $v_address, $v_port);
$xaction = id(new AlmanacDeviceTransaction())
->setTransactionType($type_interface)
->setNewValue($address->toDictionary());
if ($interface->getID()) {
$xaction->setOldValue(array(
'id' => $interface->getID(),
) + $interface->toAddress()->toDictionary());
} else {
$xaction->setOldValue(array());
}
$xactions = array();
$xactions[] = $xaction;
$editor = id(new AlmanacDeviceEditor())
->setActor($viewer)
->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true)
->setContinueOnMissingFields(true);
try {
$editor->applyTransactions($device, $xactions);
$device_uri = $device->getURI();
return id(new AphrontRedirectResponse())->setURI($device_uri);
} catch (PhabricatorApplicationTransactionValidationException $ex) {
$validation_exception = $ex;
$e_address = $ex->getShortMessage($type_interface);
}
}
$networks = id(new AlmanacNetworkQuery())
->setViewer($viewer)
->execute();
$form = id(new AphrontFormView())
->setUser($viewer)
->appendChild(
id(new AphrontFormSelectControl())
->setLabel(pht('Network'))
->setName('networkPHID')
->setValue($v_network)
->setOptions(mpull($networks, 'getName', 'getPHID')))
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Address'))
->setName('address')
->setValue($v_address)
->setError($e_address))
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Port'))
->setName('port')
->setValue($v_port)
->setError($e_address))
->appendChild(
id(new AphrontFormSubmitControl())
->addCancelButton($cancel_uri)
->setValue($save_button));
$box = id(new PHUIObjectBoxView())
->setValidationException($validation_exception)
->setHeaderText($title)
->appendChild($form);
$crumbs = $this->buildApplicationCrumbs();
$crumbs->addTextCrumb($device->getName(), $device_uri);
if ($is_new) {
$crumbs->addTextCrumb(pht('Create Interface'));
} else {
$crumbs->addTextCrumb(pht('Edit Interface'));
}
return $this->buildApplicationPage(
array(
$crumbs,
$box,
),
array(
'title' => $title,
));
}
}