M
M
Maxim2015-09-25 23:12:04
symfony
Maxim, 2015-09-25 23:12:04

Is there another way to make changes to a third-party bundle?

The essence of the change is setting the administrator role to each user,
this method seems to me not very correct, which can be done much more simply.

namespace ACVote\UsersBundle\Controller;

use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Symfony\Component\HttpFoundation\Request;
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\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;

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);
        $user->setSuperAdmin(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(),
        ));
    }
}

those. I just copied the source code from the vendor bundle and added the line I needed in it.
Or should it be something like this? but it definitely won't work that way.
$response = parent::registerAction($request);
        $user->setSuperAdmin(true);
        return $response;

And where is the password and login processing?
If you take
$user = $userManager->createUser();
$user->setEnabled(true);
$user->setSuperAdmin(true);

it's clear here. but how are other forms handled?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-09-25
@maxpointn2point

relatively correct would be to inherit from the model provided by FosUserBundle and just call this method in the constructor.

X
XeuRun, 2015-09-26
@XeuRun

You can fork the bundle on github, rewrite the code as you need and add the package to packagist.org , then add it to your project via composer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question