[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/almanac/editor/ -> AlmanacDeviceEditor.php (source)

   1  <?php
   2  
   3  final class AlmanacDeviceEditor
   4    extends PhabricatorApplicationTransactionEditor {
   5  
   6    public function getEditorApplicationClass() {
   7      return 'PhabricatorAlmanacApplication';
   8    }
   9  
  10    public function getEditorObjectsDescription() {
  11      return pht('Almanac Device');
  12    }
  13  
  14    public function getTransactionTypes() {
  15      $types = parent::getTransactionTypes();
  16  
  17      $types[] = AlmanacDeviceTransaction::TYPE_NAME;
  18      $types[] = AlmanacDeviceTransaction::TYPE_INTERFACE;
  19      $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY;
  20      $types[] = PhabricatorTransactions::TYPE_EDIT_POLICY;
  21  
  22      return $types;
  23    }
  24  
  25    protected function getCustomTransactionOldValue(
  26      PhabricatorLiskDAO $object,
  27      PhabricatorApplicationTransaction $xaction) {
  28      switch ($xaction->getTransactionType()) {
  29        case AlmanacDeviceTransaction::TYPE_NAME:
  30          return $object->getName();
  31      }
  32  
  33      return parent::getCustomTransactionOldValue($object, $xaction);
  34    }
  35  
  36    protected function getCustomTransactionNewValue(
  37      PhabricatorLiskDAO $object,
  38      PhabricatorApplicationTransaction $xaction) {
  39  
  40      switch ($xaction->getTransactionType()) {
  41        case AlmanacDeviceTransaction::TYPE_NAME:
  42        case AlmanacDeviceTransaction::TYPE_INTERFACE:
  43          return $xaction->getNewValue();
  44      }
  45  
  46      return parent::getCustomTransactionNewValue($object, $xaction);
  47    }
  48  
  49    protected function applyCustomInternalTransaction(
  50      PhabricatorLiskDAO $object,
  51      PhabricatorApplicationTransaction $xaction) {
  52  
  53      switch ($xaction->getTransactionType()) {
  54        case AlmanacDeviceTransaction::TYPE_NAME:
  55          $object->setName($xaction->getNewValue());
  56          return;
  57        case AlmanacDeviceTransaction::TYPE_INTERFACE:
  58        case PhabricatorTransactions::TYPE_VIEW_POLICY:
  59        case PhabricatorTransactions::TYPE_EDIT_POLICY:
  60        case PhabricatorTransactions::TYPE_EDGE:
  61          return;
  62      }
  63  
  64      return parent::applyCustomInternalTransaction($object, $xaction);
  65    }
  66  
  67    protected function applyCustomExternalTransaction(
  68      PhabricatorLiskDAO $object,
  69      PhabricatorApplicationTransaction $xaction) {
  70  
  71      switch ($xaction->getTransactionType()) {
  72        case AlmanacDeviceTransaction::TYPE_NAME:
  73        case PhabricatorTransactions::TYPE_VIEW_POLICY:
  74        case PhabricatorTransactions::TYPE_EDIT_POLICY:
  75        case PhabricatorTransactions::TYPE_EDGE:
  76          return;
  77        case AlmanacDeviceTransaction::TYPE_INTERFACE:
  78          $old = $xaction->getOldValue();
  79          if ($old) {
  80            $interface = id(new AlmanacInterfaceQuery())
  81              ->setViewer($this->requireActor())
  82              ->withIDs(array($old['id']))
  83              ->executeOne();
  84            if (!$interface) {
  85              throw new Exception(pht('Unable to load interface!'));
  86            }
  87          } else {
  88            $interface = AlmanacInterface::initializeNewInterface()
  89              ->setDevicePHID($object->getPHID());
  90          }
  91  
  92          $new = $xaction->getNewValue();
  93          if ($new) {
  94            $interface
  95              ->setNetworkPHID($new['networkPHID'])
  96              ->setAddress($new['address'])
  97              ->setPort((int)$new['port'])
  98              ->save();
  99          } else {
 100            $interface->delete();
 101          }
 102          return;
 103      }
 104  
 105      return parent::applyCustomExternalTransaction($object, $xaction);
 106    }
 107  
 108    protected function validateTransaction(
 109      PhabricatorLiskDAO $object,
 110      $type,
 111      array $xactions) {
 112  
 113      $errors = parent::validateTransaction($object, $type, $xactions);
 114  
 115      switch ($type) {
 116        case AlmanacDeviceTransaction::TYPE_NAME:
 117          $missing = $this->validateIsEmptyTextField(
 118            $object->getName(),
 119            $xactions);
 120  
 121          if ($missing) {
 122            $error = new PhabricatorApplicationTransactionValidationError(
 123              $type,
 124              pht('Required'),
 125              pht('Device name is required.'),
 126              nonempty(last($xactions), null));
 127  
 128            $error->setIsMissingFieldError(true);
 129            $errors[] = $error;
 130          } else {
 131            foreach ($xactions as $xaction) {
 132              $message = null;
 133              $name = $xaction->getNewValue();
 134  
 135              try {
 136                AlmanacNames::validateServiceOrDeviceName($name);
 137              } catch (Exception $ex) {
 138                $message = $ex->getMessage();
 139              }
 140  
 141              if ($message !== null) {
 142                $error = new PhabricatorApplicationTransactionValidationError(
 143                  $type,
 144                  pht('Invalid'),
 145                  $message,
 146                  $xaction);
 147                $errors[] = $error;
 148              }
 149            }
 150          }
 151  
 152          if ($xactions) {
 153            $duplicate = id(new AlmanacDeviceQuery())
 154              ->setViewer(PhabricatorUser::getOmnipotentUser())
 155              ->withNames(array(last($xactions)->getNewValue()))
 156              ->executeOne();
 157            if ($duplicate && ($duplicate->getID() != $object->getID())) {
 158              $error = new PhabricatorApplicationTransactionValidationError(
 159                $type,
 160                pht('Not Unique'),
 161                pht('Almanac devices must have unique names.'),
 162                last($xactions));
 163              $errors[] = $error;
 164            }
 165          }
 166  
 167          break;
 168        case AlmanacDeviceTransaction::TYPE_INTERFACE:
 169          // We want to make sure that all the affected networks are visible to
 170          // the actor, any edited interfaces exist, and that the actual address
 171          // components are valid.
 172  
 173          $network_phids = array();
 174          foreach ($xactions as $xaction) {
 175            $old = $xaction->getOldValue();
 176            $new = $xaction->getNewValue();
 177            if ($old) {
 178              $network_phids[] = $old['networkPHID'];
 179            }
 180            if ($new) {
 181              $network_phids[] = $new['networkPHID'];
 182  
 183              $address = $new['address'];
 184              if (!strlen($address)) {
 185                $error = new PhabricatorApplicationTransactionValidationError(
 186                  $type,
 187                  pht('Invalid'),
 188                  pht('Interfaces must have an address.'),
 189                  $xaction);
 190                $errors[] = $error;
 191              } else {
 192                // TODO: Validate addresses, but IPv6 addresses are not trival
 193                // to validate.
 194              }
 195  
 196              $port = $new['port'];
 197              if (!strlen($port)) {
 198                $error = new PhabricatorApplicationTransactionValidationError(
 199                  $type,
 200                  pht('Invalid'),
 201                  pht('Interfaces must have a port.'),
 202                  $xaction);
 203                $errors[] = $error;
 204              } else if ((int)$port < 1 || (int)$port > 65535) {
 205                $error = new PhabricatorApplicationTransactionValidationError(
 206                  $type,
 207                  pht('Invalid'),
 208                  pht(
 209                    'Port numbers must be between 1 and 65535, inclusive.'),
 210                  $xaction);
 211                $errors[] = $error;
 212              }
 213            }
 214          }
 215  
 216          if ($network_phids) {
 217            $networks = id(new AlmanacNetworkQuery())
 218              ->setViewer($this->requireActor())
 219              ->withPHIDs($network_phids)
 220              ->execute();
 221            $networks = mpull($networks, null, 'getPHID');
 222          } else {
 223            $networks = array();
 224          }
 225  
 226          $addresses = array();
 227          foreach ($xactions as $xaction) {
 228            $old = $xaction->getOldValue();
 229            if ($old) {
 230              $network = idx($networks, $old['networkPHID']);
 231              if (!$network) {
 232                $error = new PhabricatorApplicationTransactionValidationError(
 233                  $type,
 234                  pht('Invalid'),
 235                  pht(
 236                    'You can not edit an interface which belongs to a '.
 237                    'nonexistent or restricted network.'),
 238                  $xaction);
 239                $errors[] = $error;
 240              }
 241  
 242              $addresses[] = $old['id'];
 243            }
 244  
 245            $new = $xaction->getNewValue();
 246            if ($new) {
 247              $network = idx($networks, $new['networkPHID']);
 248              if (!$network) {
 249                $error = new PhabricatorApplicationTransactionValidationError(
 250                  $type,
 251                  pht('Invalid'),
 252                  pht(
 253                    'You can not add an interface on a nonexistent or '.
 254                    'restricted network.'),
 255                  $xaction);
 256                $errors[] = $error;
 257              }
 258            }
 259          }
 260  
 261          if ($addresses) {
 262            $interfaces = id(new AlmanacInterfaceQuery())
 263              ->setViewer($this->requireActor())
 264              ->withDevicePHIDs(array($object->getPHID()))
 265              ->withIDs($addresses)
 266              ->execute();
 267            $interfaces = mpull($interfaces, null, 'getID');
 268          } else {
 269            $interfaces = array();
 270          }
 271  
 272          foreach ($xactions as $xaction) {
 273            $old = $xaction->getOldValue();
 274            if ($old) {
 275              $interface = idx($interfaces, $old['id']);
 276              if (!$interface) {
 277                $error = new PhabricatorApplicationTransactionValidationError(
 278                  $type,
 279                  pht('Invalid'),
 280                  pht('You can not edit an invalid or restricted interface.'),
 281                  $xaction);
 282                $errors[] = $error;
 283              }
 284            }
 285          }
 286        break;
 287      }
 288  
 289      return $errors;
 290    }
 291  
 292  
 293  
 294  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1