/** * @Route("/socialhub/connector", name="socialhub_connector") * @Template() */ public function indexAction(Request $request) { $request->getSession()->start(); $social_provider_id = $request->get('type'); $provider_prefix = "socialhub_provider_"; $socialProvider = $this->get($provider_prefix . $social_provider_id); $profile = $socialProvider->getUserProfile(); $em = $this->getDoctrine()->getManager(); $userSocialAccount = $em->getRepository('FlowcodeSocialHubBundle:UserSocialAccount')->findOneBy(array('externalId' => $profile['id'])); if ($profile) { if (!$userSocialAccount) { $accessToken = $socialProvider->getAccessToken(); /* get social network */ $socialNetwork = $em->getRepository('FlowcodeSocialHubBundle:SocialNetwork')->findOneBy(array('type' => $social_provider_id)); if (!$socialNetwork) { die('No social network'); } $userManager = $this->container->get('flowcode.user'); $user = $userManager->loadUserByUsername($profile['email']); if (!$user) { $user = new User(); $user->setUsername($profile['email']); $user->setEmail($profile['email']); $user->setFirstname($profile['firstname']); $user->setLastname($profile['lastname']); $user->setPlainPassword($profile['id']); $user->setStatus(User::STATUS_ACTIVE); $userManager->create($user); } $userSocialAccount = new UserSocialAccount(); $userSocialAccount->setUser($user); $userSocialAccount->setSocialNetwork($socialNetwork); $userSocialAccount->setExternalId($profile['id']); $userSocialAccount->setAccessToken($accessToken); $em->persist($userSocialAccount); $em->flush(); $this->authenticateUser($request, $user); } else { $this->authenticateUser($request, $userSocialAccount->getUser()); } } if ($this->container->hasParameter('socialhub_redirect')) { $redirectUrl = $this->getParameter('socialhub_redirect'); return $this->redirect($this->generateUrl($redirectUrl)); } return $this->redirect($this->generateUrl('homepage')); }