diff --git a/src/applications/auth/controller/PhabricatorAuthRegisterController.php b/src/applications/auth/controller/PhabricatorAuthRegisterController.php --- a/src/applications/auth/controller/PhabricatorAuthRegisterController.php +++ b/src/applications/auth/controller/PhabricatorAuthRegisterController.php @@ -457,7 +457,6 @@ if (!$is_setup) { $account->setUserPHID($user->getPHID()); - $provider->willRegisterAccount($account); $account->save(); } diff --git a/src/applications/auth/provider/PhabricatorAuthProvider.php b/src/applications/auth/provider/PhabricatorAuthProvider.php --- a/src/applications/auth/provider/PhabricatorAuthProvider.php +++ b/src/applications/auth/provider/PhabricatorAuthProvider.php @@ -186,11 +186,9 @@ return; } - public function willRegisterAccount(PhabricatorExternalAccount $account) { - return; - } + final protected function newExternalAccountForIdentifiers( + array $identifiers) { - protected function loadOrCreateAccount(array $identifiers) { assert_instances_of($identifiers, 'PhabricatorExternalAccountIdentifier'); if (!$identifiers) { @@ -234,6 +232,38 @@ implode(', ', $raw_identifiers))); } + return $this->didUpdateAccount($account); + } + + final protected function newExternalAccountForUser(PhabricatorUser $user) { + $config = $this->getProviderConfig(); + + // When a user logs in with a provider like username/password, they + // always already have a Phabricator account (since there's no way they + // could have a username otherwise). + + // These users should never go to registration, so we're building a + // dummy "external account" which just links directly back to their + // internal account. + + $account = id(new PhabricatorExternalAccountQuery()) + ->setViewer($user) + ->withProviderConfigPHIDs(array($config->getPHID())) + ->withUserPHIDs(array($user->getPHID())) + ->executeOne(); + if (!$account) { + $account = $this->newExternalAccount() + ->setUserPHID($user->getPHID()); + + // TODO: Remove this when "accountID" is removed; the column is not + // nullable. + $account->setAccountID(''); + } + + return $this->didUpdateAccount($account); + } + + private function didUpdateAccount(PhabricatorExternalAccount $account) { $adapter = $this->getAdapter(); $account->setUsername($adapter->getAccountName()); diff --git a/src/applications/auth/provider/PhabricatorLDAPAuthProvider.php b/src/applications/auth/provider/PhabricatorLDAPAuthProvider.php --- a/src/applications/auth/provider/PhabricatorLDAPAuthProvider.php +++ b/src/applications/auth/provider/PhabricatorLDAPAuthProvider.php @@ -180,7 +180,9 @@ } } - return array($this->loadOrCreateAccount($identifiers), $response); + $account = $this->newExternalAccountForIdentifiers($identifiers); + + return array($account, $response); } diff --git a/src/applications/auth/provider/PhabricatorOAuth1AuthProvider.php b/src/applications/auth/provider/PhabricatorOAuth1AuthProvider.php --- a/src/applications/auth/provider/PhabricatorOAuth1AuthProvider.php +++ b/src/applications/auth/provider/PhabricatorOAuth1AuthProvider.php @@ -115,7 +115,9 @@ return array($account, $response); } - return array($this->loadOrCreateAccount($identifiers), $response); + $account = $this->newExternalAccountForIdentifiers($identifiers); + + return array($account, $response); } public function processEditForm( diff --git a/src/applications/auth/provider/PhabricatorOAuth2AuthProvider.php b/src/applications/auth/provider/PhabricatorOAuth2AuthProvider.php --- a/src/applications/auth/provider/PhabricatorOAuth2AuthProvider.php +++ b/src/applications/auth/provider/PhabricatorOAuth2AuthProvider.php @@ -95,7 +95,9 @@ return array($account, $response); } - return array($this->loadOrCreateAccount($identifiers), $response); + $account = $this->newExternalAccountForIdentifiers($identifiers); + + return array($account, $response); } public function processEditForm( diff --git a/src/applications/auth/provider/PhabricatorPasswordAuthProvider.php b/src/applications/auth/provider/PhabricatorPasswordAuthProvider.php --- a/src/applications/auth/provider/PhabricatorPasswordAuthProvider.php +++ b/src/applications/auth/provider/PhabricatorPasswordAuthProvider.php @@ -305,7 +305,7 @@ ->setObject($user); if ($engine->isValidPassword($envelope)) { - $account = $this->loadOrCreateAccount($user->getPHID()); + $account = $this->newExternalAccountForUser($user); $log_user = $user; } } @@ -339,16 +339,6 @@ return true; } - protected function willSaveAccount(PhabricatorExternalAccount $account) { - parent::willSaveAccount($account); - $account->setUserPHID($account->getAccountID()); - } - - public function willRegisterAccount(PhabricatorExternalAccount $account) { - parent::willRegisterAccount($account); - $account->setAccountID($account->getUserPHID()); - } - public static function getPasswordProvider() { $providers = self::getAllEnabledProviders(); @@ -375,4 +365,5 @@ public function shouldAllowEmailTrustConfiguration() { return false; } + }