Пример #1
0
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $google_id = $response->getUsername();
     $email = $response->getEmail();
     $nickname = $response->getNickname();
     $realname = $response->getRealName();
     $avatar = $response->getProfilePicture();
     $this->session->set('email', $email);
     $this->session->set('nickname', $nickname);
     $this->session->set('realname', $realname);
     $this->session->set('avatar', $avatar);
     $qb = $this->doctrine->getManager()->createQueryBuilder();
     $qb->select('u')->from('BookBundle:User', 'u')->where('u.googleId = :gid')->setParameter('gid', $google_id)->setMaxResults(1);
     $result = $qb->getQuery()->getResult();
     if (!count($result)) {
         $user = new User();
         $user->setUsername($google_id);
         $user->setRealname($realname);
         $user->setNickname($nickname);
         $user->setEmail($email);
         $user->setGoogleId($google_id);
         $user->setLocked(false);
         $factory = $this->container->get('security.encoder_factory');
         $encoder = $factory->getEncoder($user);
         $password = $encoder->encodePassword(md5(uniqid(), $user->getSalt()));
         $user->setPassword($password);
         $em = $this->doctrine->getManager();
         $em->persist($user);
         $em->flush();
     } else {
         $user = $result[0];
     }
     $this->session->set('id', $user->getId());
     return $this->loadUserByUsername($response->getUsername());
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $resourceOwner = $response->getResourceOwner();
     if (!$resourceOwner instanceof FacebookResourceOwner) {
         throw new UnsupportedUserException('Only Facebook users are supported');
     }
     return $this->loadUserByData(['userId' => $response->getUsername(), 'service' => 'facebook', 'username' => $response->getNickname(), 'realname' => $response->getRealName(), 'email' => $response->getEmail(), 'picture' => $response->getProfilePicture()]);
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response, $email = "")
 {
     $useremail = $response->getEmail();
     if ($useremail == "") {
         $useremail = $email;
     }
     if ($useremail != "") {
         $username = $useremail;
     } else {
         $username = $response->getUsername();
     }
     /** @var User $user */
     $user = $this->userManager->findUserByUsernameOrEmail($username);
     $service = $response->getResourceOwner()->getName();
     $setterID = $service . "Id";
     $setter = 'set' . ucfirst($service);
     $setter_id = $setter . 'Id';
     $setter_token = $setter . 'AccessToken';
     $getter = 'get' . ucfirst($service);
     $getter_id = $getter . 'Id';
     //when the user is registrating
     if (null === $user) {
         if ($this->userManager->findUserBy(array($setterID => $response->getUsername())) != null) {
             $user = $this->userManager->findUserBy(array($setterID => $response->getUsername()));
             return $user;
         } else {
             // create new user here
             $user = $this->userManager->createUser();
             $user->{$setter_id}($username);
             $user->{$setter_token}($response->getAccessToken());
             $user->setUsername($username);
             $user->setFullname($response->getRealName());
             if ($response->getResourceOwner()->getName() == 'facebook') {
                 $user->setPhoto('https://graph.facebook.com/' . $response->getUsername() . '/picture?type=large');
             } else {
                 $user->setPhoto($response->getProfilePicture());
             }
             $user->setPassword($username);
             $user->setEnabled(true);
             if (filter_var($response->getEmail(), FILTER_VALIDATE_EMAIL)) {
                 $user->setEmail($response->getEmail());
                 $user->setConfirmationToken(null);
             } else {
                 $user->setEmail($username);
             }
             $this->userManager->updateUser($user);
             return $user;
         }
     } else {
         $user->{$setter_id}($response->getUsername());
         $user->{$setter_token}($response->getAccessToken());
         $this->userManager->updateUser($user);
         return $user;
     }
 }
Пример #4
0
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        //Data from Google response
        $social_id = $response->getUsername(); /* An ID like: 112259658235204980084 */
        $email = $response->getEmail();
        $nickname = $response->getNickname();
        $realname = $response->getRealName();
        $avatar = $response->getProfilePicture();

        //set data in session
        $this->session->set('email', $email);
        $this->session->set('nickname', $nickname);
        $this->session->set('realname', $realname);
        $this->session->set('avatar', $avatar);

        //Check if this Google user already exists in our app DB
        $qb = $this->doctrine->getManager()->createQueryBuilder();
        $qb->select('u')
            ->from('BloggerBlogBundle:User', 'u')
            ->where('u.socialId = :sid')
            ->setParameter('sid', $social_id)
            ->setMaxResults(1);
        $result = $qb->getQuery()->getResult();

        //add to database if doesn't exists
        if (!count($result)) {
            $user = new User();
            $user->setUsername($social_id);
            $user->setRealname($realname);
            $user->setNickname($nickname);
            $user->setEmail($email);
            $user->setSocialId($social_id);
            //$user->setRoles('ROLE_USER');

            //Set some wild random pass since its irrelevant, this is Google login
            $factory = $this->container->get('security.encoder_factory');
            $encoder = $factory->getEncoder($user);
            $password = $encoder->encodePassword(md5(uniqid()), $user->getSalt());
            $user->setPassword($password);

            $em = $this->doctrine->getManager();
            $em->persist($user);
            $em->flush();
        } else {
            $user = $result[0]; /* return User */
        }

        //set id
        $this->session->set('id', $user->getId());

        return $this->loadUserByUsername($response->getUsername());
    }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 protected function updateUserByOAuthUserResponse(UserInterface $user, UserResponseInterface $response)
 {
     /** @var UserOAuthInterface $oauth */
     $oauth = $this->oauthFactory->createNew();
     $oauth->setIdentifier($response->getUsername());
     $oauth->setProvider($response->getResourceOwner()->getName());
     $oauth->setAccessToken($response->getAccessToken());
     $oauth->setProfilePicture($response->getProfilePicture());
     /* @var DoSUserInterface $user */
     $user->addOAuthAccount($oauth);
     $this->userManager->persist($user);
     $this->userManager->flush();
     return $user;
 }
