Changeset View
Changeset View
Standalone View
Standalone View
src/applications/almanac/storage/AlmanacDevice.php
<?php | <?php | ||||
final class AlmanacDevice | final class AlmanacDevice | ||||
extends AlmanacDAO | extends AlmanacDAO | ||||
implements | implements | ||||
PhabricatorPolicyInterface, | PhabricatorPolicyInterface, | ||||
PhabricatorApplicationTransactionInterface, | PhabricatorApplicationTransactionInterface, | ||||
PhabricatorProjectInterface, | PhabricatorProjectInterface, | ||||
PhabricatorSSHPublicKeyInterface, | PhabricatorSSHPublicKeyInterface, | ||||
AlmanacPropertyInterface, | AlmanacPropertyInterface, | ||||
PhabricatorDestructibleInterface, | PhabricatorDestructibleInterface, | ||||
PhabricatorNgramsInterface, | PhabricatorNgramsInterface, | ||||
PhabricatorConduitResultInterface { | PhabricatorConduitResultInterface, | ||||
PhabricatorExtendedPolicyInterface { | |||||
protected $name; | protected $name; | ||||
protected $nameIndex; | protected $nameIndex; | ||||
protected $mailKey; | protected $mailKey; | ||||
protected $viewPolicy; | protected $viewPolicy; | ||||
protected $editPolicy; | protected $editPolicy; | ||||
protected $isLocked; | protected $isBoundToClusterService; | ||||
private $almanacProperties = self::ATTACHABLE; | private $almanacProperties = self::ATTACHABLE; | ||||
public static function initializeNewDevice() { | public static function initializeNewDevice() { | ||||
return id(new AlmanacDevice()) | return id(new AlmanacDevice()) | ||||
->setViewPolicy(PhabricatorPolicies::POLICY_USER) | ->setViewPolicy(PhabricatorPolicies::POLICY_USER) | ||||
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN) | ->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN) | ||||
->attachAlmanacProperties(array()) | ->attachAlmanacProperties(array()) | ||||
->setIsLocked(0); | ->setIsBoundToClusterService(0); | ||||
} | } | ||||
protected function getConfiguration() { | protected function getConfiguration() { | ||||
return array( | return array( | ||||
self::CONFIG_AUX_PHID => true, | self::CONFIG_AUX_PHID => true, | ||||
self::CONFIG_COLUMN_SCHEMA => array( | self::CONFIG_COLUMN_SCHEMA => array( | ||||
'name' => 'text128', | 'name' => 'text128', | ||||
'nameIndex' => 'bytes12', | 'nameIndex' => 'bytes12', | ||||
'mailKey' => 'bytes20', | 'mailKey' => 'bytes20', | ||||
'isLocked' => 'bool', | 'isBoundToClusterService' => 'bool', | ||||
), | ), | ||||
self::CONFIG_KEY_SCHEMA => array( | self::CONFIG_KEY_SCHEMA => array( | ||||
'key_name' => array( | 'key_name' => array( | ||||
'columns' => array('nameIndex'), | 'columns' => array('nameIndex'), | ||||
'unique' => true, | 'unique' => true, | ||||
), | ), | ||||
'key_nametext' => array( | 'key_nametext' => array( | ||||
'columns' => array('name'), | 'columns' => array('name'), | ||||
Show All 17 Lines | public function save() { | ||||
return parent::save(); | return parent::save(); | ||||
} | } | ||||
public function getURI() { | public function getURI() { | ||||
return '/almanac/device/view/'.$this->getName().'/'; | return '/almanac/device/view/'.$this->getName().'/'; | ||||
} | } | ||||
public function rebuildClusterBindingStatus() { | |||||
/** | |||||
* Find locked services which are bound to this device, updating the device | |||||
* lock flag if necessary. | |||||
* | |||||
* @return list<phid> List of locking service PHIDs. | |||||
*/ | |||||
public function rebuildDeviceLocks() { | |||||
$services = id(new AlmanacServiceQuery()) | $services = id(new AlmanacServiceQuery()) | ||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | ->setViewer(PhabricatorUser::getOmnipotentUser()) | ||||
->withDevicePHIDs(array($this->getPHID())) | ->withDevicePHIDs(array($this->getPHID())) | ||||
->withLocked(true) | |||||
->execute(); | ->execute(); | ||||
$locked = (bool)count($services); | $is_cluster = false; | ||||
foreach ($services as $service) { | |||||
if ($service->isClusterService()) { | |||||
$is_cluster = true; | |||||
break; | |||||
} | |||||
} | |||||
if ($locked != $this->getIsLocked()) { | if ($is_cluster != $this->getIsBoundToClusterService()) { | ||||
$this->setIsLocked((int)$locked); | $this->setIsBoundToClusterService((int)$is_cluster); | ||||
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | ||||
queryfx( | queryfx( | ||||
$this->establishConnection('w'), | $this->establishConnection('w'), | ||||
'UPDATE %T SET isLocked = %d WHERE id = %d', | 'UPDATE %T SET isBoundToClusterService = %d WHERE id = %d', | ||||
$this->getTableName(), | $this->getTableName(), | ||||
$this->getIsLocked(), | $this->getIsBoundToClusterService(), | ||||
$this->getID()); | $this->getID()); | ||||
unset($unguarded); | unset($unguarded); | ||||
} | } | ||||
return $this; | return $this; | ||||
} | } | ||||
public function isClusterDevice() { | |||||
return $this->getIsBoundToClusterService(); | |||||
} | |||||
/* -( AlmanacPropertyInterface )------------------------------------------- */ | /* -( AlmanacPropertyInterface )------------------------------------------- */ | ||||
public function attachAlmanacProperties(array $properties) { | public function attachAlmanacProperties(array $properties) { | ||||
assert_instances_of($properties, 'AlmanacProperty'); | assert_instances_of($properties, 'AlmanacProperty'); | ||||
$this->almanacProperties = mpull($properties, null, 'getFieldName'); | $this->almanacProperties = mpull($properties, null, 'getFieldName'); | ||||
return $this; | return $this; | ||||
Show All 39 Lines | public function getCapabilities() { | ||||
); | ); | ||||
} | } | ||||
public function getPolicy($capability) { | public function getPolicy($capability) { | ||||
switch ($capability) { | switch ($capability) { | ||||
case PhabricatorPolicyCapability::CAN_VIEW: | case PhabricatorPolicyCapability::CAN_VIEW: | ||||
return $this->getViewPolicy(); | return $this->getViewPolicy(); | ||||
case PhabricatorPolicyCapability::CAN_EDIT: | case PhabricatorPolicyCapability::CAN_EDIT: | ||||
if ($this->getIsLocked()) { | |||||
return PhabricatorPolicies::POLICY_NOONE; | |||||
} else { | |||||
return $this->getEditPolicy(); | return $this->getEditPolicy(); | ||||
} | } | ||||
} | } | ||||
} | |||||
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { | public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { | ||||
return false; | return false; | ||||
} | } | ||||
public function describeAutomaticCapability($capability) { | public function describeAutomaticCapability($capability) { | ||||
if ($capability === PhabricatorPolicyCapability::CAN_EDIT) { | return null; | ||||
if ($this->getIsLocked()) { | } | ||||
return pht( | |||||
'This device is bound to a locked service, so it can not '. | |||||
'be edited.'); | /* -( PhabricatorExtendedPolicyInterface )--------------------------------- */ | ||||
public function getExtendedPolicy($capability, PhabricatorUser $viewer) { | |||||
switch ($capability) { | |||||
case PhabricatorPolicyCapability::CAN_EDIT: | |||||
if ($this->isClusterDevice()) { | |||||
return array( | |||||
array( | |||||
new PhabricatorAlmanacApplication(), | |||||
AlmanacManageClusterServicesCapability::CAPABILITY, | |||||
), | |||||
); | |||||
} | } | ||||
break; | |||||
} | } | ||||
return null; | return array(); | ||||
} | } | ||||
/* -( PhabricatorApplicationTransactionInterface )------------------------- */ | /* -( PhabricatorApplicationTransactionInterface )------------------------- */ | ||||
public function getApplicationTransactionEditor() { | public function getApplicationTransactionEditor() { | ||||
return new AlmanacDeviceEditor(); | return new AlmanacDeviceEditor(); | ||||
▲ Show 20 Lines • Show All 85 Lines • Show Last 20 Lines |