Overriding FOSUserBUndle Controllers Symfony 2 - php

I want to override FOS\UserBundle\Controller\RegistrationController to add some functionalities (to manage the fields I add in my registration form etc).
I do not know why, after overriding it, symphony ignores my controller. This is not the first time, I also tried to override others ... never found solutions ...
<?php
namespace FP\UserBundle\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController
{
public function registerAction()
{
$response = parent::registerAction();
echo "FPUserBundle";
// do custom stuff
return $response;
}
}
.
<?php
namespace FP\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class FPUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
.
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Controller;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;
/**
* Controller managing the registration
*
* #author Thibault Duplessis <thibault.duplessis#gmail.com>
* #author Christophe Coevoet <stof#notk.org>
*/
class RegistrationController extends Controller
{
public function registerAction(Request $request)
{
echo "FOSUserBundle";
/** #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');
$user = $userManager->createUser();
$user->setEnabled(true);//active l'user
$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);
//--- ajout des données pour les champs ajoutés ---
$user->setDateInscrip(new \DateTime());
$user->setRoles(array('ROLE_USER'));
//--------- Fin de l'ajout ---------
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
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('FOSUserBundle:Registration:register.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Tell the user to check his email provider
*/
public function checkEmailAction()
{
$email = $this->get('session')->get('fos_user_send_confirmation_email/email');
$this->get('session')->remove('fos_user_send_confirmation_email/email');
$user = $this->get('fos_user.user_manager')->findUserByEmail($email);
if (null === $user) {
throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
}
return $this->render('FOSUserBundle:Registration:checkEmail.html.twig', array(
'user' => $user,
));
}
/**
* Receive the confirmation token from user email provider, login the user
*/
public function confirmAction(Request $request, $token)
{
/** #var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserByConfirmationToken($token);
if (null === $user) {
throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
}
/** #var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$user->setConfirmationToken(null);
$user->setEnabled(true);
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRM, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_confirmed');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRMED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
/**
* Tell the user his account is now confirmed
*/
public function confirmedAction()
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->redirect($this->generateUrl('fp_user_inscrip', array('user' => $user)));
/*
return $this->render('FPPlatformBundle:Index:index.html.twig', array(
'user' => $user,
));*/
}
}
Despite these codes, when I run the check, "FPUserBundle" is not displayed, while "FOSUserBundle" appears well ...

Check that you've added the new Bundle into app/AppKernel.php::registerBundles function.

Overriding Classes (Controllers are classes too!) will give the new class all the futures that the base-class has (except direct access to private methods or properties). But do not forget that the base-class still can be used beside the new class. It dependence's which class you instantiate.
$a = new BaseClass();
vs
$a = new NewClass();
So the question is which one will Symfony use? And THAT is what you can manage with the routing. as long this is in your app/config/routing.yml:
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
Symfony will use the original FOSUserBundle controllers. Just find those xml files in the vendor/FOS.. directory and copy them to your own project. Change above showed rule to your own bundle and change the controllernames in the xml files. Of course you could write your own .yml files too.
read more

Related

Symfony redirect if already loggedin from login page

I'm using FOSUser Bundle to login. Now if user is already loggedin, how can I redirect user to homepage ('/'), if user visit to /login url.
I have copied SecurityController to src\AppBundle\Controller location and changed renderlogin method but it doesn't work.
renderLogin() method
protected function renderLogin(array $data)
{
if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
return new RedirectResponse('/', 403);
}
return $this->render('#FOSUser/Security/login.html.twig', $data);
}
I have added this line as well in the security controller,
use Symfony\Component\HttpFoundation\RedirectResponse;
Any help is much appreciated.
Well you need to make some changes in the SecurityController
/**
* Renders the login template with the given parameters. Overwrite this function in
* an extended controller to provide additional data for the login template.
*
* #param array $data
*
* #return Response
*/
protected function renderLogin(array $data)
{
/**
* If the user has already logged in (marked as is authenticated fully by symfony's security)
* then redirect this user back (in my case, to the dashboard, which is the main entry for
* my logged in users)
*/
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirectToRoute('homepage');
}
return $this->render('#FOSUser/Security/login.html.twig', $data);
}
}
And to redirect authenticated users who try to visit the registration page, you need to change the ``
class RegistrationController extends BaseController
{
/**
* #param Request $request
*
* #return Response
*/
public function registerAction(Request $request)
{
/**
* If the user has already logged in (marked as is authenticated fully by symfony's security)
* then redirect this user back (in my case, to the dashboard, which is the main entry for
* my logged in users)
*/
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirectToRoute('homepage');
}
/** #var $formFactory FactoryInterface */
$formFactory = $this->get('fos_user.registration.form.factory');
/** #var $userManager UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
/** #var $dispatcher EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$user = $userManager->createUser();
$user->setEnabled(true);
$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->isSubmitted()) {
if ($form->isValid()) {
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
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;
}
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);
if (null !== $response = $event->getResponse()) {
return $response;
}
}
return $this->render('#FOSUser/Registration/register.html.twig', array(
'form' => $form->createView(),
));
}
}
It's weird that this has not been fixed in the FOSUserBundle, anyway I had two Github Gists that handles this issue:
Registration : https://gist.github.com/teeyo/147f2d5d21d1beadce133a51b02d9570
Login : https://gist.github.com/teeyo/121e21b35d71a9ab4a8f321043b6f6cd
In the FOS securityController (for login) : add this to login action
if($session->get("_security_main"))
{
$route = $this->container->get('router')->generate('site_faim_homepage');
return new RedirectResponse($route);
}
I just ask if user instance already exists and if yes, then I redirect to desired route (sorry if I somehow duplicated already added post, this is my fresh experience):
if($this->getUser()){
return $this->redirectToRoute('homepage');
}

how to add profile picture in UserEntity with FosUserBundle

I try to add a profile picture in my user Entity but i fail , i work with symfony 3 and fosuserbundle , for doing this work , I use a listner , here my entire code :
the code of my UserEntity:
namespace Forum\ForumBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Util\SecureRandom;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\File\File;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #ORM\HasLifecycleCallbacks()
*/
class User extends BaseUser
{
/**
* #ORM\Column(type="string")
*
* #Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
* #Assert\Image(
*
* )
*/
private $brochure;
public function getBrochure()
{
return $this->brochure;
}
public function setBrochure(File $file = null)
{
$this->brochure = $file;
return $this;
}
public function __construct()
{
parent::__construct();
$this->test = false;
// $this->uploadProfilePicture();
// your own logic
}
}
My listener :
namespace Forum\ForumBundle\EventListener;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Forum\ForumBundle\Entity\User;
use Forum\ForumBundle\FileUploader;
class BrochureUploadListener
{
private $uploader;
public function __construct(FileUploader $uploader)
{
$this->uploader = $uploader;
}
public function prePersist(LifecycleEventArgs $args)
{die("good");
$entity = $args->getEntity();
$this->uploadFile($entity);
}
public function preUpdate(PreUpdateEventArgs $args)
{
$entity = $args->getEntity();
$this->uploadFile($entity);
}
private function uploadFile($entity)
{
// upload only works for Product entities
if (!$entity instanceof Product) {
return;
}
$file = $entity->getBrochure();
// only upload new files
if (!$file instanceof UploadedFile) {
return;
}
$fileName = $this->uploader->upload($file);
$entity->setBrochure($fileName);
}
}
the listener use Uploader file :
namespace Forum\ForumBundle;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->targetDir, $fileName);
return $fileName;
}
}
I configure my services as well
app.brochure_uploader:
class: Forum\ForumBundle\FileUploader
arguments: ['%brochures_directory%']
app.doctrine_brochure_listener:
class: Forum\ForumBundle\EventListener\BrochureUploadListener
arguments: ['#app.brochure_uploader']
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
an I declare the parameter :brochure-directory in config.yml
parameters:
locale: fr
brochures_directory: 'web/uploads/brochures'
the probleme is when I update my entity , the bro
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Controller;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* Controller managing the user profile
*
* #author Christophe Coevoet <stof#notk.org>
*/
class ProfileController extends Controller
{
/**
* Show the user
*/
public function showAction()
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->render('FOSUserBundle:Profile:show.html.twig', array(
'user' => $user
));
}
/**
* Edit the user
*/
public function editAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** #var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** #var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
/** #var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('FOSUserBundle:Profile:edit.html.twig', array(
'form' => $form->createView()
));
}
}
Have you tried changing your config.yml to:
parameters:
locale: fr
brochures_directory: '%kernel.root_dir%/../web/uploads/brochures'
Maybe that's the problem? Try it first.
Edit #2.
Secondly can you edit your app/config/parameters.yml and add the following parameter:
brochures_directory: uploads/brochures
Also make sure you are clearing your cache everytime:
php bin/console cache:clear --env=prod
See if that works.
Edit #3.
I notice your configuration is quite different than the default, and I was trying to duplicate the setup, but there's a lot of work involved in doing that.
I see a difference in what I use and you use which might also affect the problem you are seeing:
parameters:
locale: fr
brochures_directory: uploads/brochures
So in other words, remove the quotes and get rid of 'web' prefix. Also make sure you have created those folders, both "uploads" and "brochures" under it.
See if this works.
Like I said I tried to duplicate, bu then I get the "Please, upload the product brochure as a PDF file." on the "Register" page, but there would still be quite a bit of work for me to try to duplicate your setup.
I think it's probably something simple, based on the fact I'm getting that message.

Error in redirection of user after REGISTRATION_SUCCESS event in FOSUserBundle

I am trying to redirect the user to another form before user get confirmation email. So I after submit of register form I want to redirect user to another form to fill so that I can assign roles to the user. After submission of that form user should get a confirmation email. After confirmation user will login automatically.
Full Code
User management with FUB
RegistrationSuccessListener.php
namespace Usr\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RegistrationSuccessListener implements EventSubscriberInterface
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
/**
* {#inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
);
}
public function onRegistrationSuccess(GetResponseUserEvent $event)
{
$url = $this->router->generate('fos_user_registration_success');
$event->setResponse(new RedirectResponse($url));
}
}
And my services.yml
services:
usr_user.registration.success:
class: Usr\UserBundle\EventListener\RegistrationSuccessListener
arguments: ["#router"]
tags:
- { name: kernel.event_subscriber }
I am getting error:
Type error: Argument 1 passed to Usr\UserBundle\EventListener\RegistrationSuccessListener::onRegistrationSuccess() must be an instance of FOS\UserBundle\Event\GetResponseUserEvent, instance of FOS\UserBundle\Event\FormEvent given
RegistrationController.php
namespace Usr\UserBundle\Controller;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
/**
* Controller managing the registration
*/
class RegistrationController extends BaseController
{
public function registerAction(Request $request)
{
/** #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');
$user = $userManager->createUser();
$user->setEnabled(true);
$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);
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('FOSUserBundle:Registration:register.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Tell the user to check his email provider
*/
public function checkEmailAction()
{
$email = $this->get('session')->get('fos_user_send_confirmation_email/email');
$this->get('session')->remove('fos_user_send_confirmation_email/email');
$user = $this->get('fos_user.user_manager')->findUserByEmail($email);
if (null === $user) {
throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
}
return $this->render('FOSUserBundle:Registration:checkEmail.html.twig', array(
'user' => $user,
));
}
/**
* Receive the confirmation token from user email provider, login the user
*/
public function confirmAction(Request $request, $token)
{
/** #var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserByConfirmationToken($token);
if (null === $user) {
throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
}
/** #var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$user->setConfirmationToken(null);
$user->setEnabled(true);
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRM, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_confirmed');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRMED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
/**
* Tell the user his account is now confirmed
*/
public function confirmedAction()
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->render('FOSUserBundle:Registration:confirmed.html.twig', array(
'user' => $user,
'targetUrl' => $this->getTargetUrlFromSession(),
));
}
private function getTargetUrlFromSession()
{
// Set the SecurityContext for Symfony <2.6
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
$tokenStorage = $this->get('security.token_storage');
} else {
$tokenStorage = $this->get('security.context');
}
$key = sprintf('_security.%s.target_path', $tokenStorage->getToken()->getProviderKey());
if ($this->get('session')->has($key)) {
return $this->get('session')->get($key);
}
}
public function registercuccessAction()
{
return $this->render('::base.html.twig', array());
}
}
I know that is not the best practice solution but it can help you, just
change the selected route name.

