Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / policy / editor / PhabricatorPolicyEditEngineExtension.php
blob14a4768f214cebd0fbc2d717c63e541a57b95ce1
1 <?php
3 final class PhabricatorPolicyEditEngineExtension
4 extends PhabricatorEditEngineExtension {
6 const EXTENSIONKEY = 'policy.policy';
8 public function getExtensionPriority() {
9 return 250;
12 public function isExtensionEnabled() {
13 return true;
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())
37 ->setViewer($viewer)
38 ->setObject($object)
39 ->execute();
41 $map = array(
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.'),
49 'edit' => 'view',
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.'),
58 'edit' => 'edit',
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.'),
67 'edit' => 'join',
71 if ($object instanceof PhabricatorPolicyCodexInterface) {
72 $codex = PhabricatorPolicyCodex::newFromObject(
73 $object,
74 $viewer);
75 } else {
76 $codex = null;
79 $fields = array();
80 foreach ($map as $type => $spec) {
81 if (empty($types[$type])) {
82 continue;
85 $capability = $spec['capability'];
86 $key = $spec['key'];
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.
99 if ($codex) {
100 $policy_value = $codex->getPolicyForEdit($capability);
101 } else {
102 $policy_value = $object->getPolicy($capability);
105 $policy_field = id(new PhabricatorPolicyEditField())
106 ->setKey($key)
107 ->setLabel($label)
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(
125 $object);
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);
150 return $fields;