Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / auth / management / PhabricatorAuthManagementRefreshWorkflow.php
blobc5a5192f5b7beb81e3dffe03515f2fd8d3b226bd
1 <?php
3 final class PhabricatorAuthManagementRefreshWorkflow
4 extends PhabricatorAuthManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('refresh')
9 ->setExamples('**refresh**')
10 ->setSynopsis(
11 pht(
12 'Refresh OAuth access tokens. This is primarily useful for '.
13 'development and debugging.'))
14 ->setArguments(
15 array(
16 array(
17 'name' => 'user',
18 'param' => 'user',
19 'help' => pht('Refresh tokens for a given user.'),
21 ));
24 public function execute(PhutilArgumentParser $args) {
25 $console = PhutilConsole::getConsole();
26 $viewer = $this->getViewer();
28 $query = id(new PhabricatorExternalAccountQuery())
29 ->setViewer($viewer)
30 ->requireCapabilities(
31 array(
32 PhabricatorPolicyCapability::CAN_VIEW,
33 PhabricatorPolicyCapability::CAN_EDIT,
34 ));
36 $username = $args->getArg('user');
37 if (strlen($username)) {
38 $user = id(new PhabricatorPeopleQuery())
39 ->setViewer($viewer)
40 ->withUsernames(array($username))
41 ->executeOne();
42 if ($user) {
43 $query->withUserPHIDs(array($user->getPHID()));
44 } else {
45 throw new PhutilArgumentUsageException(
46 pht('No such user "%s"!', $username));
50 $accounts = $query->execute();
52 if (!$accounts) {
53 throw new PhutilArgumentUsageException(
54 pht('No accounts match the arguments!'));
55 } else {
56 $console->writeOut(
57 "%s\n",
58 pht(
59 'Found %s account(s) to refresh.',
60 phutil_count($accounts)));
63 $providers = PhabricatorAuthProvider::getAllEnabledProviders();
64 $providers = mpull($providers, null, 'getProviderConfigPHID');
66 foreach ($accounts as $account) {
67 $console->writeOut(
68 "%s\n",
69 pht(
70 'Refreshing account #%d.',
71 $account->getID()));
73 $config_phid = $account->getProviderConfigPHID();
74 if (empty($providers[$config_phid])) {
75 $console->writeOut(
76 "> %s\n",
77 pht('Skipping, provider is not enabled or does not exist.'));
78 continue;
81 $provider = $providers[$config_phid];
82 if (!($provider instanceof PhabricatorOAuth2AuthProvider)) {
83 $console->writeOut(
84 "> %s\n",
85 pht('Skipping, provider is not an OAuth2 provider.'));
86 continue;
89 $adapter = $provider->getAdapter();
90 if (!$adapter->supportsTokenRefresh()) {
91 $console->writeOut(
92 "> %s\n",
93 pht('Skipping, provider does not support token refresh.'));
94 continue;
97 $refresh_token = $account->getProperty('oauth.token.refresh');
98 if (!$refresh_token) {
99 $console->writeOut(
100 "> %s\n",
101 pht('Skipping, provider has no stored refresh token.'));
102 continue;
105 $console->writeOut(
106 "+ %s\n",
107 pht(
108 'Refreshing token, current token expires in %s seconds.',
109 new PhutilNumber(
110 $account->getProperty('oauth.token.access.expires') - time())));
112 $token = $provider->getOAuthAccessToken($account, $force_refresh = true);
113 if (!$token) {
114 $console->writeOut(
115 "* %s\n",
116 pht('Unable to refresh token!'));
117 continue;
120 $console->writeOut(
121 "+ %s\n",
122 pht(
123 'Refreshed token, new token expires in %s seconds.',
124 new PhutilNumber(
125 $account->getProperty('oauth.token.access.expires') - time())));
129 $console->writeOut("%s\n", pht('Done.'));
131 return 0;