Summary: The name of networks should be unique. Also adds support for exact-name queries for AlamanacNetworks. Test Plan: Applied migration with existing duplicates, saw networks renamed, attempted to add duplicates, got a nice error message. Reviewers: epriestley Reviewed By: epriestley Subscribers: Korvin, PHID-OPKG-gm6ozazyms6q6i22gyam Differential Revision: https://secure.phabricator.com/D19379
157 lines
3.6 KiB
PHP
157 lines
3.6 KiB
PHP
<?php
|
|
|
|
final class AlmanacNetwork
|
|
extends AlmanacDAO
|
|
implements
|
|
PhabricatorApplicationTransactionInterface,
|
|
PhabricatorPolicyInterface,
|
|
PhabricatorDestructibleInterface,
|
|
PhabricatorNgramsInterface,
|
|
PhabricatorConduitResultInterface {
|
|
|
|
protected $name;
|
|
protected $mailKey;
|
|
protected $viewPolicy;
|
|
protected $editPolicy;
|
|
|
|
public static function initializeNewNetwork() {
|
|
return id(new AlmanacNetwork())
|
|
->setViewPolicy(PhabricatorPolicies::POLICY_USER)
|
|
->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN);
|
|
}
|
|
|
|
protected function getConfiguration() {
|
|
return array(
|
|
self::CONFIG_AUX_PHID => true,
|
|
self::CONFIG_COLUMN_SCHEMA => array(
|
|
'name' => 'sort128',
|
|
'mailKey' => 'bytes20',
|
|
|
|
),
|
|
self::CONFIG_KEY_SCHEMA => array(
|
|
'key_name' => array(
|
|
'columns' => array('name'),
|
|
'unique' => true,
|
|
),
|
|
),
|
|
) + parent::getConfiguration();
|
|
}
|
|
|
|
public function generatePHID() {
|
|
return PhabricatorPHID::generateNewPHID(AlmanacNetworkPHIDType::TYPECONST);
|
|
}
|
|
|
|
public function save() {
|
|
if (!$this->mailKey) {
|
|
$this->mailKey = Filesystem::readRandomCharacters(20);
|
|
}
|
|
|
|
return parent::save();
|
|
}
|
|
|
|
public function getURI() {
|
|
return '/almanac/network/'.$this->getID().'/';
|
|
}
|
|
|
|
|
|
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
|
|
|
|
|
|
public function getApplicationTransactionEditor() {
|
|
return new AlmanacNetworkEditor();
|
|
}
|
|
|
|
public function getApplicationTransactionObject() {
|
|
return $this;
|
|
}
|
|
|
|
public function getApplicationTransactionTemplate() {
|
|
return new AlmanacNetworkTransaction();
|
|
}
|
|
|
|
public function willRenderTimeline(
|
|
PhabricatorApplicationTransactionView $timeline,
|
|
AphrontRequest $request) {
|
|
|
|
return $timeline;
|
|
}
|
|
|
|
|
|
/* -( 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:
|
|
return $this->getEditPolicy();
|
|
}
|
|
}
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
|
|
return false;
|
|
}
|
|
|
|
|
|
/* -( PhabricatorDestructibleInterface )----------------------------------- */
|
|
|
|
|
|
public function destroyObjectPermanently(
|
|
PhabricatorDestructionEngine $engine) {
|
|
|
|
$interfaces = id(new AlmanacInterfaceQuery())
|
|
->setViewer($engine->getViewer())
|
|
->withNetworkPHIDs(array($this->getPHID()))
|
|
->execute();
|
|
|
|
foreach ($interfaces as $interface) {
|
|
$engine->destroyObject($interface);
|
|
}
|
|
|
|
$this->delete();
|
|
}
|
|
|
|
|
|
/* -( PhabricatorNgramsInterface )----------------------------------------- */
|
|
|
|
|
|
public function newNgrams() {
|
|
return array(
|
|
id(new AlmanacNetworkNameNgrams())
|
|
->setValue($this->getName()),
|
|
);
|
|
}
|
|
|
|
|
|
/* -( PhabricatorConduitResultInterface )---------------------------------- */
|
|
|
|
|
|
public function getFieldSpecificationsForConduit() {
|
|
return array(
|
|
id(new PhabricatorConduitSearchFieldSpecification())
|
|
->setKey('name')
|
|
->setType('string')
|
|
->setDescription(pht('The name of the network.')),
|
|
);
|
|
}
|
|
|
|
public function getFieldValuesForConduit() {
|
|
return array(
|
|
'name' => $this->getName(),
|
|
);
|
|
}
|
|
|
|
public function getConduitSearchAttachments() {
|
|
return array();
|
|
}
|
|
|
|
}
|