/**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getNickname();
     $twitterId = $response->getUsername();
     $accessToken = $response->getAccessToken();
     $secretToken = $response->getTokenSecret();
     $user = $this->userManager->findUserByUsername($username);
     //when the user is registrating
     if (is_null($user)) {
         // create new user here
         $user = $this->userManager->createUser();
         $user->setUsername($username);
         $user->setAccessToken($accessToken);
         $user->setTwitterId($twitterId);
         $user->setSecretToken($secretToken);
         //I have set all requested data with the user's username
         //modify here with relevant data
         $user->setEmail($username);
         $user->setPlainPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user->setAccessToken($accessToken);
     $user->setSecretToken($secretToken);
     return $user;
 }
示例#2
0
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $uri = $this->request->getUri();
     $isFacebook = false;
     $isGoogle = false;
     $isLive = false;
     $isTwitter = false;
     if (strpos($uri, '/login_google') !== false) {
         $isGoogle = true;
     }
     if (strpos($uri, '/login_facebook') !== false) {
         $isFacebook = true;
     }
     if (strpos($uri, '/login_live') !== false) {
         $isLive = true;
     }
     if (strpos($uri, '/login/check-twitter') !== false) {
         $isTwitter = true;
     }
     if ($isGoogle === false && $isFacebook === false && $isLive === false && $isTwitter === false) {
         throw new \Exception("Invalid social network login attempt");
     }
     $social = "";
     if ($isGoogle) {
         $social = "google";
     }
     if ($isFacebook) {
         $social = "facebook";
     }
     if ($isLive) {
         $social = "live";
     }
     if ($isTwitter) {
         $social = "twitter";
     }
     //check to see if the user is logged in and if she is logged in with the same social network
     $isLoggedInAlready = $this->session->has('user');
     $isLoggedInAlreadyId = $this->session->get('user')['id'];
     if ($isLoggedInAlready && $this->session->get('user')['social'] == $social) {
         return $this->loadUserByUsername($isLoggedInAlreadyId);
     }
     $social_id = $response->getUsername();
     $nickname = $response->getNickname();
     $realName = $response->getRealName();
     $email = $response->getEmail();
     $avatar = $response->getProfilePicture();
     //set data in session. upon logging out we just erase the whole array.
     $sessionData = array();
     $sessionData['social_id'] = $social_id;
     $sessionData['nickname'] = $nickname;
     $sessionData['realName'] = $realName;
     $sessionData['email'] = $email;
     $sessionData['avatar'] = $avatar;
     $sessionData['social'] = $social;
     $user = null;
     if ($isLoggedInAlready) {
         $user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneById($isLoggedInAlreadyId);
     } else {
         if ($isFacebook) {
             $user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByFid($social_id);
         } else {
             if ($isGoogle) {
                 $user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByGid($social_id);
             } else {
                 if ($isLive) {
                     $user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByLid($social_id);
                 } else {
                     if ($isTwitter) {
                         $user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByTid($social_id);
                     }
                 }
             }
         }
     }
     if ($user == null) {
         $user = new User();
         $user->setSecret($response->getTokenSecret());
         $user->setToken($response->getAccessToken());
         //change these only the user hasn't been registered before.
     }
     if ($isFacebook) {
         $user->setFid($social_id);
     } else {
         if ($isGoogle) {
             $user->setGid($social_id);
         } else {
             if ($isLive) {
                 $user->setLid($social_id);
             } else {
                 if ($isTwitter) {
                     $user->setTid($social_id);
                 }
             }
         }
     }
     //$user->setLastLogin(new \DateTime('now'));
     //$user->setSocial($social);
     // SET E-MAIL
     //if all emails are empty, set the first one to this one.
     //save all changes
     $em = $this->doctrine->getManager();
     $em->persist($user);
     $em->flush();
     $id = $user->getId();
     //set id
     $sessionData['id'] = $id;
     $sessionData['is_admin'] = $this->adminChecker->check($user);
     $this->session->set('user', $sessionData);
     return $this->loadUserByUsername($user->getId());
 }