diff --git a/bin/policy b/bin/policy new file mode 120000 index 0000000000..ffb2bac562 --- /dev/null +++ b/bin/policy @@ -0,0 +1 @@ +../scripts/setup/manage_policy.php \ No newline at end of file diff --git a/scripts/setup/manage_policy.php b/scripts/setup/manage_policy.php new file mode 100755 index 0000000000..ee209334af --- /dev/null +++ b/scripts/setup/manage_policy.php @@ -0,0 +1,22 @@ +#!/usr/bin/env php +setTagline('manage policies'); +$args->setSynopsis(<<parseStandardArguments(); + +$workflows = array( + new PhabricatorPolicyManagementShowWorkflow(), + new PhutilHelpArgumentWorkflow(), +); + +$args->parseWorkflows($workflows); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 884a23f75a..10b4450e65 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1467,6 +1467,8 @@ phutil_register_library_map(array( 'PhabricatorPolicyExplainController' => 'applications/policy/controller/PhabricatorPolicyExplainController.php', 'PhabricatorPolicyFilter' => 'applications/policy/filter/PhabricatorPolicyFilter.php', 'PhabricatorPolicyInterface' => 'applications/policy/interface/PhabricatorPolicyInterface.php', + 'PhabricatorPolicyManagementShowWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementShowWorkflow.php', + 'PhabricatorPolicyManagementWorkflow' => 'applications/policy/management/PhabricatorPolicyManagementWorkflow.php', 'PhabricatorPolicyQuery' => 'applications/policy/query/PhabricatorPolicyQuery.php', 'PhabricatorPolicyTestCase' => 'applications/policy/__tests__/PhabricatorPolicyTestCase.php', 'PhabricatorPolicyTestObject' => 'applications/policy/__tests__/PhabricatorPolicyTestObject.php', @@ -3622,6 +3624,8 @@ phutil_register_library_map(array( 'PhabricatorPolicyDataTestCase' => 'PhabricatorTestCase', 'PhabricatorPolicyException' => 'Exception', 'PhabricatorPolicyExplainController' => 'PhabricatorPolicyController', + 'PhabricatorPolicyManagementShowWorkflow' => 'PhabricatorPolicyManagementWorkflow', + 'PhabricatorPolicyManagementWorkflow' => 'PhutilArgumentWorkflow', 'PhabricatorPolicyQuery' => 'PhabricatorQuery', 'PhabricatorPolicyTestCase' => 'PhabricatorTestCase', 'PhabricatorPolicyTestObject' => 'PhabricatorPolicyInterface', diff --git a/src/applications/policy/management/PhabricatorPolicyManagementShowWorkflow.php b/src/applications/policy/management/PhabricatorPolicyManagementShowWorkflow.php new file mode 100644 index 0000000000..ca6c6754a2 --- /dev/null +++ b/src/applications/policy/management/PhabricatorPolicyManagementShowWorkflow.php @@ -0,0 +1,81 @@ +setName('show') + ->setSynopsis('Show policy information about an object.') + ->setExamples( + "**show** D123") + ->setArguments( + array( + array( + 'name' => 'objects', + 'wildcard' => true, + ), + )); + } + + public function execute(PhutilArgumentParser $args) { + $console = PhutilConsole::getConsole(); + $viewer = PhabricatorUser::getOmnipotentUser(); + + $obj_names = $args->getArg('objects'); + if (!$obj_names) { + throw new PhutilArgumentUsageException( + pht( + "Specify the name of an object to show policy information for.")); + } else if (count($obj_names) > 1) { + throw new PhutilArgumentUsageException( + pht( + "Specify the name of exactly one object to show policy information ". + "for.")); + } + + $object = id(new PhabricatorObjectQuery()) + ->setViewer($viewer) + ->withNames($obj_names) + ->executeOne(); + + if (!$object) { + $name = head($obj_names); + throw new PhutilArgumentUsageException( + pht( + "No such object '%s'!", + $name)); + } + + $handle = id(new PhabricatorHandleQuery()) + ->setViewer($viewer) + ->withPHIDs(array($object->getPHID())) + ->executeOne(); + + $policies = PhabricatorPolicyQuery::loadPolicies( + $viewer, + $object); + + $console->writeOut("__%s__\n\n", pht('OBJECT')); + $console->writeOut(" %s\n", $handle->getFullName()); + $console->writeOut("\n"); + + $console->writeOut("__%s__\n\n", pht('CAPABILITIES')); + foreach ($policies as $capability => $policy) { + $console->writeOut(" **%s**\n", $capability); + $console->writeOut(" %s\n", $policy->renderDescription()); + $console->writeOut(" %s\n", $policy->getExplanation($capability)); + $console->writeOut("\n"); + + $more = (array)$object->describeAutomaticCapability($capability); + if ($more) { + foreach ($more as $line) { + $console->writeOut(" %s\n", $line); + } + $console->writeOut("\n"); + } + } + + } + +} diff --git a/src/applications/policy/management/PhabricatorPolicyManagementWorkflow.php b/src/applications/policy/management/PhabricatorPolicyManagementWorkflow.php new file mode 100644 index 0000000000..85d1fe7b3e --- /dev/null +++ b/src/applications/policy/management/PhabricatorPolicyManagementWorkflow.php @@ -0,0 +1,10 @@ +