Page MenuHomePhabricator

D19324.id46230.diff
No OneTemporary

D19324.id46230.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -62,6 +62,7 @@
'AlmanacInterfaceDeleteController' => 'applications/almanac/controller/AlmanacInterfaceDeleteController.php',
'AlmanacInterfaceDeviceTransaction' => 'applications/almanac/xaction/AlmanacInterfaceDeviceTransaction.php',
'AlmanacInterfaceEditController' => 'applications/almanac/controller/AlmanacInterfaceEditController.php',
+ 'AlmanacInterfaceEditEngine' => 'applications/almanac/editor/AlmanacInterfaceEditEngine.php',
'AlmanacInterfaceEditor' => 'applications/almanac/editor/AlmanacInterfaceEditor.php',
'AlmanacInterfaceNetworkTransaction' => 'applications/almanac/xaction/AlmanacInterfaceNetworkTransaction.php',
'AlmanacInterfacePHIDType' => 'applications/almanac/phid/AlmanacInterfacePHIDType.php',
@@ -5251,15 +5252,16 @@
'PhabricatorExtendedPolicyInterface',
'PhabricatorApplicationTransactionInterface',
),
- 'AlmanacInterfaceAddressTransaction' => 'AlmanacNetworkTransactionType',
+ 'AlmanacInterfaceAddressTransaction' => 'AlmanacInterfaceTransactionType',
'AlmanacInterfaceDatasource' => 'PhabricatorTypeaheadDatasource',
'AlmanacInterfaceDeleteController' => 'AlmanacDeviceController',
- 'AlmanacInterfaceDeviceTransaction' => 'AlmanacNetworkTransactionType',
+ 'AlmanacInterfaceDeviceTransaction' => 'AlmanacInterfaceTransactionType',
'AlmanacInterfaceEditController' => 'AlmanacDeviceController',
+ 'AlmanacInterfaceEditEngine' => 'PhabricatorEditEngine',
'AlmanacInterfaceEditor' => 'PhabricatorApplicationTransactionEditor',
- 'AlmanacInterfaceNetworkTransaction' => 'AlmanacNetworkTransactionType',
+ 'AlmanacInterfaceNetworkTransaction' => 'AlmanacInterfaceTransactionType',
'AlmanacInterfacePHIDType' => 'PhabricatorPHIDType',
- 'AlmanacInterfacePortTransaction' => 'AlmanacNetworkTransactionType',
+ 'AlmanacInterfacePortTransaction' => 'AlmanacInterfaceTransactionType',
'AlmanacInterfaceQuery' => 'AlmanacQuery',
'AlmanacInterfaceTableView' => 'AphrontView',
'AlmanacInterfaceTransaction' => 'PhabricatorModularTransaction',
diff --git a/src/applications/almanac/controller/AlmanacInterfaceEditController.php b/src/applications/almanac/controller/AlmanacInterfaceEditController.php
--- a/src/applications/almanac/controller/AlmanacInterfaceEditController.php
+++ b/src/applications/almanac/controller/AlmanacInterfaceEditController.php
@@ -4,32 +4,16 @@
extends AlmanacDeviceController {
public function handleRequest(AphrontRequest $request) {
- $viewer = $request->getViewer();
+ $viewer = $this->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();
- }
+ $engine = id(new AlmanacInterfaceEditEngine())
+ ->setController($this);
- $device = $interface->getDevice();
-
- $is_new = false;
- $title = pht('Edit Interface');
- $save_button = pht('Save Changes');
- } else {
+ $id = $request->getURIData('id');
+ if (!$id) {
$device = id(new AlmanacDeviceQuery())
->setViewer($viewer)
- ->withIDs(array($request->getStr('deviceID')))
+ ->withIDs(array($request->getInt('deviceID')))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
@@ -40,127 +24,12 @@
return new Aphront404Response();
}
- $interface = AlmanacInterface::initializeNewInterface();
- $is_new = true;
-
- $title = pht('Create Interface');
- $save_button = pht('Create Interface');
+ $engine
+ ->addContextParameter('deviceID', $device->getID())
+ ->setDevice($device);
}
- $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(pht('Interface'))
- ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
- ->setForm($form);
-
- $crumbs = $this->buildApplicationCrumbs();
- $crumbs->addTextCrumb($device->getName(), $device_uri);
- if ($is_new) {
- $crumbs->addTextCrumb(pht('Create Interface'));
- $header = id(new PHUIHeaderView())
- ->setHeader(pht('Create Interface'))
- ->setHeaderIcon('fa-plus-square');
- } else {
- $crumbs->addTextCrumb(pht('Edit Interface'));
- $header = id(new PHUIHeaderView())
- ->setHeader(pht('Edit Interface'))
- ->setHeaderIcon('fa-pencil');
- }
- $crumbs->setBorder(true);
-
- $view = id(new PHUITwoColumnView())
- ->setHeader($header)
- ->setFooter(array(
- $box,
- ));
-
- return $this->newPage()
- ->setTitle($title)
- ->setCrumbs($crumbs)
- ->appendChild($view);
-
+ return $engine->buildResponse();
}
}
diff --git a/src/applications/almanac/editor/AlmanacInterfaceEditEngine.php b/src/applications/almanac/editor/AlmanacInterfaceEditEngine.php
new file mode 100644
--- /dev/null
+++ b/src/applications/almanac/editor/AlmanacInterfaceEditEngine.php
@@ -0,0 +1,139 @@
+<?php
+
+final class AlmanacInterfaceEditEngine
+ extends PhabricatorEditEngine {
+
+ const ENGINECONST = 'almanac.interface';
+
+ private $device;
+
+ public function setDevice(AlmanacDevice $device) {
+ $this->device = $device;
+ return $this;
+ }
+
+ public function getDevice() {
+ if (!$this->device) {
+ throw new PhutilInvalidStateException('setDevice');
+ }
+ return $this->device;
+ }
+
+ public function isEngineConfigurable() {
+ return false;
+ }
+
+ public function getEngineName() {
+ return pht('Almanac Interfaces');
+ }
+
+ public function getSummaryHeader() {
+ return pht('Edit Almanac Interface Configurations');
+ }
+
+ public function getSummaryText() {
+ return pht('This engine is used to edit Almanac interfaces.');
+ }
+
+ public function getEngineApplicationClass() {
+ return 'PhabricatorAlmanacApplication';
+ }
+
+ protected function newEditableObject() {
+ $interface = AlmanacInterface::initializeNewInterface();
+
+ $device = $this->getDevice();
+ $interface
+ ->setDevicePHID($device->getPHID())
+ ->attachDevice($device);
+
+ return $interface;
+ }
+
+ protected function newObjectQuery() {
+ return new AlmanacInterfaceQuery();
+ }
+
+ protected function getObjectCreateTitleText($object) {
+ return pht('Create Interface');
+ }
+
+ protected function getObjectCreateButtonText($object) {
+ return pht('Create Interface');
+ }
+
+ protected function getObjectEditTitleText($object) {
+ return pht('Edit Interface');
+ }
+
+ protected function getObjectEditShortText($object) {
+ return pht('Edit Interface');
+ }
+
+ protected function getObjectCreateShortText() {
+ return pht('Create Interface');
+ }
+
+ protected function getObjectName() {
+ return pht('Interface');
+ }
+
+ protected function getEditorURI() {
+ return '/almanac/interface/edit/';
+ }
+
+ protected function getObjectCreateCancelURI($object) {
+ return '/almanac/interface/';
+ }
+
+ protected function getObjectViewURI($object) {
+ return $object->getDevice()->getURI();
+ }
+
+ protected function buildCustomEditFields($object) {
+ $viewer = $this->getViewer();
+
+ // TODO: Some day, this should be a datasource.
+ $networks = id(new AlmanacNetworkQuery())
+ ->setViewer($viewer)
+ ->execute();
+ $network_map = mpull($networks, 'getName', 'getPHID');
+
+ return array(
+ id(new PhabricatorTextEditField())
+ ->setKey('device')
+ ->setLabel(pht('Device'))
+ ->setIsConduitOnly(true)
+ ->setTransactionType(
+ AlmanacInterfaceDeviceTransaction::TRANSACTIONTYPE)
+ ->setDescription(pht('When creating an interface, set the device.'))
+ ->setConduitDescription(pht('Set the device.'))
+ ->setConduitTypeDescription(pht('Device PHID.'))
+ ->setValue($object->getDevicePHID()),
+ id(new PhabricatorSelectEditField())
+ ->setKey('network')
+ ->setLabel(pht('Network'))
+ ->setDescription(pht('Network for the interface.'))
+ ->setTransactionType(
+ AlmanacInterfaceNetworkTransaction::TRANSACTIONTYPE)
+ ->setValue($object->getNetworkPHID())
+ ->setOptions($network_map),
+ id(new PhabricatorTextEditField())
+ ->setKey('address')
+ ->setLabel(pht('Address'))
+ ->setDescription(pht('Address of the service.'))
+ ->setTransactionType(
+ AlmanacInterfaceAddressTransaction::TRANSACTIONTYPE)
+ ->setIsRequired(true)
+ ->setValue($object->getAddress()),
+ id(new PhabricatorTextEditField())
+ ->setKey('port')
+ ->setLabel(pht('Port'))
+ ->setDescription(pht('Port of the service.'))
+ ->setTransactionType(AlmanacInterfacePortTransaction::TRANSACTIONTYPE)
+ ->setIsRequired(true)
+ ->setValue($object->getPort()),
+ );
+ }
+
+}
diff --git a/src/applications/almanac/xaction/AlmanacInterfaceAddressTransaction.php b/src/applications/almanac/xaction/AlmanacInterfaceAddressTransaction.php
--- a/src/applications/almanac/xaction/AlmanacInterfaceAddressTransaction.php
+++ b/src/applications/almanac/xaction/AlmanacInterfaceAddressTransaction.php
@@ -1,7 +1,7 @@
<?php
final class AlmanacInterfaceAddressTransaction
- extends AlmanacNetworkTransactionType {
+ extends AlmanacInterfaceTransactionType {
const TRANSACTIONTYPE = 'almanac:interface:address';
diff --git a/src/applications/almanac/xaction/AlmanacInterfaceDeviceTransaction.php b/src/applications/almanac/xaction/AlmanacInterfaceDeviceTransaction.php
--- a/src/applications/almanac/xaction/AlmanacInterfaceDeviceTransaction.php
+++ b/src/applications/almanac/xaction/AlmanacInterfaceDeviceTransaction.php
@@ -1,7 +1,7 @@
<?php
final class AlmanacInterfaceDeviceTransaction
- extends AlmanacNetworkTransactionType {
+ extends AlmanacInterfaceTransactionType {
const TRANSACTIONTYPE = 'almanac:interface:device';
@@ -24,7 +24,8 @@
public function validateTransactions($object, array $xactions) {
$errors = array();
- if ($this->isEmptyTextTransaction($object->getAddress(), $xactions)) {
+ $device_phid = $object->getDevicePHID();
+ if ($this->isEmptyTextTransaction($device_phid, $xactions)) {
$errors[] = $this->newRequiredError(
pht('Interfaces must have a device.'));
}
@@ -52,6 +53,19 @@
$xaction);
continue;
}
+
+ $device = head($devices);
+ $can_edit = PhabricatorPolicyFilter::hasCapability(
+ $this->getActor(),
+ $device,
+ PhabricatorPolicyCapability::CAN_EDIT);
+ if (!$can_edit) {
+ $errors[] = $this->newInvalidError(
+ pht(
+ 'You can not attach an interface to a device which you do not '.
+ 'have permission to edit.'));
+ continue;
+ }
}
return $errors;
diff --git a/src/applications/almanac/xaction/AlmanacInterfaceNetworkTransaction.php b/src/applications/almanac/xaction/AlmanacInterfaceNetworkTransaction.php
--- a/src/applications/almanac/xaction/AlmanacInterfaceNetworkTransaction.php
+++ b/src/applications/almanac/xaction/AlmanacInterfaceNetworkTransaction.php
@@ -1,7 +1,7 @@
<?php
final class AlmanacInterfaceNetworkTransaction
- extends AlmanacNetworkTransactionType {
+ extends AlmanacInterfaceTransactionType {
const TRANSACTIONTYPE = 'almanac:interface:network';
diff --git a/src/applications/almanac/xaction/AlmanacInterfacePortTransaction.php b/src/applications/almanac/xaction/AlmanacInterfacePortTransaction.php
--- a/src/applications/almanac/xaction/AlmanacInterfacePortTransaction.php
+++ b/src/applications/almanac/xaction/AlmanacInterfacePortTransaction.php
@@ -1,7 +1,7 @@
<?php
final class AlmanacInterfacePortTransaction
- extends AlmanacNetworkTransactionType {
+ extends AlmanacInterfaceTransactionType {
const TRANSACTIONTYPE = 'almanac:interface:port';
@@ -30,7 +30,7 @@
public function validateTransactions($object, array $xactions) {
$errors = array();
- if ($this->isEmptyTextTransaction($object->getName(), $xactions)) {
+ if ($this->isEmptyTextTransaction($object->getPort(), $xactions)) {
$errors[] = $this->newRequiredError(
pht('Interfaces must have a port number.'));
}

File Metadata

Mime Type
text/plain
Expires
Sat, Mar 8, 10:39 PM (1 w, 2 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7384766
Default Alt Text
D19324.id46230.diff (15 KB)

Event Timeline