2014-10-03 14:52:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class AlmanacDevice
|
|
|
|
extends AlmanacDAO
|
2014-11-06 00:28:36 +01:00
|
|
|
implements
|
|
|
|
PhabricatorPolicyInterface,
|
|
|
|
PhabricatorCustomFieldInterface,
|
|
|
|
PhabricatorApplicationTransactionInterface,
|
2014-11-06 00:30:00 +01:00
|
|
|
PhabricatorProjectInterface,
|
2014-11-11 17:20:08 +01:00
|
|
|
PhabricatorSSHPublicKeyInterface,
|
2014-11-06 00:28:36 +01:00
|
|
|
AlmanacPropertyInterface {
|
2014-10-03 14:52:41 +02:00
|
|
|
|
|
|
|
protected $name;
|
2014-10-17 14:02:14 +02:00
|
|
|
protected $nameIndex;
|
|
|
|
protected $mailKey;
|
|
|
|
protected $viewPolicy;
|
|
|
|
protected $editPolicy;
|
|
|
|
|
2014-11-06 00:28:36 +01:00
|
|
|
private $customFields = self::ATTACHABLE;
|
|
|
|
private $almanacProperties = self::ATTACHABLE;
|
|
|
|
|
2014-10-17 14:02:14 +02:00
|
|
|
public static function initializeNewDevice() {
|
|
|
|
return id(new AlmanacDevice())
|
|
|
|
->setViewPolicy(PhabricatorPolicies::POLICY_USER)
|
2014-11-16 22:54:11 +01:00
|
|
|
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)
|
|
|
|
->attachAlmanacProperties(array());
|
2014-10-17 14:02:14 +02:00
|
|
|
}
|
2014-10-03 14:52:41 +02:00
|
|
|
|
|
|
|
public function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_AUX_PHID => true,
|
|
|
|
self::CONFIG_COLUMN_SCHEMA => array(
|
2014-10-17 14:02:14 +02:00
|
|
|
'name' => 'text128',
|
|
|
|
'nameIndex' => 'bytes12',
|
2014-10-17 14:06:54 +02:00
|
|
|
'mailKey' => 'bytes20',
|
2014-10-17 14:02:14 +02:00
|
|
|
),
|
|
|
|
self::CONFIG_KEY_SCHEMA => array(
|
|
|
|
'key_name' => array(
|
|
|
|
'columns' => array('nameIndex'),
|
|
|
|
'unique' => true,
|
|
|
|
),
|
|
|
|
'key_nametext' => array(
|
|
|
|
'columns' => array('name'),
|
|
|
|
),
|
2014-10-03 14:52:41 +02:00
|
|
|
),
|
|
|
|
) + parent::getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generatePHID() {
|
|
|
|
return PhabricatorPHID::generateNewPHID(AlmanacDevicePHIDType::TYPECONST);
|
|
|
|
}
|
|
|
|
|
2014-10-17 14:02:14 +02:00
|
|
|
public function save() {
|
|
|
|
AlmanacNames::validateServiceOrDeviceName($this->getName());
|
|
|
|
|
|
|
|
$this->nameIndex = PhabricatorHash::digestForIndex($this->getName());
|
|
|
|
|
|
|
|
if (!$this->mailKey) {
|
|
|
|
$this->mailKey = Filesystem::readRandomCharacters(20);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI() {
|
|
|
|
return '/almanac/device/view/'.$this->getName().'/';
|
|
|
|
}
|
|
|
|
|
2014-10-03 14:52:41 +02:00
|
|
|
|
2014-11-06 00:28:36 +01:00
|
|
|
/* -( AlmanacPropertyInterface )------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function attachAlmanacProperties(array $properties) {
|
|
|
|
assert_instances_of($properties, 'AlmanacProperty');
|
|
|
|
$this->almanacProperties = mpull($properties, null, 'getFieldName');
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAlmanacProperties() {
|
|
|
|
return $this->assertAttached($this->almanacProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAlmanacProperty($key) {
|
|
|
|
$this->assertAttached($this->almanacProperties);
|
|
|
|
return isset($this->almanacProperties[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAlmanacProperty($key) {
|
|
|
|
return $this->assertAttachedKey($this->almanacProperties, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAlmanacPropertyValue($key, $default = null) {
|
|
|
|
if ($this->hasAlmanacProperty($key)) {
|
|
|
|
return $this->getAlmanacProperty($key)->getFieldValue();
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-03 14:52:41 +02:00
|
|
|
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
2014-10-17 14:02:14 +02:00
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
2014-10-03 14:52:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPolicy($capability) {
|
|
|
|
switch ($capability) {
|
|
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
2014-10-17 14:02:14 +02:00
|
|
|
return $this->getViewPolicy();
|
|
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
|
|
return $this->getEditPolicy();
|
2014-10-03 14:52:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function describeAutomaticCapability($capability) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-11-06 00:28:36 +01:00
|
|
|
|
|
|
|
/* -( PhabricatorCustomFieldInterface )------------------------------------ */
|
|
|
|
|
|
|
|
|
|
|
|
public function getCustomFieldSpecificationForRole($role) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCustomFieldBaseClass() {
|
|
|
|
return 'AlmanacCustomField';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCustomFields() {
|
|
|
|
return $this->assertAttached($this->customFields);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attachCustomFields(PhabricatorCustomFieldAttachment $fields) {
|
|
|
|
$this->customFields = $fields;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getApplicationTransactionEditor() {
|
|
|
|
return new AlmanacDeviceEditor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getApplicationTransactionObject() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getApplicationTransactionTemplate() {
|
|
|
|
return new AlmanacDeviceTransaction();
|
|
|
|
}
|
|
|
|
|
2014-11-11 17:20:08 +01:00
|
|
|
|
|
|
|
/* -( PhabricatorSSHPublicKeyInterface )----------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getSSHPublicKeyManagementURI(PhabricatorUser $viewer) {
|
|
|
|
return $this->getURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-03 14:52:41 +02:00
|
|
|
}
|