コード例 #1
0
 public function registerAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
     $formFactory = $this->get('fos_user.registration.form.factory');
     /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
     $userManager = $this->get('fos_user.user_manager');
     /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
     $dispatcher = $this->get('event_dispatcher');
     //we set the picture of the user to the default picture.
     $picture = new Media();
     $picture->setPath('/uploads/img/profile.png')->setName('default profile picture on bestfootball');
     //setting the country to France and region to ile de france.
     $country = $em->getRepository('BFSiteBundle:Country')->find(2);
     $state = $em->getRepository('BFSiteBundle:State')->find(111);
     $user = $userManager->createUser();
     $user->setEnabled(true)->setMedia($picture)->setDuelWins(0)->setPoints(0)->setDuelPoints(0)->setCountry($country)->setState($state);
     //section du parrainage
     $parainId = $request->query->get('parain');
     if ($parainId === null) {
         //il n'y a pas de parain
         $user->setParain(null);
     } else {
         $parain = $em->getRepository('BFUserBundle:User')->find($parainId);
         $user->setParain($parain);
     }
     $message = $this->get('translator')->trans('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);
     $event = new GetResponseUserEvent($user, $request);
     $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
     if (null !== $event->getResponse()) {
         return $event->getResponse();
     }
     $form = $formFactory->createForm();
     $form->setData($user);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $event = new FormEvent($form, $request);
         $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
         $userManager->updateUser($user);
         //we save the picture entity
         $em->persist($picture);
         $em->persist($notification);
         $em->flush();
         if (null === ($response = $event->getResponse())) {
             $url = $this->generateUrl('fos_user_registration_confirmed');
             $response = new RedirectResponse($url);
         }
         $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
         return $response;
     }
     return $this->render('BFUserBundle:Registration:register.html.twig', array('form' => $form->createView(), 'parain' => $parainId));
 }
コード例 #2
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) {
         $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;
 }