Пример #6
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_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('CodeMe\\TheBundle\\Entity\\User')->findOneById($isLoggedInAlreadyId);
     } else {
         if ($isFacebook) {
             $user = $this->doctrine->getRepository('CodeMe\\TheBundle\\Entity\\User')->findOneByFid($social_id);
         } else {
             if ($isGoogle) {
                 $user = $this->doctrine->getRepository('CodeMe\\TheBundle\\Entity\\User')->findOneByGid($social_id);
             } else {
                 if ($isLive) {
                     $user = $this->doctrine->getRepository('CodeMe\\TheBundle\\Entity\\User')->findOneByLid($social_id);
                 } else {
                     if ($isTwitter) {
                         $user = $this->doctrine->getRepository('CodeMe\\TheBundle\\Entity\\User')->findOneByTid($social_id);
                     }
                 }
             }
         }
     }
     if ($user == null) {
         $user = new User();
         //change these only the user hasn't been registered before.
         $user->setNickname($nickname);
         $user->setRealname($realName);
         $user->setAvatar($avatar);
     }
     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.
     if ($user->getEmail() == "") {
         $user->setEmail($email);
     } else {
         //if it really is an e-mail, try putting it in email2 or email3
         if ($email != "") {
             //is the e-mail different than the previous one?
             if ($email != $user->getEmail()) {
                 //if there an e-mail in email2? no:
                 if ($user->getEmail2() == "") {
                     $user->setEmail2($email);
                 } else {
                     //there is an e-mail in email2 and it's different. fall back to setting the user3 to w/e.
                     if ($user->getEmail2() != $email) {
                         $user->setEmail3($email);
                     }
                 }
             }
         }
     }
     //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());
 }
Пример #7
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $sessionArr = $this->session->all();
     $resourceOwnerName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($resourceOwnerName);
     $setter_id = $setter . 'Id';
     $setter_token = $setter . 'AccessToken';
     if (!isset($this->properties[$resourceOwnerName])) {
         throw new \RuntimeException(sprintf("No property defined for entity for resource owner '%s'.", $resourceOwnerName));
     }
     $username = $response->getUsername();
     if (isset($sessionArr['_security_secured_area'])) {
         //when user already logged and connect with other social network
         $instance = unserialize($sessionArr['_security_secured_area']);
         $user = $this->repository->findOneById($instance->getUser()->getId());
         if (!$user instanceof Actor) {
             throw new \RuntimeException("_security_secured_area key exist but any user have been stored ");
         }
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $this->em->flush();
         return $user;
     } else {
         //when user not logged and connect with other social network
         $user = $this->repository->findOneBy(array($this->properties[$resourceOwnerName] => $username));
         if (null === $user && $resourceOwnerName != 'twitter') {
             $user = $this->repository->findOneBy(array('email' => $response->getEmail()));
         }
         //when the user is registrating
         if (null === $user) {
             // create new user here
             $user = new Actor();
             $user->{$setter_id}($username);
             $user->{$setter_token}($response->getAccessToken());
             //Encode pass
             $encoder = $this->encoderFactory->getEncoder($user);
             $password = $encoder->encodePassword($username, $user->getSalt());
             $user->setPassword($password);
             //Add ROLE
             $role = $this->em->getRepository('CoreBundle:Role')->findOneBy(array('role' => 'ROLE_USER'));
             $user->addRole($role);
             //I have set all requested data with the user's username
             //modify here with relevant data
             if (isset($oauthData['name'])) {
                 $user->setName($oauthData['name']);
             } else {
                 $user->setName($username);
             }
             $user->setUsername($username);
             if ($resourceOwnerName == 'twitter') {
                 $user->setEmail($username);
             } else {
                 $user->setEmail($response->getEmail());
             }
             $user->setIsActive(true);
             $this->em->persist($user);
             $this->em->flush();
         }
         //we need response data to update or fill: username, name, profile image
         $this->updateOAuthData($user, array('owner' => $resourceOwnerName, 'id' => $username, 'name' => $response->getRealName(), 'profileImage' => $response->getProfilePicture()));
         return $user;
     }
     //        return $user;
 }
 /**
  * {@inheritDoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     return new MyECPUser($response->getUsername(), $response->getNickname(), $response->getEmail(), "https://my.ecp.fr" . $response->getProfilePicture(), $response->getRealName());
 }