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.'),
71 if ($object instanceof PhabricatorPolicyCodexInterface
) {
72 $codex = PhabricatorPolicyCodex
::newFromObject(
80 foreach ($map as $type => $spec) {
81 if (empty($types[$type])) {
85 $capability = $spec['capability'];
87 $aliases = $spec['aliases'];
88 $label = $spec['label'];
89 $description = $spec['description'];
90 $conduit_description = $spec['description.conduit'];
91 $edit = $spec['edit'];
93 // Objects may present a policy value to the edit workflow that is
94 // different from their nominal policy value: for example, when tasks
95 // are locked, they appear as "Editable By: No One" to other applications
96 // but we still want to edit the actual policy stored in the database
97 // when we show the user a form with a policy control in it.
100 $policy_value = $codex->getPolicyForEdit($capability);
102 $policy_value = $object->getPolicy($capability);
105 $policy_field = id(new PhabricatorPolicyEditField())
108 ->setAliases($aliases)
109 ->setIsCopyable(true)
110 ->setCapability($capability)
111 ->setPolicies($policies)
112 ->setTransactionType($type)
113 ->setEditTypeKey($edit)
114 ->setDescription($description)
115 ->setConduitDescription($conduit_description)
116 ->setConduitTypeDescription(pht('New policy PHID or constant.'))
117 ->setValue($policy_value);
118 $fields[] = $policy_field;
120 if ($object instanceof PhabricatorSpacesInterface
) {
121 if ($capability == PhabricatorPolicyCapability
::CAN_VIEW
) {
122 $type_space = PhabricatorTransactions
::TYPE_SPACE
;
123 if (isset($types[$type_space])) {
124 $space_phid = PhabricatorSpacesNamespaceQuery
::getObjectSpacePHID(
127 $space_field = id(new PhabricatorSpaceEditField())
128 ->setKey('spacePHID')
129 ->setLabel(pht('Space'))
130 ->setEditTypeKey('space')
131 ->setIsCopyable(true)
132 ->setIsLockable(false)
133 ->setIsReorderable(false)
134 ->setAliases(array('space', 'policy.space'))
135 ->setTransactionType($type_space)
136 ->setDescription(pht('Select a space for the object.'))
137 ->setConduitDescription(
138 pht('Shift the object between spaces.'))
139 ->setConduitTypeDescription(pht('New space PHID.'))
140 ->setValue($space_phid);
141 $fields[] = $space_field;
143 $space_field->setPolicyField($policy_field);
144 $policy_field->setSpaceField($space_field);