Changeset View
Changeset View
Standalone View
Standalone View
src/applications/auth/provider/PhabricatorAuthProvider.php
| Show First 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | abstract class PhabricatorAuthProvider extends Phobject { | ||||
| protected function willSaveAccount(PhabricatorExternalAccount $account) { | protected function willSaveAccount(PhabricatorExternalAccount $account) { | ||||
| return; | return; | ||||
| } | } | ||||
| public function willRegisterAccount(PhabricatorExternalAccount $account) { | public function willRegisterAccount(PhabricatorExternalAccount $account) { | ||||
| return; | return; | ||||
| } | } | ||||
| protected function loadOrCreateAccount($account_id) { | protected function loadOrCreateAccount(array $identifiers) { | ||||
| if (!strlen($account_id)) { | assert_instances_of($identifiers, 'PhabricatorExternalAccountIdentifier'); | ||||
| throw new Exception(pht('Empty account ID!')); | |||||
| } | |||||
| $adapter = $this->getAdapter(); | |||||
| $adapter_class = get_class($adapter); | |||||
| if (!strlen($adapter->getAdapterType())) { | if (!$identifiers) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| "AuthAdapter (of class '%s') has an invalid implementation: ". | 'Authentication provider (of class "%s") is attempting to '. | ||||
| "no adapter type.", | 'load or create an external account, but provided no account '. | ||||
| $adapter_class)); | 'identifiers.', | ||||
| get_class($this))); | |||||
| } | } | ||||
| if (!strlen($adapter->getAdapterDomain())) { | if (count($identifiers) !== 1) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| "AuthAdapter (of class '%s') has an invalid implementation: ". | 'Unexpected number of account identifiers returned (by class "%s").', | ||||
| "no adapter domain.", | get_class($this))); | ||||
| $adapter_class)); | |||||
| } | } | ||||
| $account = id(new PhabricatorExternalAccount())->loadOneWhere( | $config = $this->getProviderConfig(); | ||||
| 'accountType = %s AND accountDomain = %s AND accountID = %s', | $viewer = PhabricatorUser::getOmnipotentUser(); | ||||
| $adapter->getAdapterType(), | |||||
| $adapter->getAdapterDomain(), | $raw_identifiers = mpull($identifiers, 'getIdentifierRaw'); | ||||
| $account_id); | |||||
| if (!$account) { | $accounts = id(new PhabricatorExternalAccountQuery()) | ||||
| ->setViewer($viewer) | |||||
| ->withProviderConfigPHIDs(array($config->getPHID())) | |||||
| ->withAccountIDs($raw_identifiers) | |||||
| ->execute(); | |||||
| if (!$accounts) { | |||||
| $account = $this->newExternalAccount() | $account = $this->newExternalAccount() | ||||
| ->setAccountID($account_id); | ->setAccountID(head($raw_identifiers)); | ||||
| } else if (count($accounts) === 1) { | |||||
| $account = head($accounts); | |||||
| } else { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Authentication provider (of class "%s") is attempting to load '. | |||||
| 'or create an external account, but provided a list of '. | |||||
| 'account identifiers which map to more than one account: %s.', | |||||
| get_class($this), | |||||
| implode(', ', $raw_identifiers))); | |||||
| } | } | ||||
| $adapter = $this->getAdapter(); | |||||
| $account->setUsername($adapter->getAccountName()); | $account->setUsername($adapter->getAccountName()); | ||||
| $account->setRealName($adapter->getAccountRealName()); | $account->setRealName($adapter->getAccountRealName()); | ||||
| $account->setEmail($adapter->getAccountEmail()); | $account->setEmail($adapter->getAccountEmail()); | ||||
| $account->setAccountURI($adapter->getAccountURI()); | $account->setAccountURI($adapter->getAccountURI()); | ||||
| $account->setProfileImagePHID(null); | $account->setProfileImagePHID(null); | ||||
| $image_uri = $adapter->getAccountImageURI(); | $image_uri = $adapter->getAccountImageURI(); | ||||
| if ($image_uri) { | if ($image_uri) { | ||||
| try { | try { | ||||
| $name = PhabricatorSlug::normalize($this->getProviderName()); | $name = PhabricatorSlug::normalize($this->getProviderName()); | ||||
| $name = $name.'-profile.jpg'; | $name = $name.'-profile.jpg'; | ||||
| // TODO: If the image has not changed, we do not need to make a new | // TODO: If the image has not changed, we do not need to make a new | ||||
| // file entry for it, but there's no convenient way to do this with | // file entry for it, but there's no convenient way to do this with | ||||
| // PhabricatorFile right now. The storage will get shared, so the impact | // PhabricatorFile right now. The storage will get shared, so the impact | ||||
| // here is negligible. | // here is negligible. | ||||
| $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | ||||
| $image_file = PhabricatorFile::newFromFileDownload( | $image_file = PhabricatorFile::newFromFileDownload( | ||||
| $image_uri, | $image_uri, | ||||
| array( | array( | ||||
| 'name' => $name, | 'name' => $name, | ||||
| 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, | 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, | ||||
| )); | )); | ||||
| if ($image_file->isViewableImage()) { | if ($image_file->isViewableImage()) { | ||||
| ▲ Show 20 Lines • Show All 284 Lines • Show Last 20 Lines | |||||