/** * Enforces that the given user will have an username * @param \FOS\UserBundle\Model\UserInterface $user */ public function enforceUsername(UserInterface $user) { $current = $user->getUsernameCanonical(); if (is_null($current) || strlen($current) == 0) { $email = explode('@', $user->getEmailCanonical(), 2); $username = $email[0]; if (!UsernameValidator::isUsernameValid($username)) { $username = UsernameValidator::getValidUsername(); } $newUsername = $this->getNextAvailableUsername($username); $user->setUsername($newUsername); $this->updateCanonicalFields($user); } }
public function onRegistrationSuccess(FormEvent $event) { $user = $event->getForm()->getData(); if (null === $user->getConfirmationToken()) { $user->setConfirmationToken($this->tokenGenerator->generateToken()); $user->setEmailExpiration(new \DateTime("+{$this->emailUnconfirmedTime}")); } $key = '_security.main.target_path'; if ($this->session->has($key)) { //this is to be catch by loggedinUserListener.php return $event->setResponse(new RedirectResponse($this->router->generate('lc_home'))); } $email = explode('@', $user->getEmailCanonical(), 2); $username = $email[0]; if (!UsernameValidator::isUsernameValid($username)) { $url = $this->router->generate('lc_update_username'); } else { $url = $this->router->generate('fos_user_profile_edit'); } $event->setResponse(new RedirectResponse($url)); }
/** * {@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; }