3 final class PhabricatorPolicyEditEngineExtension
4 extends PhabricatorEditEngineExtension
{
6 const EXTENSIONKEY
= 'policy.policy';
8 public function getExtensionPriority() {
12 public function isExtensionEnabled() {
16 public function getExtensionName() {
17 return pht('Policies');
20 public function supportsObject(
21 PhabricatorEditEngine
$engine,
22 PhabricatorApplicationTransactionInterface
$object) {
23 return ($object instanceof PhabricatorPolicyInterface
);
26 public function buildCustomEditFields(
27 PhabricatorEditEngine
$engine,
28 PhabricatorApplicationTransactionInterface
$object) {
30 $viewer = $engine->getViewer();
32 $editor = $object->getApplicationTransactionEditor();
33 $types = $editor->getTransactionTypesForObject($object);
34 $types = array_fuse($types);
36 $policies = id(new PhabricatorPolicyQuery())
42 PhabricatorTransactions
::TYPE_VIEW_POLICY
=> array(
43 'key' => 'policy.view',
44 'aliases' => array('view'),
45 'capability' => PhabricatorPolicyCapability
::CAN_VIEW
,
46 'label' => pht('View Policy'),
47 'description' => pht('Controls who can view the object.'),
48 'description.conduit' => pht('Change the view policy of the object.'),
51 PhabricatorTransactions
::TYPE_EDIT_POLICY
=> array(
52 'key' => 'policy.edit',
53 'aliases' => array('edit'),
54 'capability' => PhabricatorPolicyCapability
::CAN_EDIT
,
55 'label' => pht('Edit Policy'),
56 'description' => pht('Controls who can edit the object.'),
57 'description.conduit' => pht('Change the edit policy of the object.'),
60 PhabricatorTransactions
::TYPE_JOIN_POLICY
=> array(
61 'key' => 'policy.join',
62 'aliases' => array('join'),
63 'capability' => PhabricatorPolicyCapability
::CAN_JOIN
,
64 'label' => pht('Join Policy'),
65 'description' => pht('Controls who can join the object.'),
66 'description.conduit' => pht('Change the join policy of the object.'),
69 PhabricatorTransactions
::TYPE_INTERACT_POLICY
=> array(
70 'key' => 'policy.interact',
71 'aliases' => array('interact'),
72 'capability' => PhabricatorPolicyCapability
::CAN_INTERACT
,
73 'label' => pht('Interact Policy'),
74 'description' => pht('Controls who can interact with the object.'),
76 => pht('Change the interaction policy of the object.'),
81 if ($object instanceof PhabricatorPolicyCodexInterface
) {
82 $codex = PhabricatorPolicyCodex
::newFromObject(
90 foreach ($map as $type => $spec) {
91 if (empty($types[$type])) {
95 $capability = $spec['capability'];
97 $aliases = $spec['aliases'];
98 $label = $spec['label'];
99 $description = $spec['description'];
100 $conduit_description = $spec['description.conduit'];
101 $edit = $spec['edit'];
103 // Objects may present a policy value to the edit workflow that is
104 // different from their nominal policy value: for example, when tasks
105 // are locked, they appear as "Editable By: No One" to other applications
106 // but we still want to edit the actual policy stored in the database
107 // when we show the user a form with a policy control in it.
110 $policy_value = $codex->getPolicyForEdit($capability);
112 $policy_value = $object->getPolicy($capability);
115 $policy_field = id(new PhabricatorPolicyEditField())
118 ->setAliases($aliases)
119 ->setIsCopyable(true)
120 ->setCapability($capability)
121 ->setPolicies($policies)
122 ->setTransactionType($type)
123 ->setEditTypeKey($edit)
124 ->setDescription($description)
125 ->setConduitDescription($conduit_description)
126 ->setConduitTypeDescription(pht('New policy PHID or constant.'))
127 ->setValue($policy_value);
128 $fields[] = $policy_field;
130 if ($object instanceof PhabricatorSpacesInterface
) {
131 if ($capability == PhabricatorPolicyCapability
::CAN_VIEW
) {
132 $type_space = PhabricatorTransactions
::TYPE_SPACE
;
133 if (isset($types[$type_space])) {
134 $space_phid = PhabricatorSpacesNamespaceQuery
::getObjectSpacePHID(
137 $space_field = id(new PhabricatorSpaceEditField())
138 ->setKey('spacePHID')
139 ->setLabel(pht('Space'))
140 ->setEditTypeKey('space')
141 ->setIsCopyable(true)
142 ->setIsLockable(false)
143 ->setIsReorderable(false)
144 ->setAliases(array('space', 'policy.space'))
145 ->setTransactionType($type_space)
146 ->setDescription(pht('Select a space for the object.'))
147 ->setConduitDescription(
148 pht('Shift the object between spaces.'))
149 ->setConduitTypeDescription(pht('New space PHID.'))
150 ->setValue($space_phid);
151 $fields[] = $space_field;
153 $space_field->setPolicyField($policy_field);
154 $policy_field->setSpaceField($space_field);