Skip to content

Commit 1d1ecb5

Browse files
author
epriestley
committed
Add bin/policy unlock
Summary: Ref T603. We might need a fine-grained CLI tool later on, but here's a bat we can bludgeon things with. Test Plan: - Ran `bin/policy unlock D12` (adjusted policies). - Ran `bin/policy unlock rPca85c457ebcb` (got "not mutable" stuff). Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D7189
1 parent fb93fd0 commit 1d1ecb5

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

scripts/setup/manage_policy.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
$workflows = array(
1818
new PhabricatorPolicyManagementShowWorkflow(),
19+
new PhabricatorPolicyManagementUnlockWorkflow(),
1920
new PhutilHelpArgumentWorkflow(),
2021
);
2122

src/__phutil_library_map__.php

+2
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,7 @@
14691469
'PhabricatorPolicyFilter' => 'applications/policy/filter/PhabricatorPolicyFilter.php',
14701470
'PhabricatorPolicyInterface' => 'applications/policy/interface/PhabricatorPolicyInterface.php',
14711471
'PhabricatorPolicyManagementShowWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementShowWorkflow.php',
1472+
'PhabricatorPolicyManagementUnlockWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementUnlockWorkflow.php',
14721473
'PhabricatorPolicyManagementWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementWorkflow.php',
14731474
'PhabricatorPolicyQuery' => 'applications/policy/query/PhabricatorPolicyQuery.php',
14741475
'PhabricatorPolicyTestCase' => 'applications/policy/__tests__/PhabricatorPolicyTestCase.php',
@@ -3627,6 +3628,7 @@
36273628
'PhabricatorPolicyException' => 'Exception',
36283629
'PhabricatorPolicyExplainController' => 'PhabricatorPolicyController',
36293630
'PhabricatorPolicyManagementShowWorkflow' => 'PhabricatorPolicyManagementWorkflow',
3631+
'PhabricatorPolicyManagementUnlockWorkflow' => 'PhabricatorPolicyManagementWorkflow',
36303632
'PhabricatorPolicyManagementWorkflow' => 'PhutilArgumentWorkflow',
36313633
'PhabricatorPolicyQuery' => 'PhabricatorQuery',
36323634
'PhabricatorPolicyTestCase' => 'PhabricatorTestCase',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
final class PhabricatorPolicyManagementUnlockWorkflow
4+
extends PhabricatorPolicyManagementWorkflow {
5+
6+
protected function didConstruct() {
7+
$this
8+
->setName('unlock')
9+
->setSynopsis(
10+
'Unlock an object by setting its policies to allow anyone to view '.
11+
'and edit it.')
12+
->setExamples(
13+
"**unlock** D123")
14+
->setArguments(
15+
array(
16+
array(
17+
'name' => 'objects',
18+
'wildcard' => true,
19+
),
20+
));
21+
}
22+
23+
public function execute(PhutilArgumentParser $args) {
24+
$console = PhutilConsole::getConsole();
25+
$viewer = PhabricatorUser::getOmnipotentUser();
26+
27+
$obj_names = $args->getArg('objects');
28+
if (!$obj_names) {
29+
throw new PhutilArgumentUsageException(
30+
pht(
31+
"Specify the name of an object to unlock."));
32+
} else if (count($obj_names) > 1) {
33+
throw new PhutilArgumentUsageException(
34+
pht(
35+
"Specify the name of exactly one object to unlock."));
36+
}
37+
38+
$object = id(new PhabricatorObjectQuery())
39+
->setViewer($viewer)
40+
->withNames($obj_names)
41+
->executeOne();
42+
43+
if (!$object) {
44+
$name = head($obj_names);
45+
throw new PhutilArgumentUsageException(
46+
pht(
47+
"No such object '%s'!",
48+
$name));
49+
}
50+
51+
$handle = id(new PhabricatorHandleQuery())
52+
->setViewer($viewer)
53+
->withPHIDs(array($object->getPHID()))
54+
->executeOne();
55+
56+
$console->writeOut("%s\n", pht('Unlocking: %s', $handle->getFullName()));
57+
58+
$updated = false;
59+
foreach ($object->getCapabilities() as $capability) {
60+
switch ($capability) {
61+
case PhabricatorPolicyCapability::CAN_VIEW:
62+
try {
63+
$object->setViewPolicy(PhabricatorPolicies::POLICY_USER);
64+
$console->writeOut("%s\n", pht('Unlocked view policy.'));
65+
$updated = true;
66+
} catch (Exception $ex) {
67+
$console->writeOut("%s\n", pht('View policy is not mutable.'));
68+
}
69+
break;
70+
case PhabricatorPolicyCapability::CAN_EDIT:
71+
try {
72+
$object->setEditPolicy(PhabricatorPolicies::POLICY_USER);
73+
$console->writeOut("%s\n", pht('Unlocked edit policy.'));
74+
$updated = true;
75+
} catch (Exception $ex) {
76+
$console->writeOut("%s\n", pht('Edit policy is not mutable.'));
77+
}
78+
break;
79+
case PhabricatorPolicyCapability::CAN_JOIN:
80+
try {
81+
$object->setJoinPolicy(PhabricatorPolicies::POLICY_USER);
82+
$console->writeOut("%s\n", pht('Unlocked join policy.'));
83+
$updated = true;
84+
} catch (Exception $ex) {
85+
$console->writeOut("%s\n", pht('Join policy is not mutable.'));
86+
}
87+
break;
88+
}
89+
}
90+
91+
if ($updated) {
92+
$object->save();
93+
$console->writeOut("%s\n", pht('Saved object.'));
94+
} else {
95+
$console->writeOut(
96+
"%s\n",
97+
pht(
98+
'Object has no mutable policies. Try unlocking parent/container '.
99+
'object instead. For example, to gain access to a commit, unlock '.
100+
'the repository it belongs to.'));
101+
}
102+
}
103+
104+
}

0 commit comments

Comments
 (0)