|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorAuthManagementRevokeWorkflow |
| 4 | + extends PhabricatorAuthManagementWorkflow { |
| 5 | + |
| 6 | + protected function didConstruct() { |
| 7 | + $this |
| 8 | + ->setName('revoke') |
| 9 | + ->setExamples( |
| 10 | + "**revoke** --type __type__ --from __user__\n". |
| 11 | + "**revoke** --everything --everywhere") |
| 12 | + ->setSynopsis( |
| 13 | + pht( |
| 14 | + 'Revoke credentials which may have been leaked or disclosed.')) |
| 15 | + ->setArguments( |
| 16 | + array( |
| 17 | + array( |
| 18 | + 'name' => 'from', |
| 19 | + 'param' => 'user', |
| 20 | + 'help' => pht( |
| 21 | + 'Revoke credentials for the specified user.'), |
| 22 | + ), |
| 23 | + array( |
| 24 | + 'name' => 'type', |
| 25 | + 'param' => 'type', |
| 26 | + 'help' => pht( |
| 27 | + 'Revoke credentials of the given type.'), |
| 28 | + ), |
| 29 | + array( |
| 30 | + 'name' => 'everything', |
| 31 | + 'help' => pht('Revoke all credentials types.'), |
| 32 | + ), |
| 33 | + array( |
| 34 | + 'name' => 'everywhere', |
| 35 | + 'help' => pht('Revoke from all credential owners.'), |
| 36 | + ), |
| 37 | + )); |
| 38 | + } |
| 39 | + |
| 40 | + public function execute(PhutilArgumentParser $args) { |
| 41 | + $viewer = PhabricatorUser::getOmnipotentUser(); |
| 42 | + |
| 43 | + $all_types = PhabricatorAuthRevoker::getAllRevokers(); |
| 44 | + |
| 45 | + $type = $args->getArg('type'); |
| 46 | + $is_everything = $args->getArg('everything'); |
| 47 | + if (!strlen($type) && !$is_everything) { |
| 48 | + throw new PhutilArgumentUsageException( |
| 49 | + pht( |
| 50 | + 'Specify the credential type to revoke with "--type" or specify '. |
| 51 | + '"--everything".')); |
| 52 | + } else if (strlen($type) && $is_everything) { |
| 53 | + throw new PhutilArgumentUsageException( |
| 54 | + pht( |
| 55 | + 'Specify the credential type to revoke with "--type" or '. |
| 56 | + '"--everything", but not both.')); |
| 57 | + } else if ($is_everything) { |
| 58 | + $types = $all_types; |
| 59 | + } else { |
| 60 | + if (empty($all_types[$type])) { |
| 61 | + throw new PhutilArgumentUsageException( |
| 62 | + pht( |
| 63 | + 'Credential type "%s" is not valid. Valid credential types '. |
| 64 | + 'are: %s.', |
| 65 | + $type, |
| 66 | + implode(', ', array_keys($all_types)))); |
| 67 | + } |
| 68 | + $types = array($all_types[$type]); |
| 69 | + } |
| 70 | + |
| 71 | + $is_everywhere = $args->getArg('everywhere'); |
| 72 | + $from = $args->getArg('from'); |
| 73 | + $target = null; |
| 74 | + if (!strlen($from) && !$is_everywhere) { |
| 75 | + throw new PhutilArgumentUsageException( |
| 76 | + pht( |
| 77 | + 'Specify the target to revoke credentals from with "--from" or '. |
| 78 | + 'specify "--everywhere".')); |
| 79 | + } else if (strlen($from) && $is_everywhere) { |
| 80 | + throw new PhutilArgumentUsageException( |
| 81 | + pht( |
| 82 | + 'Specify the target to revoke credentials from with "--from" or '. |
| 83 | + 'specify "--everywhere", but not both.')); |
| 84 | + } else if ($is_everywhere) { |
| 85 | + // Just carry the flag through. |
| 86 | + } else { |
| 87 | + $target = id(new PhabricatorObjectQuery()) |
| 88 | + ->setViewer($viewer) |
| 89 | + ->withNames(array($from)) |
| 90 | + ->executeOne(); |
| 91 | + if (!$target) { |
| 92 | + throw new PhutilArgumentUsageException( |
| 93 | + pht( |
| 94 | + 'Target "%s" is not a valid target to revoke credentials from. '. |
| 95 | + 'Usually, revoke from "@username".', |
| 96 | + $from)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if ($is_everywhere) { |
| 101 | + echo id(new PhutilConsoleBlock()) |
| 102 | + ->addParagraph( |
| 103 | + pht( |
| 104 | + 'You are destroying an entire class of credentials. This may be '. |
| 105 | + 'very disruptive to users. You should normally do this only if '. |
| 106 | + 'you suspect there has been a widespread compromise which may '. |
| 107 | + 'have impacted everyone.')) |
| 108 | + ->drawConsoleString(); |
| 109 | + |
| 110 | + $prompt = pht('Really destroy credentials everywhere?'); |
| 111 | + if (!phutil_console_confirm($prompt)) { |
| 112 | + throw new PhutilArgumentUsageException( |
| 113 | + pht('Aborted workflow.')); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + foreach ($types as $type) { |
| 118 | + $type->setViewer($viewer); |
| 119 | + if ($is_everywhere) { |
| 120 | + $count = $type->revokeAllCredentials(); |
| 121 | + } else { |
| 122 | + $count = $type->revokeCredentialsFrom($target); |
| 123 | + } |
| 124 | + |
| 125 | + echo tsprintf( |
| 126 | + "%s\n", |
| 127 | + pht( |
| 128 | + 'Destroyed %s credential(s) of type "%s".', |
| 129 | + new PhutilNumber($count), |
| 130 | + $type->getRevokerKey())); |
| 131 | + } |
| 132 | + |
| 133 | + echo tsprintf( |
| 134 | + "%s\n", |
| 135 | + pht('Done.')); |
| 136 | + |
| 137 | + return 0; |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments