|
| 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 | +} |
0 commit comments