mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-28 09:42:41 +01:00
3ef0721ada
Summary: Fixes T6956. Before this change, we called PhabricatorUser::getOmnipotentUser in the various delete methods to query the data. Now, we use $engine->getViewer(), since its always a good thing to have less calls to PhabricatorUser::getOmnipotentUser thrown around the codebase. I used the "codemod" tool to audit the existing calls to PhabricatorDestructorEngine (all of them) so ostensibly this gets all the spots. If I missed something though, its still going to work, so this change is very low risk. Test Plan: ./bin/remove destroy P1; visit P1 and get a 404 Reviewers: epriestley Reviewed By: epriestley Subscribers: Korvin, epriestley Maniphest Tasks: T6956 Differential Revision: https://secure.phabricator.com/D12866
234 lines
5.9 KiB
PHP
234 lines
5.9 KiB
PHP
<?php
|
|
|
|
final class AlmanacService
|
|
extends AlmanacDAO
|
|
implements
|
|
PhabricatorPolicyInterface,
|
|
PhabricatorCustomFieldInterface,
|
|
PhabricatorApplicationTransactionInterface,
|
|
PhabricatorProjectInterface,
|
|
AlmanacPropertyInterface,
|
|
PhabricatorDestructibleInterface {
|
|
|
|
protected $name;
|
|
protected $nameIndex;
|
|
protected $mailKey;
|
|
protected $viewPolicy;
|
|
protected $editPolicy;
|
|
protected $serviceClass;
|
|
protected $isLocked;
|
|
|
|
private $customFields = self::ATTACHABLE;
|
|
private $almanacProperties = self::ATTACHABLE;
|
|
private $bindings = self::ATTACHABLE;
|
|
private $serviceType = self::ATTACHABLE;
|
|
|
|
public static function initializeNewService() {
|
|
return id(new AlmanacService())
|
|
->setViewPolicy(PhabricatorPolicies::POLICY_USER)
|
|
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)
|
|
->attachAlmanacProperties(array())
|
|
->setIsLocked(0);
|
|
}
|
|
|
|
protected function getConfiguration() {
|
|
return array(
|
|
self::CONFIG_AUX_PHID => true,
|
|
self::CONFIG_COLUMN_SCHEMA => array(
|
|
'name' => 'text128',
|
|
'nameIndex' => 'bytes12',
|
|
'mailKey' => 'bytes20',
|
|
'serviceClass' => 'text64',
|
|
'isLocked' => 'bool',
|
|
),
|
|
self::CONFIG_KEY_SCHEMA => array(
|
|
'key_name' => array(
|
|
'columns' => array('nameIndex'),
|
|
'unique' => true,
|
|
),
|
|
'key_nametext' => array(
|
|
'columns' => array('name'),
|
|
),
|
|
'key_class' => array(
|
|
'columns' => array('serviceClass'),
|
|
),
|
|
),
|
|
) + parent::getConfiguration();
|
|
}
|
|
|
|
public function generatePHID() {
|
|
return PhabricatorPHID::generateNewPHID(AlmanacServicePHIDType::TYPECONST);
|
|
}
|
|
|
|
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/service/view/'.$this->getName().'/';
|
|
}
|
|
|
|
public function getBindings() {
|
|
return $this->assertAttached($this->bindings);
|
|
}
|
|
|
|
public function attachBindings(array $bindings) {
|
|
$this->bindings = $bindings;
|
|
return $this;
|
|
}
|
|
|
|
public function getServiceType() {
|
|
return $this->assertAttached($this->serviceType);
|
|
}
|
|
|
|
public function attachServiceType(AlmanacServiceType $type) {
|
|
$this->serviceType = $type;
|
|
return $this;
|
|
}
|
|
|
|
|
|
/* -( 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;
|
|
}
|
|
}
|
|
|
|
public function getAlmanacPropertyFieldSpecifications() {
|
|
return $this->getServiceType()->getFieldSpecifications();
|
|
}
|
|
|
|
|
|
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
|
|
|
|
|
public function getCapabilities() {
|
|
return array(
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
);
|
|
}
|
|
|
|
public function getPolicy($capability) {
|
|
switch ($capability) {
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
return $this->getViewPolicy();
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
if ($this->getIsLocked()) {
|
|
return PhabricatorPolicies::POLICY_NOONE;
|
|
} else {
|
|
return $this->getEditPolicy();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
|
|
return false;
|
|
}
|
|
|
|
public function describeAutomaticCapability($capability) {
|
|
switch ($capability) {
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
if ($this->getIsLocked()) {
|
|
return pht('This service is locked and can not be edited.');
|
|
}
|
|
break;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
/* -( 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 AlmanacServiceEditor();
|
|
}
|
|
|
|
public function getApplicationTransactionObject() {
|
|
return $this;
|
|
}
|
|
|
|
public function getApplicationTransactionTemplate() {
|
|
return new AlmanacServiceTransaction();
|
|
}
|
|
|
|
public function willRenderTimeline(
|
|
PhabricatorApplicationTransactionView $timeline,
|
|
AphrontRequest $request) {
|
|
|
|
return $timeline;
|
|
}
|
|
|
|
|
|
/* -( PhabricatorDestructibleInterface )----------------------------------- */
|
|
|
|
|
|
public function destroyObjectPermanently(
|
|
PhabricatorDestructionEngine $engine) {
|
|
|
|
$bindings = id(new AlmanacBindingQuery())
|
|
->setViewer($engine->getViewer())
|
|
->withServicePHIDs(array($this->getPHID()))
|
|
->execute();
|
|
foreach ($bindings as $binding) {
|
|
$engine->destroyObject($binding);
|
|
}
|
|
|
|
$this->delete();
|
|
}
|
|
|
|
}
|