Set up registration FOSUserBundle with FOSRestBundle REST API

Problem fixed, check my answer.
I'm building a registration endpoint on my Symfony2.7 rest api.
I am using FosRestBundle and FosUserBundle
Here is the user model :
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct() {
parent::__construct();
// your own logic
}
}
\
Here is the UserType form :
\
class UserType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email')
->add('username', null)
->add('plainPassword', 'repeated', array(
'type' => 'password',
'first_options' => array('label' => 'password'),
'second_options' => array('label' => 'password_confirmation'),
))
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User',
'csrf_protection' => false,
));
}
/**
* #return string
*/
public function getName()
{
return 'user';
}
}
And this the post user controller :
public function postUserAction(\Symfony\Component\HttpFoundation\Request $request) {
$user = new \AppBundle\Entity\User();
$form = $this->createForm(new \AppBundle\Form\UserType(), $user);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$view = $this->view(array('token'=>$this->get("lexik_jwt_authentication.jwt_manager")->create($user)), Codes::HTTP_CREATED);
return $this->handleView($view);
}
return array(
'form' => $form,
);
}
The problem is that when i submit wrong information, or empty information, the server return a bad formated 500 error with doctrine / mysql details of null value for not null row in state of a json response with the list of bad formated entries.
Any idea on how to fix this behaviour ?
How come the validation get by passed and
Ok after spending a lot of time reading the FOSUserBundle code, and particularly the registration controller and the form factory, i came up with a fully working solution.
Before doing anything don't forget to disable CSRF in your symfony2 configuration.
Here is the controller I use to register :
public function postUserAction(\Symfony\Component\HttpFoundation\Request $request) {
/** #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');
$user = $userManager->createUser();
$user->setEnabled(true);
$event = new \FOS\UserBundle\Event\GetResponseUserEvent($user, $request);
$dispatcher->dispatch(\FOS\UserBundle\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 \FOS\UserBundle\Event\FormEvent($form, $request);
$dispatcher->dispatch(\FOS\UserBundle\FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_confirmed');
$response = new \Symfony\Component\HttpFoundation\RedirectResponse($url);
}
$dispatcher->dispatch(\FOS\UserBundle\FOSUserEvents::REGISTRATION_COMPLETED, new \FOS\UserBundle\Event\FilterUserResponseEvent($user, $request, $response));
$view = $this->view(array('token' => $this->get("lexik_jwt_authentication.jwt_manager")->create($user)), Codes::HTTP_CREATED);
return $this->handleView($view);
}
$view = $this->view($form, Codes::HTTP_BAD_REQUEST);
return $this->handleView($view);
}
Now the tricky part was submiting the form using REST. The problem was that when I sent i JSON like this one :
{
"email":"xxxxx#xxxx.com",
"username":"xxx",
"plainPassword":{
"first":"xxx",
"second":"xxx"
}
}
The API was responding like nothing was submited.
The solution is that Symfony2 is waiting for you to encapsulate your form data in the form name !
The question was "I didnt create this form so i dont know what is its name..".
So i went again in the bundle code and found out that the form type was fos_user_registration and the getName function was returning fos_user_registration_form.
As a result i tried to submit my JSON this way :
{"fos_user_registration_form":{
"email":"xxxxxx#xxxxxxx.com",
"username":"xxxxxx",
"plainPassword":{
"first":"xxxxx",
"second":"xxxxx"
}
}}
And voila! it worked.
If you are struggling setting up your fosuserbundle with fosrestbundle and LexikJWTAuthenticationBundle just ask me i'll be glad to help.
Another way is this registration without the forms from FOSUserBundle.
Make a POST Request with params: email, user, password.
public function postUserAction(Request $request)
{
$userManager = $this->get('fos_user.user_manager');
$email = $request->request->get('email');
$username = $request->request->get('user');
$password = $request->request->get('password');
$email_exist = $userManager->findUserByEmail($email);
$username_exist = $userManager->findUserByUsername($username);
if($email_exist || $username_exist){
$response = new JsonResponse();
$response->setData("Username/Email ".$username."/".$email." existiert bereits");
return $response;
}
$user = $userManager->createUser();
$user->setUsername($username);
$user->setEmail($email);
$user->setLocked(false);
$user->setEnabled(true);
$user->setPlainPassword($password);
$userManager->updateUser($user, true);
$response = new JsonResponse();
$response->setData("User: ".$user->getUsername()." wurde erstellt");
return $response;
}
#Adel 'Sean' Helal your way doesn't work, at least with last versions of FOSRestBundle, FOSUserBundle and Symfony with Flex. I almost shoot myself in the head trying to make it work. At the end I found the solution and it's pretty simple. Only parse the request is required.
Fragment of my controller code
...
$form->setData($user);
// THIS LINE DO THE MAGIC
$data = json_decode($request->getContent(), true);
if ($data === null) {
throw new BadRequestHttpException();
}
$form->submit($data);
if ( ! $form->isValid()) {
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);
if (null !== $response = $event->getResponse()) {
return $response;
}
return new JsonResponse($this->getFormErrors($form), Response::HTTP_BAD_REQUEST);
}
...
The composer.json dependencies:
...
"symfony/lts": "^3",
"symfony/flex": "^1.0",
"friendsofsymfony/rest-bundle": "^2.3",
"friendsofsymfony/user-bundle": "^2.0",
"lexik/jwt-authentication-bundle": "^2.4",
...
My functional test code:
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\Container;
class ApiUserControllerTest extends WebTestCase
{
/**
* #var Container
*/
private $container;
public function setUp()
{
self::bootKernel();
$this->container = self::$kernel->getContainer();
}
public function testRegistration()
{
$userData = [
'username' => 'test',
'email' => 'test#email.com',
'plainPassword' => [
'first' => 'test123', 'second' => 'test123'
]
];
$client = $this->container->get('eight_points_guzzle.client.rest');
$response = $client->post(
'api/registration',
['json' => $userData]
);
$bodyResponse = \GuzzleHttp\json_decode($response->getBody(), true);
$this->assertEquals(201, $response->getStatusCode());
$this->assertArrayHasKey('token', $bodyResponse);
$this->assertNotEmpty($bodyResponse['token']);
}
}

