Skip to content

Commit e2ed527

Browse files
author
epriestley
committedSep 29, 2013
Add a very simple bin/policy script for CLI policy administration
Summary: Ref T603. I want to provide at least a basic CLI tool for fixing policy problems, since there are various ways users can lock themselves out of objects right now. Although I imagine we'll solve most of them in the application eventually, having a workaround in the meantime will probably make support a lot easier. This implements `bin/policy show <object>`, which shows an object's policy settings. In a future diff, I'll implement something like `bin/policy set --capability view --policy users <object>`, although maybe just `bin/policy unlock <object>` (which sets view and edit to "all users") would be better for now. Whichever way we go, it will be some blanket answer to people showing up in IRC having locked themselves out of objects which unblocks them while we work on preventing the issue in the first place. Test Plan: See screenshot. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D7171
1 parent 432cdb6 commit e2ed527

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
 

‎bin/policy

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../scripts/setup/manage_policy.php

‎scripts/setup/manage_policy.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$root = dirname(dirname(dirname(__FILE__)));
5+
require_once $root.'/scripts/__init_script__.php';
6+
7+
$args = new PhutilArgumentParser($argv);
8+
$args->setTagline('manage policies');
9+
$args->setSynopsis(<<<EOSYNOPSIS
10+
**policy** __command__ [__options__]
11+
Administrative tool for reviewing and editing policies.
12+
13+
EOSYNOPSIS
14+
);
15+
$args->parseStandardArguments();
16+
17+
$workflows = array(
18+
new PhabricatorPolicyManagementShowWorkflow(),
19+
new PhutilHelpArgumentWorkflow(),
20+
);
21+
22+
$args->parseWorkflows($workflows);

‎src/__phutil_library_map__.php

+4
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,8 @@
14671467
'PhabricatorPolicyExplainController' => 'applications/policy/controller/PhabricatorPolicyExplainController.php',
14681468
'PhabricatorPolicyFilter' => 'applications/policy/filter/PhabricatorPolicyFilter.php',
14691469
'PhabricatorPolicyInterface' => 'applications/policy/interface/PhabricatorPolicyInterface.php',
1470+
'PhabricatorPolicyManagementShowWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementShowWorkflow.php',
1471+
'PhabricatorPolicyManagementWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementWorkflow.php',
14701472
'PhabricatorPolicyQuery' => 'applications/policy/query/PhabricatorPolicyQuery.php',
14711473
'PhabricatorPolicyTestCase' => 'applications/policy/__tests__/PhabricatorPolicyTestCase.php',
14721474
'PhabricatorPolicyTestObject' => 'applications/policy/__tests__/PhabricatorPolicyTestObject.php',
@@ -3622,6 +3624,8 @@
36223624
'PhabricatorPolicyDataTestCase' => 'PhabricatorTestCase',
36233625
'PhabricatorPolicyException' => 'Exception',
36243626
'PhabricatorPolicyExplainController' => 'PhabricatorPolicyController',
3627+
'PhabricatorPolicyManagementShowWorkflow' => 'PhabricatorPolicyManagementWorkflow',
3628+
'PhabricatorPolicyManagementWorkflow' => 'PhutilArgumentWorkflow',
36253629
'PhabricatorPolicyQuery' => 'PhabricatorQuery',
36263630
'PhabricatorPolicyTestCase' => 'PhabricatorTestCase',
36273631
'PhabricatorPolicyTestObject' => 'PhabricatorPolicyInterface',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
final class PhabricatorPolicyManagementShowWorkflow
4+
extends PhabricatorPolicyManagementWorkflow {
5+
6+
protected function didConstruct() {
7+
$this
8+
->setName('show')
9+
->setSynopsis('Show policy information about an object.')
10+
->setExamples(
11+
"**show** D123")
12+
->setArguments(
13+
array(
14+
array(
15+
'name' => 'objects',
16+
'wildcard' => true,
17+
),
18+
));
19+
}
20+
21+
public function execute(PhutilArgumentParser $args) {
22+
$console = PhutilConsole::getConsole();
23+
$viewer = PhabricatorUser::getOmnipotentUser();
24+
25+
$obj_names = $args->getArg('objects');
26+
if (!$obj_names) {
27+
throw new PhutilArgumentUsageException(
28+
pht(
29+
"Specify the name of an object to show policy information for."));
30+
} else if (count($obj_names) > 1) {
31+
throw new PhutilArgumentUsageException(
32+
pht(
33+
"Specify the name of exactly one object to show policy information ".
34+
"for."));
35+
}
36+
37+
$object = id(new PhabricatorObjectQuery())
38+
->setViewer($viewer)
39+
->withNames($obj_names)
40+
->executeOne();
41+
42+
if (!$object) {
43+
$name = head($obj_names);
44+
throw new PhutilArgumentUsageException(
45+
pht(
46+
"No such object '%s'!",
47+
$name));
48+
}
49+
50+
$handle = id(new PhabricatorHandleQuery())
51+
->setViewer($viewer)
52+
->withPHIDs(array($object->getPHID()))
53+
->executeOne();
54+
55+
$policies = PhabricatorPolicyQuery::loadPolicies(
56+
$viewer,
57+
$object);
58+
59+
$console->writeOut("__%s__\n\n", pht('OBJECT'));
60+
$console->writeOut(" %s\n", $handle->getFullName());
61+
$console->writeOut("\n");
62+
63+
$console->writeOut("__%s__\n\n", pht('CAPABILITIES'));
64+
foreach ($policies as $capability => $policy) {
65+
$console->writeOut(" **%s**\n", $capability);
66+
$console->writeOut(" %s\n", $policy->renderDescription());
67+
$console->writeOut(" %s\n", $policy->getExplanation($capability));
68+
$console->writeOut("\n");
69+
70+
$more = (array)$object->describeAutomaticCapability($capability);
71+
if ($more) {
72+
foreach ($more as $line) {
73+
$console->writeOut(" %s\n", $line);
74+
}
75+
$console->writeOut("\n");
76+
}
77+
}
78+
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
abstract class PhabricatorPolicyManagementWorkflow
4+
extends PhutilArgumentWorkflow {
5+
6+
final public function isExecutable() {
7+
return true;
8+
}
9+
10+
}

0 commit comments

Comments
 (0)
Failed to load comments.