Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / policy / management / PhabricatorPolicyManagementUnlockWorkflow.php
blobeff06f9d5f5fc9fb517ff16ebce4bab4a2ec747e
1 <?php
3 final class PhabricatorPolicyManagementUnlockWorkflow
4 extends PhabricatorPolicyManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('unlock')
9 ->setSynopsis(
10 pht(
11 'Unlock one or more objects by changing their view policies, edit '.
12 'policies, or owners.'))
13 ->setHelp(
14 pht(
15 'Identify each __object__ by passing an object name '.
16 '(like "T123") or a PHID (like "PHID-ABCD-1234...").'.
17 "\n\n".
18 'Not every type of object has an editable view policy, edit '.
19 'policy, or owner, so not all modes will work with all objects. '))
20 ->setExamples('**unlock** --view __user__ __object__ ...')
21 ->setArguments(
22 array(
23 array(
24 'name' => 'view',
25 'param' => 'username',
26 'help' => pht(
27 'Change the view policy of an object so that the specified '.
28 'user may view it.'),
30 array(
31 'name' => 'edit',
32 'param' => 'username',
33 'help' => pht(
34 'Change the edit policy of an object so that the specified '.
35 'user may edit it.'),
37 array(
38 'name' => 'owner',
39 'param' => 'username',
40 'help' => pht(
41 'Change the owner of an object to the specified user.'),
43 array(
44 'name' => 'objects',
45 'wildcard' => true,
47 ));
50 public function execute(PhutilArgumentParser $args) {
51 $viewer = $this->getViewer();
53 $object_names = $args->getArg('objects');
54 if (!$object_names) {
55 throw new PhutilArgumentUsageException(
56 pht('Specify the name of an object to unlock.'));
57 } else if (count($object_names) > 1) {
58 throw new PhutilArgumentUsageException(
59 pht('Specify the name of exactly one object to unlock.'));
62 $object_name = head($object_names);
64 $object = id(new PhabricatorObjectQuery())
65 ->setViewer($viewer)
66 ->withNames(array($object_name))
67 ->executeOne();
68 if (!$object) {
69 throw new PhutilArgumentUsageException(
70 pht(
71 'Unable to find any object with the specified name ("%s").',
72 $object_name));
75 $view_user = $this->loadUser($args->getArg('view'));
76 $edit_user = $this->loadUser($args->getArg('edit'));
77 $owner_user = $this->loadUser($args->getArg('owner'));
79 if (!$view_user && !$edit_user && !$owner_user) {
80 throw new PhutilArgumentUsageException(
81 pht(
82 'Choose which capabilities to unlock with "--view", "--edit", '.
83 'or "--owner".'));
86 $handle = id(new PhabricatorHandleQuery())
87 ->setViewer($viewer)
88 ->withPHIDs(array($object->getPHID()))
89 ->executeOne();
91 echo tsprintf(
92 "<bg:blue>** %s **</bg> %s\n",
93 pht('UNLOCKING'),
94 pht('Unlocking: %s', $handle->getFullName()));
96 $engine = PhabricatorUnlockEngine::newUnlockEngineForObject($object);
98 $xactions = array();
99 if ($view_user) {
100 $xactions[] = $engine->newUnlockViewTransactions($object, $view_user);
102 if ($edit_user) {
103 $xactions[] = $engine->newUnlockEditTransactions($object, $edit_user);
105 if ($owner_user) {
106 $xactions[] = $engine->newUnlockOwnerTransactions($object, $owner_user);
108 $xactions = array_mergev($xactions);
110 $policy_application = new PhabricatorPolicyApplication();
111 $content_source = $this->newContentSource();
113 $editor = $object->getApplicationTransactionEditor()
114 ->setActor($viewer)
115 ->setActingAsPHID($policy_application->getPHID())
116 ->setContinueOnMissingFields(true)
117 ->setContinueOnNoEffect(true)
118 ->setContentSource($content_source);
120 $editor->applyTransactions($object, $xactions);
122 echo tsprintf(
123 "<bg:green>** %s **</bg> %s\n",
124 pht('UNLOCKED'),
125 pht('Modified object policies.'));
127 $uri = $handle->getURI();
128 if (strlen($uri)) {
129 echo tsprintf(
130 "\n **%s**: __%s__\n\n",
131 pht('Object URI'),
132 PhabricatorEnv::getURI($uri));
135 return 0;
138 private function loadUser($username) {
139 $viewer = $this->getViewer();
141 if ($username === null) {
142 return null;
145 $user = id(new PhabricatorPeopleQuery())
146 ->setViewer($viewer)
147 ->withUsernames(array($username))
148 ->executeOne();
150 if (!$user) {
151 throw new PhutilArgumentUsageException(
152 pht(
153 'No user with username "%s" exists.',
154 $username));
157 return $user;