Path: blob/master/src/applications/almanac/storage/AlmanacInterface.php
12256 views
<?php12final class AlmanacInterface3extends AlmanacDAO4implements5PhabricatorPolicyInterface,6PhabricatorDestructibleInterface,7PhabricatorExtendedPolicyInterface,8PhabricatorApplicationTransactionInterface,9PhabricatorConduitResultInterface {1011protected $devicePHID;12protected $networkPHID;13protected $address;14protected $port;1516private $device = self::ATTACHABLE;17private $network = self::ATTACHABLE;1819public static function initializeNewInterface() {20return id(new AlmanacInterface());21}2223protected function getConfiguration() {24return array(25self::CONFIG_AUX_PHID => true,26self::CONFIG_COLUMN_SCHEMA => array(27'address' => 'text64',28'port' => 'uint32',29),30self::CONFIG_KEY_SCHEMA => array(31'key_location' => array(32'columns' => array('networkPHID', 'address', 'port'),33),34'key_device' => array(35'columns' => array('devicePHID'),36),37'key_unique' => array(38'columns' => array('devicePHID', 'networkPHID', 'address', 'port'),39'unique' => true,40),41),42) + parent::getConfiguration();43}4445public function generatePHID() {46return PhabricatorPHID::generateNewPHID(47AlmanacInterfacePHIDType::TYPECONST);48}4950public function getDevice() {51return $this->assertAttached($this->device);52}5354public function attachDevice(AlmanacDevice $device) {55$this->device = $device;56return $this;57}5859public function getNetwork() {60return $this->assertAttached($this->network);61}6263public function attachNetwork(AlmanacNetwork $network) {64$this->network = $network;65return $this;66}6768public function toAddress() {69return AlmanacAddress::newFromParts(70$this->getNetworkPHID(),71$this->getAddress(),72$this->getPort());73}7475public function getAddressHash() {76return $this->toAddress()->toHash();77}7879public function renderDisplayAddress() {80return $this->getAddress().':'.$this->getPort();81}8283public function loadIsInUse() {84$binding = id(new AlmanacBindingQuery())85->setViewer(PhabricatorUser::getOmnipotentUser())86->withInterfacePHIDs(array($this->getPHID()))87->setLimit(1)88->executeOne();8990return (bool)$binding;91}929394/* -( PhabricatorPolicyInterface )----------------------------------------- */959697public function getCapabilities() {98return array(99PhabricatorPolicyCapability::CAN_VIEW,100PhabricatorPolicyCapability::CAN_EDIT,101);102}103104public function getPolicy($capability) {105return $this->getDevice()->getPolicy($capability);106}107108public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {109return $this->getDevice()->hasAutomaticCapability($capability, $viewer);110}111112public function describeAutomaticCapability($capability) {113$notes = array(114pht('An interface inherits the policies of the device it belongs to.'),115pht(116'You must be able to view the network an interface resides on to '.117'view the interface.'),118);119120return $notes;121}122123124/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */125126127public function getExtendedPolicy($capability, PhabricatorUser $viewer) {128switch ($capability) {129case PhabricatorPolicyCapability::CAN_EDIT:130if ($this->getDevice()->isClusterDevice()) {131return array(132array(133new PhabricatorAlmanacApplication(),134AlmanacManageClusterServicesCapability::CAPABILITY,135),136);137}138break;139}140141return array();142}143144145/* -( PhabricatorDestructibleInterface )----------------------------------- */146147148public function destroyObjectPermanently(149PhabricatorDestructionEngine $engine) {150151$bindings = id(new AlmanacBindingQuery())152->setViewer($engine->getViewer())153->withInterfacePHIDs(array($this->getPHID()))154->execute();155foreach ($bindings as $binding) {156$engine->destroyObject($binding);157}158159$this->delete();160}161162163/* -( PhabricatorApplicationTransactionInterface )------------------------- */164165166public function getApplicationTransactionEditor() {167return new AlmanacInterfaceEditor();168}169170public function getApplicationTransactionTemplate() {171return new AlmanacInterfaceTransaction();172}173174175/* -( PhabricatorConduitResultInterface )---------------------------------- */176177178public function getFieldSpecificationsForConduit() {179return array(180id(new PhabricatorConduitSearchFieldSpecification())181->setKey('devicePHID')182->setType('phid')183->setDescription(pht('The device the interface is on.')),184id(new PhabricatorConduitSearchFieldSpecification())185->setKey('networkPHID')186->setType('phid')187->setDescription(pht('The network the interface is part of.')),188id(new PhabricatorConduitSearchFieldSpecification())189->setKey('address')190->setType('string')191->setDescription(pht('The address of the interface.')),192id(new PhabricatorConduitSearchFieldSpecification())193->setKey('port')194->setType('int')195->setDescription(pht('The port number of the interface.')),196);197}198199public function getFieldValuesForConduit() {200return array(201'devicePHID' => $this->getDevicePHID(),202'networkPHID' => $this->getNetworkPHID(),203'address' => (string)$this->getAddress(),204'port' => (int)$this->getPort(),205);206}207208public function getConduitSearchAttachments() {209return array();210}211212}213214215