public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $generator = new SecureRandom();
     $random = $generator->nextBytes(10);
     $data = $response->getResponse();
     $username = $response->getUsername();
     $email = $response->getEmail() ? $response->getEmail() : $username;
     $service = $response->getResourceOwner()->getName();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //$user = $this->userManager->findUserBy(array($service.'Id' => $username));
     //when the user is registrating
     if (null === $user) {
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $user->setUsername($email);
         $user->setEmail($email);
         $user->setPlainPassword($random);
         $user->setName($response->getRealName());
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //when the user is registrating
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         // create new user here
         /** @var User $user */
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->setUsername($response->getNickname());
         $user->setEmail($response->getEmail());
         $user->setPassword('');
         $user->setEnabled(true);
         $socialData = $response->getResponse();
         if ($service == 'vkontakte') {
             $socialData = $socialData['response'][0];
         }
         $user->setFirstname($socialData['first_name']);
         $user->setLastname($socialData['last_name']);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     return $user;
 }
Esempio n. 3
0
 /**
  * @param UserResponseInterface $response
  * @return User
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     /** @var User $user */
     $user = $this->userManager->findUserBy(['slackUserName' => $response->getUsername()]);
     if ($user === null) {
         $user = $this->userManager->createUser();
         $user->setSlackUserName($username);
         $user->setSlackAccessToken($response->getAccessToken());
         $user->setUsername($response->getNickname());
         $user->setEmail($response->getEmail());
         $user->setRealName($response->getRealName());
         $user->setPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         $token = new Token($user);
         $this->tokenRepository->persist($token);
         $user->addToken($token);
         $this->userManager->updateUser($user);
         return $user;
     }
     $user = parent::loadUserByOAuthUserResponse($response);
     $user->setSlackAccessToken($response->getAccessToken());
     $user->setRealName($response->getRealName());
     $token = new Token($user);
     $this->tokenRepository->persist($token);
     $user->addToken($token);
     $this->userManager->updateUser($user);
     return $user;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //when the user is registering
     if (null === $user) {
         $realName = $response->getRealName();
         if ($realName !== null) {
             $extractName = explode(" ", $realName);
             $firstName = $extractName[0];
             $lastName = end($extractName);
         }
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $user->setUsername($response->getNickname());
         $user->setEmail($response->getEmail());
         $user->setPlainPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserByUsernameOrEmail($response->getEmail());
     //$user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     if (null === $user) {
         $user = $this->userManager->createUser();
         $this->linkUserToOAuthAccount($response, $user);
         $user->setUsername($response->getUsername());
         if ($response->getEmail() == null) {
             $email = $response->getUsername() . "@ddd.com";
         } else {
             $email = $response->getEmail();
         }
         $user->setEmail($email);
         $user->setPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     try {
         $user = parent::loadUserByOAuthUserResponse($response);
     } catch (AccountNotLinkedException $e) {
         $this->linkUserToOAuthAccount($response, $user);
         $this->userManager->updateUser($user);
     }
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $service = $response->getResourceOwner()->getName();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username, 'auth_type' => $service));
     //when the user is registrating
     if (null === $user) {
         $user = $this->userManager->createUser();
         $user->setUid($username);
         $user->setAccessToken($response->getAccessToken());
         $user->setAuthType($service);
         //I have set all requested data with the user's username
         //modify here with relevant data
         $user->setUsername($response->getRealName());
         $user->setUsernameCanonical($username);
         $user->setEmail($response->getEmail());
         if ($service == "facebook") {
             $user->setPicture("https://graph.facebook.com/{$username}/picture");
         } else {
             $user->setPicture($response->getProfilePicture());
         }
         $user->setPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     //update access token
     $user->setAccessToken($response->getAccessToken());
     return $user;
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $email = $response->getEmail();
     $nickname = $response->getNickname();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //when the user is registrating
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         //I have set all requested data with the user's username
         //modify here with relevant data
         $user->setUsername($nickname);
         $user->setEmail($email);
         $user->setPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 8
0
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $realName = $response->getRealName();
     /** @var User $user */
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setterId = $setter . 'Id';
         $setterToken = $setter . 'AccessToken';
         $user = $this->userManager->createUser();
         $user->{$setterId}($username);
         $user->{$setterToken}($response->getAccessToken());
         $username = sprintf('%s_%s', $service, $username);
         $user->setUsername($username);
         $user->setRealName($realName);
         $user->setEmail($username);
         $user->setPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /**
  * {@inheritdoc}
  * @todo use translations to return error messages and not hardcoded english sentences
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $user = $this->userManager->createUser();
         //we check for the username existence - if so, redirect to login.
         if ($existent_user = $this->userManager->findUserByUsername($response->getUsername())) {
             $message = $this->get('translator')->trans('Username %name% already connected.', array('%name%' => $response->getUsername()));
             $logger->warning(sprintf('%s.[%s].[%s] => %s', 'AuthenticationException', 'AuthenticateUser exception.', gettype($user), $user));
             throw new AuthenticationException(sprintf("'%s'", $message));
         }
         if (null !== $username) {
             switch ($service) {
                 case 'google':
                     $user = $this->loadGoogleUser($response);
                     break;
                 case 'facebook':
                     $user = $this->loadFacebookUser($response);
                     break;
                 case 'twitter':
                     $user = $this->loadTwitterUser($response);
                     break;
             }
         }
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     return $user;
 }
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $data = $response->getResponse();
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     $service = $response->getResourceOwner()->getName();
     //when the user is registrating
     if (null === $user) {
         if ($service == "google") {
             $service = "gplus";
         }
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Uid';
         $setter_token = $setter . 'Name';
         // create new user here
         // TODO: Check email, if exist add service
         $userE = $this->userManager->findUserBy(array("email" => $response->getEmail()));
         if (null === $userE) {
             $user = $this->userManager->createUser();
             $user->setUsername($response->getEmail());
             if ($service == "gplus") {
                 $user->setFirstname($data['given_name']);
             }
             if ($service == "facebook") {
                 $name = explode(" ", $data['name']);
                 $user->setFirstname($name[0]);
                 $user->setLastname($name[1]);
             }
             $user->setEmail($response->getEmail());
             $user->setPassword("");
         } else {
             $user = $userE;
         }
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $user->setEnabled(true);
         // Customfields
         $user->setProfilePicture($response->getProfilePicture());
         //TODO: Save Locale, $user->setLocale($response->getLocale());
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     if ($serviceName == "google") {
         $serviceName = "gplus";
     }
     $setter = 'set' . ucfirst($serviceName) . 'Name';
     //update access token
     $user->{$setter}($response->getAccessToken());
     //update custom fields
     //TODO: Check google response, facebook?
     if ($service == "facebook") {
         $url = "http://graph.facebook.com/" . $data['id'] . "/picture?type=normal";
         $user->setProfilePicture($url);
     }
     return $user;
 }
Esempio n. 11
0
 /**
  * @param UserResponseInterface $response
  *
  * @return UserInterface
  */
 private function initExistingUser(UserResponseInterface $response)
 {
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     // update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 12
0
 /**
  * {@inheritDoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     try {
         return parent::loadUserByOAuthUserResponse($response);
     } catch (UsernameNotFoundException $e) {
         if (null === ($user = $this->userManager->findUserByEmail($response->getEmail()))) {
             return $this->createUserByOAuthUserResponse($response);
         }
         return $this->updateUserByOAuthUserResponse($user, $response);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $serviceAccessTokenSetter = 'set' . ucfirst($serviceName) . 'AccessToken';
     $serviceAccessTokenGetter = 'get' . ucfirst($serviceName) . 'AccessToken';
     if (method_exists($user, $serviceAccessTokenSetter) && method_exists($user, $serviceAccessTokenGetter) && $user->{$serviceAccessTokenGetter}() !== $response->getAccessToken()) {
         $user->{$serviceAccessTokenSetter}($response->getAccessToken());
         $this->userManager->updateUser($user);
     }
     return $user;
 }
Esempio n. 14
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $email = $response->getEmail();
     //Check if already registered with this social network
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //Check if already registered with a different social network
     if ($email != '' and null === $user) {
         $user = $this->userManager->findUserBy(array('emailCanonical' => strtolower($email)));
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $this->userManager->updateUser($user);
     }
     //when the user is registrating
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         //Registrating data
         $user->setUsername($username);
         $user->setEmail($response->getEmail());
         $user->setFirstName($response->getFirstName());
         $user->setLastName($response->getLastName());
         $user->setPassword($username);
         $user->addRole('ROLE_USER');
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         //Business logic data
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     $this->userManager->updateUser($user);
     return $user;
 }
Esempio n. 15
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByoAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy([$this->getProperty($response) => $username]);
     if (null === $user) {
         return $this->doRegistration($response);
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     // do not set access token due to security concerns
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 16
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     //  echo $username;die();
     /** @var User $user */
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //when the user is registrating
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // 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/' . $username . '/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 {
         if ($user->getEmail() != $response->getEmail() && filter_var($response->getEmail(), FILTER_VALIDATE_EMAIL)) {
             $user->setEmail($response->getEmail());
             $this->userManager->updateUser($user);
         }
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 17
0
 /**
  * this method is to register new users in the database.
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     //$backTrace = json_encode(debug_backtrace());
     $responseData = json_encode($response->getResponse());
     $this->logger->info('Calling loadUserByOAuthUserResponse from \\n' . $responseData);
     $service = $response->getResourceOwner()->getName();
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //when the user is registrating
     if (null === $user) {
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         $setter_data = $setter . 'RawData';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $user->{$setter_data}($responseData);
         $user->setUsername($response->getNickname());
         $user->setPlainPassword('Welcome1!');
         if (strlen($response->getEmail()) >= 5) {
             $user->setEmail($response->getEmail());
         } else {
             $user->setEmail($username . '@' . $service . '.com');
         }
         $user->setFirstName($response->getFirstName());
         $user->setLastName($response->getLastName());
         $user->setPicture($response->getProfilePicture());
         $this->userManager->updateCanonicalFields($user);
         $this->userManager->updatePassword($user);
         $user->setEnabled(true);
         $this->logger->info('Persisting new User ' . $user->serialize());
         $this->userManager->updateUser($user);
         return $user;
     }
     $this->logger->info('OAuth User ' . $username . ' already exists -  go with the HWIOAuth way');
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 18
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     //throw new \RuntimeException('Test error');
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $user = $this->userManager->createUser();
         //we check for the email existence - if so, redirect to login.
         if ($existent_user = $this->userManager->findUserByEmail($response->getEmail())) {
             throw new \Symfony\Component\Security\Core\Exception\AuthenticationException(sprintf("Email '%s' already in use.", $response->getEmail()));
         }
         //we check for the username existence - if so, redirect to login.
         if ($existent_user = $this->userManager->findUserByUsername($response->getEmail())) {
             throw new \Symfony\Component\Security\Core\Exception\AuthenticationException(sprintf("Username '%s' already in use.", $response->getEmail()));
         }
         switch ($service) {
             case 'google':
                 $user = $this->loadGoogleUser($response);
                 break;
             case 'facebook':
                 $user = $this->loadFacebookUser($response);
                 break;
             case 'github':
                 $user = $this->loadGithubUser($response);
                 break;
         }
         //the user needs to be reloaded in order to assign roles...
         //don't know why, but this is the ONLY way it worked
         $user1 = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
         $user1->addRole('ROLE_ADMIN');
         $this->userManager->updateUser($user1);
         return $user1;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 19
0
 /** {@inheritdoc} */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     try {
         return parent::loadUserByOAuthUserResponse($response);
     } catch (AccountNotLinkedException $e) {
         //persist user on first login
         $user = User::fromOAuthResponse($response);
         //maybe we already know this user from different provider?
         $alreadySignedUser = $this->userRepository->findBy(['email' => $user->getEmail()]);
         if (count($alreadySignedUser)) {
             /** @var User $userTmp */
             $userTmp = $alreadySignedUser[0];
             $userTmp->setPictureUrl($user->getPictureUrl());
             $user = $userTmp;
         }
         $this->em->persist($user);
         $this->em->flush();
         return $user;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     try {
         $user = parent::loadUserByOAuthUserResponse($response);
         if ($this->eventDispatcher) {
             $event = new UserProviderEvent($user, $response);
             $this->eventDispatcher->dispatch(self::USER_LOGIN_EVENT, $event);
             $this->userManager->updateUser($user);
         }
     } catch (AccountNotLinkedException $e) {
         $user = $this->userManager->createUser();
         $user->setUsername($response->getNickname());
         $user->setEmail($response->getEmail());
         $user->setPlainPassword($response->getAccessToken());
         $user->setEnabled(true);
         if ($this->eventDispatcher) {
             $event = new UserProviderEvent($user, $response);
             $this->eventDispatcher->dispatch(self::USER_REGISTRATION_EVENT, $event);
         }
         $this->userManager->updateUser($user);
     }
     return $user;
 }
Esempio n. 21
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $email = $response->getEmail();
     $user = $this->userManager->findUserBy(array('email' => $email));
     //when the user is registrating
     $service = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($service);
     $setter_id = $setter . 'Id';
     $setter_token = $setter . 'AccessToken';
     if (null === $user) {
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         //I have set all requested data with the user's username
         //modify here with relevant data
         $user->setUsername($response->getNickname());
         $user->setEmail($email);
         $user->setPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         $event = new OAuthResponseEvent($user, $response);
         $this->eventDispatcher->dispatch(OesteveUserEvents::REGISTRATION_OAUTH_COMPLETED, $event);
         return $user;
     }
     $user->{$setter_id}($username);
     $this->userManager->updateUser($user);
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 22
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //when the user is registrating
     if (null === $user) {
         //$response->getResponse() => ARRAY CON MOLTI CAMPI
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         //I have set all requested data with the user's username
         //modify here with relevant data
         $user->setEmail($username);
         switch ($service) {
             case 'twitter':
                 $user->setUsername($response->getResponse()["screen_name"]);
                 $user->setNickname("TW_" . $response->getResponse()["screen_name"] . uniqid());
                 $user->setLocale($response->getResponse()["lang"]);
                 $user->setAvatar($response->getResponse()["profile_image_url_https"]);
                 break;
             case 'facebook':
                 $user->setFirstName($response->getResponse()["first_name"]);
                 $user->setLastName($response->getResponse()["last_name"]);
                 $user->setNickname("FB_" . $response->getResponse()["username"] . uniqid());
                 $user->setUsername($response->getResponse()["name"]);
                 $user->setGender($response->getResponse()["gender"]);
                 $user->setBirthday($response->getResponse()["birthday"], \Ephp\ACLBundle\Entity\User::FACEBOOK);
                 $user->setEmail($response->getEmail());
                 $user->setAvatar($response->getProfilePicture());
                 $user->setLocale($response->getResponse()["locale"]);
                 break;
                 $user->setUsername($username);
             default:
         }
         //                $utente = $this->returnTweet($username); ALTRO METODO PER PRENDERE I DATI, PER ORA COMMENTO
         $user->setPassword($username);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         /**
          * Creazione Profilo
          */
         //            $this->em->beginTransaction();
         //            $profilo = new \SN\ProfiloBundle\Entity\Profilo();
         //            $profilo->setSlug($user->getNickname());
         //            $profilo->setNickname($user->getNickname());
         //            $profilo->setUtente($user);
         //            $this->em->persist($profilo);
         //            $this->em->flush();
         //            $this->em->commit();
         return $user;
     }
     switch ($response->getResourceOwner()->getName()) {
         case 'twitter':
             $user->setUsername($response->getResponse()["screen_name"]);
             $user->setLocale($response->getResponse()["lang"]);
             $user->setAvatar($response->getResponse()["profile_image_url_https"]);
             break;
         case 'facebook':
             $user->setFirstName($response->getResponse()["first_name"]);
             $user->setLastName($response->getResponse()["last_name"]);
             //$user->setNickname($response->getResponse()["username"]);
             $user->setUsername($response->getResponse()["name"]);
             $user->setGender($response->getResponse()["gender"]);
             //$user->setBirthday($response->getResponse()["birthday"]);
             //$user->setEmail($response->getEmail());
             $user->setAvatar($response->getProfilePicture());
             $user->setLocale($response->getResponse()["locale"]);
             break;
             $user->setUsername($username);
         default:
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /**
  * {@inheritDoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $userInfo = $this->getUserInfo($response);
     $service = $response->getResourceOwner()->getName();
     $user = $this->userManager->findUserBy(array("{$service}Id" => $userInfo['id']));
     if ($user instanceof PersonInterface) {
         $user = parent::loadUserByOAuthUserResponse($response);
         $serviceName = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
         $user->{$setter}($response->getAccessToken());
         return $user;
     }
     $userInfo = $this->checkEmail($service, $userInfo);
     $user = $this->userManager->createUser();
     $this->setUserInfo($user, $userInfo, $service);
     if ($userInfo['first_name']) {
         $user->setFirstName($userInfo['first_name']);
     }
     if ($userInfo['family_name']) {
         $user->setSurname($userInfo['family_name']);
     }
     if ($service === 'facebook') {
         $this->setFacebookData($user, $response->getResponse());
     }
     $username = Uuid::uuid4()->toString();
     if (!UsernameValidator::isUsernameValid($username)) {
         $username = UsernameValidator::getValidUsername();
     }
     $availableUsername = $this->userManager->getNextAvailableUsername($username, 10, Uuid::uuid4()->toString());
     $user->setUsername($availableUsername);
     $user->setEmail($userInfo['email']);
     $user->setPassword('');
     $user->setEnabled(true);
     $this->userManager->updateCanonicalFields($user);
     /** @var ValidatorInterface $validator */
     $validator = $this->container->get('validator');
     /** @var ConstraintViolationList $errors */
     $errors = $validator->validate($user, ['LoginCidadaoProfile']);
     if (count($errors) > 0) {
         foreach ($errors as $error) {
             if ($error->getPropertyPath() === 'email' && method_exists($error, 'getConstraint') && $error->getConstraint() instanceof UniqueEntity) {
                 throw new DuplicateEmailException($service);
             }
         }
     }
     $form = $this->formFactory->createForm();
     $form->setData($user);
     $request = $this->container->get('request');
     $eventResponse = new RedirectResponse('/');
     $event = new FormEvent($form, $request);
     $this->dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
     $this->userManager->updateUser($user);
     $this->dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $eventResponse));
     return $user;
 }
