mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-13 18:32:41 +01:00
b1b1ff83f2
Summary: Ref T603. I want to let applications define new capabilities (like "can manage global rules" in Herald) and get full support for them, including reasonable error strings in the UI. Currently, this is difficult for a couple of reasons. Partly this is just a code organization issue, which is easy to fix. The bigger thing is that we have a bunch of strings which depend on both the policy and capability, like: "You must be an administrator to view this object." "Administrator" is the policy, and "view" is the capability. That means every new capability has to add a string for each policy, and every new policy (should we introduce any) needs to add a string for each capability. And we can't do any piecemeal "You must be a {$role} to {$action} this object" becuase it's impossible to translate. Instead, make all the strings depend on //only// the policy, //only// the capability, or //only// the object type. This makes the dialogs read a little more strangely, but I think it's still pretty easy to understand, and it makes adding new stuff way way easier. Also provide more context, and more useful exception messages. Test Plan: - See screenshots. - Also triggered a policy exception and verified it was dramatically more useful than it used to be. Reviewers: btrahan, chad Reviewed By: btrahan CC: chad, aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D7260
82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
|
|
final class PhabricatorPolicyManagementShowWorkflow
|
|
extends PhabricatorPolicyManagementWorkflow {
|
|
|
|
protected function didConstruct() {
|
|
$this
|
|
->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",
|
|
PhabricatorPolicy::getPolicyExplanation($viewer, $policy->getPHID()));
|
|
$console->writeOut("\n");
|
|
|
|
$more = (array)$object->describeAutomaticCapability($capability);
|
|
if ($more) {
|
|
foreach ($more as $line) {
|
|
$console->writeOut(" %s\n", $line);
|
|
}
|
|
$console->writeOut("\n");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|