Access to service overriding FOS User Bundle Controller

I'm new in Symfony2,
I'm trying to override a controller using a service inside
This is the register controller
class RegistrationController extends BaseController
{
public function registerAction(Request $request)
{
/** #var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.registration.form.factory');
/** #var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
/** #var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');
$user = $userManager->createUser();
$user->setEnabled(true);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, new UserEvent($user, $request));
$form = $formFactory->createForm();
$form->setData($user);
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('easy_app_user_profile');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
//create UserInfo
$doctrine = $this->container->get('doctrine');
$userInfo = new UserInformation();
$userInfo->setUser($user);
//save the userInfo
$em = $doctrine->getManager();
$em->persist($userInfo);
$em->flush();
//add user first login
$loginManager = $this->get('user_login_manager');
$loginManager->saveUser($request, $user);
return $response;
}
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
}
near the end I'm using
$loginManager = $this->get('user_login_manager');
$loginManager->saveUser($request, $user);
But I can't use get because this is not extending Controller.
So I don't know how to access to my service in this controller
Thanks
$this->get('some_service') is only a helper shortcut defined in the symfony base controller. Look at you code above and see how all the services are called:
$loginManager = $this->container->get('user_login_manager');
Btw. if you are using the latest version of FOSUserBundle (dev-master), then the new event system might fit better than overriding the controller. REGISTER_COMPLETED may fit for you use case. If you take a look in the controller code above, you can see, when the event is dispatched. You should fairly use events than controller overriding.

Categories