Esempio n. 24
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy([$this->getProperty($response) => $username]);
     // Email is mandatory, we fill it up with random thing first
     $email = uniqid('badger');
     if (null !== $response->getEmail()) {
         $email = $response->getEmail();
     }
     // When the user is registering
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // Create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $user->setUsername($response->getNickname());
         $user->setEmail($email);
         $user->setPassword($username);
         // TODO: change
         $user->setProfilePicture($response->getProfilePicture());
         $user->setEnabled(true);
         $user->setNuts(0);
         $tag = $this->tagRepository->findOneBy(['isDefault' => true]);
         if (null !== $tag) {
             $user->addTag($tag);
         }
         $this->userManager->updateUser($user);
         return $user;
     }
     // If user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     // Update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $serviceUserId = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $serviceUserId));
     //registration
     if (null === $user) {
         //check if user with service email exist in database
         if ($this->userManager->findUserByEmail($response->getEmail())) {
             $message = 'Vartotojas su nurodytu el.pašto adresu jau egzistuoja.';
             throw new \Symfony\Component\Security\Core\Exception\AuthenticationException($message);
         }
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         //create new user
         $user = $this->userManager->createUser();
         $user->{$setter_id}($serviceUserId);
         $user->{$setter_token}($response->getAccessToken());
         //get response as array
         $responseCustomFields = $response->getResponse();
         //fill user info
         $user->setEmail($response->getEmail());
         $user->setProfilePicture($response->getProfilePicture());
         //custom fields witch can be empty if user doesn't allow to provide them
         if ($response->getRealName() !== null) {
             $user->setUsername($serviceUserId);
         } else {
             $user->setUsername($response->getEmail());
         }
         switch ($service) {
             case "facebook":
                 if (array_key_exists('first_name', $responseCustomFields)) {
                     $user->setFirstName($responseCustomFields['first_name']);
                 }
                 if (array_key_exists('last_name', $responseCustomFields)) {
                     $user->setLastName($responseCustomFields['last_name']);
                 }
                 break;
             case "google":
                 if (array_key_exists('given_name', $responseCustomFields)) {
                     $user->setFirstName($responseCustomFields['given_name']);
                 }
                 if (array_key_exists('family_name', $responseCustomFields)) {
                     $user->setLastName($responseCustomFields['family_name']);
                 }
                 break;
         }
         if (array_key_exists('gender', $responseCustomFields)) {
             $user->setGender($responseCustomFields['gender']);
         }
         //generate random password
         $passwordGenerator = new SecureRandom();
         $random = $passwordGenerator->nextBytes(16);
         $passwordString = bin2hex($random);
         $password = substr($passwordString, 0, 10);
         $user->setPlainPassword($password);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         //welcoming email message
         $message = 'Sveiki ' . $user->getFullName() . ', sveikiname sėkmingai prisijugus prie BasketPlanner bendruomenės.';
         $subject = 'Sveikiname užsiregistravus BasketPlanner svetainėje.';
         $this->notificationService->sendNotification($user->getEmail(), $subject, $message);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 26
0
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $service = ucfirst($response->getResourceOwner()->getName());
     if ($service == "Facebook") {
         $user = $this->userManager->findUserBy(array("facebook_id" => $username));
     } else {
         if ($service == "Twitter") {
             $user = $this->userManager->findUserBy(array("twitter_id" => $username));
         } else {
             $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
         }
     }
     //when the user is registrating
     if (null === $user) {
         $data = $response->getResponse();
         $paths = $response->getPaths();
         // read the response data
         $accessToken = $response->getAccessToken();
         $realName = $response->getRealname();
         // get the service name
         $service = $response->getResourceOwner()->getName();
         // set methods name (e.g. setFacebook)
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // create a new user
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($accessToken);
         $user->setPassword('');
         $user->setEnabled(true);
         $user->setDisplayName($realName);
         $user->setRequestExtraInfo(true);
         $user->setUsername("user{$username}");
         if (isset($paths["email"])) {
             $email = $paths["email"];
             if (isset($data[$email])) {
                 $user->setEmail($data[$email]);
             }
         }
         if (isset($paths["thumbnailUrl"])) {
             $thumbnailUrl = $paths["thumbnailUrl"];
             if (isset($data[$thumbnailUrl])) {
                 $thumbnail = str_replace("_normal", "", $data[$thumbnailUrl]);
                 $user->setThumbUrl($thumbnail);
             }
         }
         if (isset($paths["bio"])) {
             $bio = $paths["bio"];
             if (isset($data[$bio])) {
                 $user->setBio($data[$bio]);
             }
         }
         if (ucfirst($service) == "Facebook") {
             $user->setThumbUrl("http://graph.facebook.com/{$username}/picture?width=200&height=200");
         }
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
     //when the user is registrating
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $mail = $response->getEmail();
         $firstname = $response->getFirstname();
         $lastname = $response->getLastname();
         $nickname = $firstname . rand(0, 1000);
         $data = $response->getResponse();
         $gender = $data['gender'];
         $birthday = $data['birthday'];
         //we check for the email existence - if so, throw error.
         if ($this->userManager->findUserByEmail($response->getEmail())) {
             $message = 'There is already an account with this email address';
             throw new \Symfony\Component\Security\Core\Exception\AuthenticationException($message);
         }
         while ($this->userManager->findUserByUsername($nickname)) {
             //their is already a user with this username
             $nickname = $firstname . rand(0, 1000000);
         }
         $birthday = new \DateTime($birthday);
         $setter = 'set' . ucfirst($service);
         $setter_id = $setter . 'Id';
         $setter_token = $setter . 'AccessToken';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         //I have set all requested data with the user's username
         //creating the picture entity for the new user
         $picture = new Media();
         $picture->setPath('/uploads/img/profile.png')->setName('default profile picture on bestfootball')->setImage('/uploads/img/profile.png')->setOriginalImage('/uploads/img/profile.png');
         /* $message = 'Welcome to bestfootball. Please complete your personal informations by clicking on this notification or by going to the informations section. Once that is all set up, you can go out there and show your skills!';
            $link = $this->generateUrl('bf_site_settings');
            $service = $this->container->get('bf_site.notification');
            $notification = $service->create($user, $message, null, $link); */
         //profile picture for facebook
         if ($service == 'facebook') {
             $profilepicture = $response->getProfilePicture();
             $picture->setPath($profilepicture)->setName('Profile picture of ' . $username . ' on Bestfootball.fr')->setImage($profilepicture)->setOriginalImage($profilepicture);
         }
         //modify here with relevant data
         $user->setUsername($nickname);
         if ($mail === null) {
             $user->setEmail("*****@*****.**");
         } else {
             $user->setEmail($mail);
         }
         $user->setPlainPassword($username);
         $user->setEnabled(true);
         $user->setPoints(0);
         $user->setDuelPoints(0);
         $user->setDuelWins(0);
         $user->setMedia($picture);
         $user->setName($lastname);
         $user->setFirstname($firstname);
         $user->setGender($gender);
         $user->setBirthday($birthday);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
Esempio n. 28
0
 /**
  * {@inheritDoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $rawResponse = $response->getResponse();
     $username = $response->getUsername();
     $screenName = $response->getNickname();
     $service = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($service);
     $setter_id = $setter . 'Id';
     $setter_token = $setter . 'AccessToken';
     $setter_username = $setter . 'Username';
     $newUser = false;
     $user = $this->userManager->findUserBy(array("{$service}Id" => $username));
     if (null === $user) {
         switch ($service) {
             case 'twitter':
                 $email = $this->session->get('twitter.email');
                 if (!$email) {
                     throw new MissingEmailException();
                 } else {
                     $this->session->remove('twitter.email');
                 }
                 $defaultUsername = "******";
                 break;
             case 'google':
                 $email = $rawResponse['email'];
                 $defaultUsername = $email;
                 break;
             default:
                 $email = $rawResponse['email'];
                 $defaultUsername = $email;
                 break;
         }
         $newUser = true;
         $user = $this->userManager->createUser();
         $user->{$setter_id}($username);
         $user->{$setter_token}($response->getAccessToken());
         $user->{$setter_username}($screenName);
         $fullName = explode(' ', $response->getRealName(), 2);
         if (isset($fullName[0][1]) && $fullName[0][1] != '') {
             $user->setFirstName($fullName[0]);
         }
         if (isset($fullName[1][1]) && $fullName[1][1] != '') {
             $user->setSurname($fullName[1]);
         }
         if (!UsernameValidator::isUsernameValid($screenName)) {
             $screenName = UsernameValidator::getValidUsername();
         }
         $availableUsername = $this->userManager->getNextAvailableUsername($screenName, 10, $defaultUsername);
         $user->setUsername($availableUsername);
         $user->setEmail($email);
         $user->setPassword('');
         $user->setEnabled(true);
         $this->userManager->updateCanonicalFields($user);
         $form = $this->formFactory->createForm();
         $form->setData($user);
         $request = $this->container->get('request');
         $eventResponse = new \Symfony\Component\HttpFoundation\RedirectResponse('/');
         $event = new FormEvent($form, $request);
         if ($newUser) {
             $this->dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
         }
         $this->userManager->updateUser($user);
         if ($newUser) {
             $this->dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $eventResponse));
         }
         return $user;
     }
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /**
  * @param UserResponseInterface $response
  *
  * @return \FOS\UserBundle\Model\UserInterface|UserInterface
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $data = $response->getResponse();
     $username = $data['login'];
     $user = $this->userManager->findUserByUsername($username);
     //when the user is registrating
     if (null === $user) {
         $service = $response->getResourceOwner()->getName();
         $setter = 'set' . ucfirst($service);
         $setterId = $setter . 'Id';
         $setterToken = $setter . 'AccessToken';
         // create new user here
         $user = $this->userManager->createUser();
         $user->{$setterId}($data['id']);
         $user->{$setterToken}($response->getAccessToken());
         unset($data['name']);
         unset($data['email']);
         if (isset($data['name'])) {
             $user->setRealName($data['name']);
         }
         if (isset($data['email'])) {
             $user->setEmail($data['email']);
         }
         //I have set all requested data with the user's username modify here with relevant data
         $user->setUsername($username);
         $user->setPlainPassword($data['id']);
         $user->setAvatarUrl($data['avatar_url']);
         $user->setGravatarId($data['gravatar_id']);
         $user->setEnabled(true);
         $this->userManager->updateUser($user);
         return $user;
     }
     //if user exists - go with the HWIOAuth way
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
     //update access token
     $user->{$setter}($response->getAccessToken());
     return $user;
 }
 /**
  * {@inheritdoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     $user = $this->userManager->findUserBy([$this->getProperty($response) => $username]);
     $userEmail = $this->userManager->findUserBy(['email' => $response->getEmail()]);
     if (null === $user && null === $userEmail) {
         /** @var User $user */
         $user = (new User())->setUsername($username)->setEmail($response->getEmail())->setPassword($username)->setFullName($response->getLastName() . ' ' . $response->getFirstName())->setEnabled(true);
         $service = $response->getResourceOwner()->getName();
         switch ($service) {
             case 'vkontakte':
                 $user->setVkId($username)->setVkAccessToken($response->getAccessToken());
                 break;
             case 'facebook':
                 $user->setFacebookId($username)->setFacebookAccessToken($response->getAccessToken());
                 break;
             case 'google':
                 $user->setGoogleId($username)->setGoogleAccessToken($response->getAccessToken());
                 break;
         }
         $this->userManager->updateUser($user);
         return $user;
     }
     /** @var User $user */
     $user = parent::loadUserByOAuthUserResponse($response);
     $serviceName = $response->getResourceOwner()->getName();
     switch ($serviceName) {
         case 'vkontakte':
             $user->setVkAccessToken($response->getAccessToken());
             break;
         case 'facebook':
             $user->setFacebookAccessToken($response->getAccessToken());
             break;
         case 'google':
             $user->setGoogleAccessToken($response->getAccessToken());
             break;
     }
     return $user;
 }