Changeset View
Changeset View
Standalone View
Standalone View
src/applications/policy/rule/PhabricatorPolicyRule.php
<?php | <?php | ||||
/** | /** | ||||
* @task objectpolicy Implementing Object Policies | * @task objectpolicy Implementing Object Policies | ||||
*/ | */ | ||||
abstract class PhabricatorPolicyRule extends Phobject { | abstract class PhabricatorPolicyRule extends Phobject { | ||||
const CONTROL_TYPE_TEXT = 'text'; | const CONTROL_TYPE_TEXT = 'text'; | ||||
const CONTROL_TYPE_SELECT = 'select'; | const CONTROL_TYPE_SELECT = 'select'; | ||||
const CONTROL_TYPE_TOKENIZER = 'tokenizer'; | const CONTROL_TYPE_TOKENIZER = 'tokenizer'; | ||||
const CONTROL_TYPE_NONE = 'none'; | const CONTROL_TYPE_NONE = 'none'; | ||||
abstract public function getRuleDescription(); | abstract public function getRuleDescription(); | ||||
abstract public function applyRule( | |||||
PhabricatorUser $viewer, | |||||
$value, | |||||
PhabricatorPolicyInterface $object); | |||||
public function willApplyRules( | public function willApplyRules( | ||||
PhabricatorUser $viewer, | PhabricatorUser $viewer, | ||||
array $values, | array $values, | ||||
array $objects) { | array $objects) { | ||||
return; | return; | ||||
} | } | ||||
abstract public function applyRule( | |||||
PhabricatorUser $viewer, | |||||
$value, | |||||
PhabricatorPolicyInterface $object); | |||||
public function getValueControlType() { | public function getValueControlType() { | ||||
return self::CONTROL_TYPE_TEXT; | return self::CONTROL_TYPE_TEXT; | ||||
} | } | ||||
public function getValueControlTemplate() { | public function getValueControlTemplate() { | ||||
return null; | return null; | ||||
} | } | ||||
/** | /** | ||||
* Return `true` if this rule can be applied to the given object. | * Return `true` if this rule can be applied to the given object. | ||||
* | * | ||||
* Some policy rules may only operation on certain kinds of objects. For | * Some policy rules may only operation on certain kinds of objects. For | ||||
* example, a "task author" rule | * example, a "task author" rule. | ||||
epriestley: Maybe "...can only operate on tasks". | |||||
*/ | */ | ||||
public function canApplyToObject(PhabricatorPolicyInterface $object) { | public function canApplyToObject(PhabricatorPolicyInterface $object) { | ||||
return true; | return true; | ||||
} | } | ||||
protected function getDatasourceTemplate( | protected function getDatasourceTemplate( | ||||
PhabricatorTypeaheadDatasource $datasource) { | PhabricatorTypeaheadDatasource $datasource) { | ||||
return array( | return array( | ||||
'markup' => new AphrontTokenizerTemplateView(), | 'markup' => new AphrontTokenizerTemplateView(), | ||||
'uri' => $datasource->getDatasourceURI(), | 'uri' => $datasource->getDatasourceURI(), | ||||
'placeholder' => $datasource->getPlaceholderText(), | 'placeholder' => $datasource->getPlaceholderText(), | ||||
'browseURI' => $datasource->getBrowseURI(), | 'browseURI' => $datasource->getBrowseURI(), | ||||
); | ); | ||||
} | } | ||||
public function getRuleOrder() { | public function getRuleOrder() { | ||||
return 500; | return 500; | ||||
} | } | ||||
public function getValueForStorage($value) { | public function getValueForStorage($value) { | ||||
return $value; | return $value; | ||||
} | } | ||||
public function getValueForDisplay(PhabricatorUser $viewer, $value) { | public function getValueForDisplay(PhabricatorUser $viewer, $value) { | ||||
return $value; | return $value; | ||||
} | } | ||||
public function getRequiredHandlePHIDsForSummary($value) { | public function getRequiredHandlePHIDsForSummary($value) { | ||||
$phids = array(); | $phids = array(); | ||||
switch ($this->getValueControlType()) { | switch ($this->getValueControlType()) { | ||||
case self::CONTROL_TYPE_TOKENIZER: | case self::CONTROL_TYPE_TOKENIZER: | ||||
$phids = $value; | $phids = $value; | ||||
break; | break; | ||||
case self::CONTROL_TYPE_TEXT: | case self::CONTROL_TYPE_TEXT: | ||||
case self::CONTROL_TYPE_SELECT: | case self::CONTROL_TYPE_SELECT: | ||||
case self::CONTROL_TYPE_NONE: | case self::CONTROL_TYPE_NONE: | ||||
default: | default: | ||||
if (phid_get_type($value) != | if (phid_get_type($value) != | ||||
PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) { | PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) { | ||||
$phids = array($value); | $phids = array($value); | ||||
} else { | } else { | ||||
$phids = array(); | $phids = array(); | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
return $phids; | return $phids; | ||||
} | } | ||||
/** | /** | ||||
* Return true if the given value creates a rule with a meaningful effect. | * Return `true` if the given value creates a rule with a meaningful effect. | ||||
* An example of a rule with no meaningful effect is a "users" rule with no | * An example of a rule with no meaningful effect is a "users" rule with no | ||||
* users specified. | * users specified. | ||||
* | * | ||||
* @return bool True if the value creates a meaningful rule. | * @return bool True if the value creates a meaningful rule. | ||||
*/ | */ | ||||
public function ruleHasEffect($value) { | public function ruleHasEffect($value) { | ||||
return true; | return true; | ||||
} | } | ||||
Show All 28 Lines | public static function passTransactionHintToRule( | ||||
PhabricatorPolicyInterface $object, | PhabricatorPolicyInterface $object, | ||||
PhabricatorPolicyRule $rule, | PhabricatorPolicyRule $rule, | ||||
$hint) { | $hint) { | ||||
$cache = PhabricatorCaches::getRequestCache(); | $cache = PhabricatorCaches::getRequestCache(); | ||||
$cache->setKey(self::getObjectPolicyCacheKey($object, $rule), $hint); | $cache->setKey(self::getObjectPolicyCacheKey($object, $rule), $hint); | ||||
} | } | ||||
protected function getTransactionHint( | final protected function getTransactionHint( | ||||
PhabricatorPolicyInterface $object) { | PhabricatorPolicyInterface $object) { | ||||
$cache = PhabricatorCaches::getRequestCache(); | $cache = PhabricatorCaches::getRequestCache(); | ||||
return $cache->getKey(self::getObjectPolicyCacheKey($object, $this)); | return $cache->getKey(self::getObjectPolicyCacheKey($object, $this)); | ||||
} | } | ||||
private static function getObjectPolicyCacheKey( | private static function getObjectPolicyCacheKey( | ||||
PhabricatorPolicyInterface $object, | PhabricatorPolicyInterface $object, | ||||
Show All 16 Lines | /* -( Implementing Object Policies )--------------------------------------- */ | ||||
* | * | ||||
* @return string Unique identifier for this rule. | * @return string Unique identifier for this rule. | ||||
* @task objectpolicy | * @task objectpolicy | ||||
*/ | */ | ||||
public function getObjectPolicyKey() { | public function getObjectPolicyKey() { | ||||
return null; | return null; | ||||
} | } | ||||
public function getObjectPolicyFullKey() { | final public function getObjectPolicyFullKey() { | ||||
$key = $this->getObjectPolicyKey(); | $key = $this->getObjectPolicyKey(); | ||||
if (!$key) { | if (!$key) { | ||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
'This policy rule (of class "%s") does not have an associated '. | 'This policy rule (of class "%s") does not have an associated '. | ||||
'object policy key.', | 'object policy key.', | ||||
get_class($this))); | get_class($this))); | ||||
Show All 22 Lines |
Maybe "...can only operate on tasks".