Path: blob/master/src/applications/almanac/xaction/AlmanacBindingInterfaceTransaction.php
12256 views
<?php12final class AlmanacBindingInterfaceTransaction3extends AlmanacBindingTransactionType {45const TRANSACTIONTYPE = 'almanac:binding:interface';67public function generateOldValue($object) {8return $object->getInterfacePHID();9}1011public function applyInternalEffects($object, $value) {12$interface = $this->loadInterface($value);1314$object15->setDevicePHID($interface->getDevicePHID())16->setInterfacePHID($interface->getPHID());17}1819public function applyExternalEffects($object, $value) {2021// When we change which services a device is bound to, we need to22// recalculate whether it is a cluster device or not so we can tell if23// the "Can Manage Cluster Services" permission applies to it.2425$viewer = PhabricatorUser::getOmnipotentUser();26$interface_phids = array();2728$interface_phids[] = $this->getOldValue();29$interface_phids[] = $this->getNewValue();3031$interface_phids = array_filter($interface_phids);32$interface_phids = array_unique($interface_phids);3334$interfaces = id(new AlmanacInterfaceQuery())35->setViewer($viewer)36->withPHIDs($interface_phids)37->execute();3839$device_phids = array();40foreach ($interfaces as $interface) {41$device_phids[] = $interface->getDevicePHID();42}4344$device_phids = array_unique($device_phids);4546$devices = id(new AlmanacDeviceQuery())47->setViewer($viewer)48->withPHIDs($device_phids)49->execute();5051foreach ($devices as $device) {52$device->rebuildClusterBindingStatus();53}54}5556public function getTitle() {57if ($this->getOldValue() === null) {58return pht(59'%s set the interface for this binding to %s.',60$this->renderAuthor(),61$this->renderNewHandle());62} else if ($this->getNewValue() == null) {63return pht(64'%s removed the interface for this binding.',65$this->renderAuthor());66} else {67return pht(68'%s changed the interface for this binding from %s to %s.',69$this->renderAuthor(),70$this->renderOldHandle(),71$this->renderNewHandle());72}73}7475public function validateTransactions($object, array $xactions) {76$errors = array();7778$interface_phid = $object->getInterfacePHID();79if ($this->isEmptyTextTransaction($interface_phid, $xactions)) {80$errors[] = $this->newRequiredError(81pht('Bindings must specify an interface.'));82}8384foreach ($xactions as $xaction) {85$interface_phid = $xaction->getNewValue();8687$interface = $this->loadInterface($interface_phid);88if (!$interface) {89$errors[] = $this->newInvalidError(90pht(91'You can not bind a service to an invalid or restricted '.92'interface.'),93$xaction);94continue;95}9697$binding = id(new AlmanacBindingQuery())98->setViewer(PhabricatorUser::getOmnipotentUser())99->withServicePHIDs(array($object->getServicePHID()))100->withInterfacePHIDs(array($interface_phid))101->executeOne();102if ($binding && ($binding->getID() != $object->getID())) {103$errors[] = $this->newInvalidError(104pht(105'You can not bind a service to the same interface multiple '.106'times.'),107$xaction);108continue;109}110}111112return $errors;113}114115private function loadInterface($phid) {116return id(new AlmanacInterfaceQuery())117->setViewer($this->getActor())118->withPHIDs(array($phid))119->executeOne();120}121}122123124