public function loadUserByOAuthUserResponse(UserResponseInterface $response) { $username = $response->getUsername(); $email = $response->getEmail(); $service = $response->getResourceOwner()->getName(); /** @var UserOauthAccount $connection */ $connection = $this->em->getRepository('OjsUserBundle:UserOauthAccount')->findOneBy(['providerId' => $username, 'provider' => $service]); if (!$connection && !empty($email)) { $userByEmail = $this->userManager->findUserByEmail($email); if ($userByEmail) { $connection = new UserOauthAccount(); $connection->setUser($userByEmail); $connection->setProvider($service); $connection->setProviderId($response->getUsername()); } } if (!$connection || $connection->getUser() === null) { $message = sprintf("User not found. Please register first and then connect the account from your profile.", $username); throw new AccountNotLinkedException($message); } $connection->setToken($response->getAccessToken()); $this->em->persist($connection); $this->em->flush(); return $connection->getUser(); }
public function loadUserByOAuthUserResponse(UserResponseInterface $response) { $username = $response->getUsername(); $email = $response->getEmail(); $service = $response->getResourceOwner()->getName(); /** @var UserOauthAccount $connection */ $connection = $this->em->getRepository('OjsUserBundle:UserOauthAccount')->findOneBy(['providerId' => $username, 'provider' => $service]); if (!$connection && !empty($email)) { $userByEmail = $this->userManager->findUserByEmail($email); if ($userByEmail) { $connection = new UserOauthAccount(); $connection->setUser($userByEmail); $connection->setProvider($service); $connection->setProviderId($response->getUsername()); } } if (!$connection || $connection->getUser() === null) { if (empty($response->getEmail())) { $message = sprintf("User not found. Please register first and then connect the account from your profile.", $username); throw new AccountNotLinkedException($message); } $fullName = $response->getRealName(); $parts = explode(" ", $fullName); $lastname = array_pop($parts); $firstname = implode(" ", $parts); $user = $this->userManager->createUser(); $user->setEnabled(true); $user->setUsername($response->getUsername()); $user->setEmail($response->getEmail()); $user->setPlainPassword(bin2hex(random_bytes(5))); $user->setFirstName($firstname); $user->setLastName($lastname); $this->userManager->updateUser($user); $connection = new UserOauthAccount(); $connection->setUser($user); $connection->setProvider($service); $connection->setProviderId($response->getUsername()); } $connection->setToken($response->getAccessToken()); $this->em->persist($connection); $this->em->flush(); return $connection->getUser(); }