/** * this function will receive user data and ask user to enter his email in case new user * or will signin the user in case linkedIn user * @author Ahmed <*****@*****.**> */ public function linkedInUserDataAction() { //check that a logged in user can not access this action if (TRUE === $this->get('security.context')->isGranted('ROLE_NOTACTIVE')) { //go to the home page return $this->redirect('/'); } //get the request object $request = $this->getRequest(); //get the session object $session = $request->getSession(); //get the translator object $translator = $this->get('translator'); //get the oauth token from the session $oauth_token = $session->get('oauth_token', FALSE); //get the oauth token secret from the session $oauth_token_secret = $session->get('oauth_token_secret', FALSE); //get linkedIn oauth array from the session $linkedIn_oauth = $session->get('oauth_linkedin', FALSE); //check if we got linkedin data if ($oauth_token && $oauth_token_secret) { //get the user data $userData = LinkedinController::getUserData($this->container->getParameter('linkedin_api_key'), $this->container->getParameter('linkedin_secret_key'), $linkedIn_oauth); //check if we get the user data if ($userData) { $userData = $userData['linkedin']; $userData = json_decode(json_encode((array) simplexml_load_string($userData)), 1); //get the entity manager $em = $this->getDoctrine()->getManager(); //check if the user linkedId id is in our database $socialAccounts = $em->getRepository('ObjectsUserBundle:SocialAccounts')->findOneBy(array('linkedInId' => $userData['id'])); //check if we found the user if ($socialAccounts) { //user found check if the access tokens have changed if ($socialAccounts->getLinkedinOauthToken() != $oauth_token) { //tokens changed update the tokens $socialAccounts->setLinkedinOauthToken($oauth_token); $socialAccounts->setLinkedinOauthTokenSecret($oauth_token_secret); //save the new access tokens $em->flush(); } //get the user object $user = $socialAccounts->getUser(); //try to login the user try { // create the authentication token $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles()); // give it to the security context $this->container->get('security.context')->setToken($token); //redirect the user return $this->redirectUserAction(); } catch (\Exception $e) { //failed to login the user go to the login page return $this->redirect($this->generateUrl('login', array(), TRUE)); } } /** * * the account of the same email as linkedin account maybe exist but not linked so we will link it * and directly logging the user * if the account is not active we automatically activate it * else will create the account ,sign up the user * * */ $userRepository = $this->getDoctrine()->getRepository('ObjectsUserBundle:User'); $roleRepository = $this->getDoctrine()->getRepository('ObjectsUserBundle:Role'); $user = $userRepository->findOneByEmail($userData['email-address']); //if user exist only add linkedin account to social accounts record if user have one //if not create new record if ($user) { $socialAccounts = $user->getSocialAccounts(); if (empty($socialAccounts)) { $socialAccounts = new SocialAccounts(); $socialAccounts->setUser($user); } $socialAccounts->setLinkedinOauthToken($oauth_token); $socialAccounts->setLinkedinOauthTokenSecret($oauth_token_secret); $socialAccounts->setLinkedInId($userData['id']); $user->setSocialAccounts($socialAccounts); //activate user if is not activated //get object of notactive Role $notActiveRole = $roleRepository->findOneByName('ROLE_NOTACTIVE'); if ($user->getUserRoles()->contains($notActiveRole)) { //get a user role object $userRole = $roleRepository->findOneByName('ROLE_USER'); //remove notactive Role from user in exist $user->getUserRoles()->removeElement($notActiveRole); $user->getUserRoles()->add($userRole); $linkedInActivatedmessage = $this->get('translator')->trans('Your LinkedIN account was successfully Linked to your account') . ' ' . $this->get('translator')->trans('your account is now active'); //set flash message to tell user that him/her account has been successfully activated $session->getFlashBag()->set('notice', $linkedInActivatedmessage); } else { $linkedInDmessage = $this->get('translator')->trans('Your LinkedIN account was successfully Linked to your account'); //set flash message to tell user that him/her account has been successfully linked $session->getFlashBag()->set('notice', $linkedInDmessage); } $em->persist($user); $em->flush(); //try to login the user try { // create the authentication token $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles()); // give it to the security context $this->get('security.context')->setToken($token); //redirect the user return $this->redirectUserAction(); } catch (\Exception $e) { //can not reload the user object log out the user $this->get('security.context')->setToken(null); //invalidate the current user session $this->getRequest()->getSession()->invalidate(); //redirect to the login page return $this->redirect($this->generateUrl('login', array(), TRUE)); } } //create a new user object $user = new User(); //get the container object $container = $this->container; $newUserName = ''; //set the name if (isset($userData['first-name'])) { $user->setFirstName($userData['first-name']); $newUserName = $userData['first-name']; } if (isset($userData['last-name'])) { $user->setLastName($userData['last-name']); $newUserName .= '_' . $userData['last-name']; } //set a valid login name $user->setLoginName($this->suggestLoginName($newUserName)); //set the profile url if (isset($userData['site-standard-profile-request']['url'])) { $user->setUrl($userData['site-standard-profile-request']['url']); } //set the about text if (isset($userData['summary'])) { $user->setAbout($userData['summary']); } //set user country code if (isset($userData['location']['country']['code'])) { $user->setCountryCode($userData['location']['country']['code']); } //try to download the user image from linkedIn if user has one if (isset($userData['picture-url'])) { $image = LinkedinController::downloadLinkedInImage($userData['picture-url'], $user->getUploadRootDir()); //check if we got an image if ($image) { //add the image to the user $user->setImage($image); } } //set the user email if (isset($userData['email-address'])) { $user->setEmail($userData['email-address']); } //set the user dateOfBirth if (isset($userData['date-of-birth'])) { $user->setDateOfBirth(new \DateTime($userData['date-of-birth']['year'] . '-' . $userData['date-of-birth']['month'] . '-' . $userData['date-of-birth']['day'])); } //create social accounts object $socialAccounts = new SocialAccounts(); $socialAccounts->setLinkedinOauthToken($oauth_token); $socialAccounts->setLinkedinOauthTokenSecret($oauth_token_secret); $socialAccounts->setLinkedInId($userData['id']); $socialAccounts->setUser($user); //set the user linkedIn info $user->setSocialAccounts($socialAccounts); //user data are valid finish the signup process return $this->finishSignUp($user); } else { //linkedIn data not found go to the login page return $this->redirect($this->generateUrl('login', array(), TRUE)); } } else { //linkedIn data not found go to the login page return $this->redirect($this->generateUrl('login', array(), TRUE)); } }