Path: blob/master/src/applications/almanac/storage/AlmanacNamespace.php
12256 views
<?php12final class AlmanacNamespace3extends AlmanacDAO4implements5PhabricatorPolicyInterface,6PhabricatorApplicationTransactionInterface,7PhabricatorProjectInterface,8PhabricatorDestructibleInterface,9PhabricatorNgramsInterface,10PhabricatorConduitResultInterface {1112protected $name;13protected $nameIndex;14protected $viewPolicy;15protected $editPolicy;1617public static function initializeNewNamespace() {18return id(new self())19->setViewPolicy(PhabricatorPolicies::POLICY_USER)20->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN);21}2223protected function getConfiguration() {24return array(25self::CONFIG_AUX_PHID => true,26self::CONFIG_COLUMN_SCHEMA => array(27'name' => 'text128',28'nameIndex' => 'bytes12',29),30self::CONFIG_KEY_SCHEMA => array(31'key_nameindex' => array(32'columns' => array('nameIndex'),33'unique' => true,34),35'key_name' => array(36'columns' => array('name'),37),38),39) + parent::getConfiguration();40}4142public function getPHIDType() {43return AlmanacNamespacePHIDType::TYPECONST;44}4546public function save() {47AlmanacNames::validateName($this->getName());4849$this->nameIndex = PhabricatorHash::digestForIndex($this->getName());5051return parent::save();52}5354public function getURI() {55return urisprintf(56'/almanac/namespace/view/%s/',57$this->getName());58}5960public function getNameLength() {61return strlen($this->getName());62}6364/**65* Load the namespace which prevents use of an Almanac name, if one exists.66*/67public static function loadRestrictedNamespace(68PhabricatorUser $viewer,69$name) {7071// For a name like "x.y.z", produce a list of controlling namespaces like72// ("z", "y.x", "x.y.z").73$names = array();74$parts = explode('.', $name);75for ($ii = 0; $ii < count($parts); $ii++) {76$names[] = implode('.', array_slice($parts, -($ii + 1)));77}7879// Load all the possible controlling namespaces.80$namespaces = id(new AlmanacNamespaceQuery())81->setViewer(PhabricatorUser::getOmnipotentUser())82->withNames($names)83->execute();84if (!$namespaces) {85return null;86}8788// Find the "nearest" (longest) namespace that exists. If both89// "sub.domain.com" and "domain.com" exist, we only care about the policy90// on the former.91$namespaces = msort($namespaces, 'getNameLength');92$namespace = last($namespaces);9394$can_edit = PhabricatorPolicyFilter::hasCapability(95$viewer,96$namespace,97PhabricatorPolicyCapability::CAN_EDIT);98if ($can_edit) {99return null;100}101102return $namespace;103}104105106/* -( PhabricatorPolicyInterface )----------------------------------------- */107108109public function getCapabilities() {110return array(111PhabricatorPolicyCapability::CAN_VIEW,112PhabricatorPolicyCapability::CAN_EDIT,113);114}115116public function getPolicy($capability) {117switch ($capability) {118case PhabricatorPolicyCapability::CAN_VIEW:119return $this->getViewPolicy();120case PhabricatorPolicyCapability::CAN_EDIT:121return $this->getEditPolicy();122}123}124125public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {126return false;127}128129130/* -( PhabricatorApplicationTransactionInterface )------------------------- */131132133public function getApplicationTransactionEditor() {134return new AlmanacNamespaceEditor();135}136137public function getApplicationTransactionTemplate() {138return new AlmanacNamespaceTransaction();139}140141142/* -( PhabricatorDestructibleInterface )----------------------------------- */143144145public function destroyObjectPermanently(146PhabricatorDestructionEngine $engine) {147$this->delete();148}149150151/* -( PhabricatorNgramsInterface )----------------------------------------- */152153154public function newNgrams() {155return array(156id(new AlmanacNamespaceNameNgrams())157->setValue($this->getName()),158);159}160161162/* -( PhabricatorConduitResultInterface )---------------------------------- */163164165public function getFieldSpecificationsForConduit() {166return array(167id(new PhabricatorConduitSearchFieldSpecification())168->setKey('name')169->setType('string')170->setDescription(pht('The name of the namespace.')),171);172}173174public function getFieldValuesForConduit() {175return array(176'name' => $this->getName(),177);178}179180public function getConduitSearchAttachments() {181return array();182}183184185}186187188