Changeset View
Changeset View
Standalone View
Standalone View
src/applications/doorkeeper/worker/DoorkeeperAsanaFeedWorker.php
Show First 20 Lines • Show All 526 Lines • ▼ Show 20 Lines | private function lookupAsanaUserIDs($all_phids) { | ||||
$provider = PhabricatorAsanaAuthProvider::getAsanaProvider(); | $provider = PhabricatorAsanaAuthProvider::getAsanaProvider(); | ||||
$accounts = id(new PhabricatorExternalAccountQuery()) | $accounts = id(new PhabricatorExternalAccountQuery()) | ||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | ->setViewer(PhabricatorUser::getOmnipotentUser()) | ||||
->withUserPHIDs($all_phids) | ->withUserPHIDs($all_phids) | ||||
->withAccountTypes(array($provider->getProviderType())) | ->withAccountTypes(array($provider->getProviderType())) | ||||
->withAccountDomains(array($provider->getProviderDomain())) | ->withAccountDomains(array($provider->getProviderDomain())) | ||||
->needAccountIdentifiers(true) | |||||
->requireCapabilities( | ->requireCapabilities( | ||||
array( | array( | ||||
PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
)) | )) | ||||
->execute(); | ->execute(); | ||||
foreach ($accounts as $account) { | foreach ($accounts as $account) { | ||||
$phid_map[$account->getUserPHID()] = $account->getAccountID(); | $phid_map[$account->getUserPHID()] = $this->getAsanaAccountID($account); | ||||
} | } | ||||
// Put this back in input order. | // Put this back in input order. | ||||
$phid_map = array_select_keys($phid_map, $all_phids); | $phid_map = array_select_keys($phid_map, $all_phids); | ||||
return $phid_map; | return $phid_map; | ||||
} | } | ||||
private function findAnyValidAsanaAccessToken(array $user_phids) { | private function findAnyValidAsanaAccessToken(array $user_phids) { | ||||
if (!$user_phids) { | if (!$user_phids) { | ||||
return array(null, null, null); | return array(null, null, null); | ||||
} | } | ||||
$provider = $this->getProvider(); | $provider = $this->getProvider(); | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$accounts = id(new PhabricatorExternalAccountQuery()) | $accounts = id(new PhabricatorExternalAccountQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withUserPHIDs($user_phids) | ->withUserPHIDs($user_phids) | ||||
->withAccountTypes(array($provider->getProviderType())) | ->withAccountTypes(array($provider->getProviderType())) | ||||
->withAccountDomains(array($provider->getProviderDomain())) | ->withAccountDomains(array($provider->getProviderDomain())) | ||||
->needAccountIdentifiers(true) | |||||
->requireCapabilities( | ->requireCapabilities( | ||||
array( | array( | ||||
PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
)) | )) | ||||
->execute(); | ->execute(); | ||||
// Reorder accounts in the original order. | // Reorder accounts in the original order. | ||||
Show All 23 Lines | foreach ($accounts as $account) { | ||||
continue; | continue; | ||||
} | } | ||||
$user = id(new PhabricatorPeopleQuery()) | $user = id(new PhabricatorPeopleQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withPHIDs(array($account->getUserPHID())) | ->withPHIDs(array($account->getUserPHID())) | ||||
->executeOne(); | ->executeOne(); | ||||
if ($user) { | if ($user) { | ||||
return array($user, $account->getAccountID(), $token); | return array($user, $this->getAsanaAccountID($account), $token); | ||||
} | } | ||||
} | } | ||||
return array(null, null, null); | return array(null, null, null); | ||||
} | } | ||||
private function makeAsanaAPICall($token, $action, $method, array $params) { | private function makeAsanaAPICall($token, $action, $method, array $params) { | ||||
foreach ($params as $key => $value) { | foreach ($params as $key => $value) { | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | foreach ($project_ids as $project_id) { | ||||
$this->makeAsanaAPICall( | $this->makeAsanaAPICall( | ||||
$oauth_token, | $oauth_token, | ||||
"tasks/{$task_id}/addProject", | "tasks/{$task_id}/addProject", | ||||
'POST', | 'POST', | ||||
$data); | $data); | ||||
} | } | ||||
} | } | ||||
private function getAsanaAccountID(PhabricatorExternalAccount $account) { | |||||
$identifiers = $account->getAccountIdentifiers(); | |||||
if (count($identifiers) !== 1) { | |||||
throw new Exception( | |||||
pht( | |||||
'Expected external Asana account to have exactly one external '. | |||||
'account identifier, found %s.', | |||||
phutil_count($identifiers))); | |||||
} | |||||
return head($identifiers)->getIdentifierRaw(); | |||||
} | |||||
} | } |