How to definitely disable registration in FOSUserBundle
Asked Answered
N

7

16

In my project, I allow only one user to manage the content of the website. This user will be added using the command line at first.

Now, I want to get the registration action inaccessible and I don't know how? Till now, I just put the ROLE_ADMIN in the access control for the route register to avoid that visitors can go throw it.

Any tips?

Natalia answered 29/6, 2015 at 0:37 Comment(2)
can't you just remove the route that is used for registration?Holler
Yea, but, I don't to touch to the fosuserbundle routes...Anyways the work is done and the issue is solved, thanks.Natalia
I
15

There are many ways to solve this issue. You can simply remove fos_user_registration_register route from routing.yml. Or use more complicated solution: set up event listener to FOS\UserBundle\FOSUserEvents::REGISTRATION_INITIALIZE event and redirect user to login page.

services.xml

<service id="app.registration.listener" class="AppBundle\EventListener\RegistrationListener">
    <tag name="kernel.event_subscriber" />
    <argument type="service" id="router" />
</service>

RegistrationListener.php

<?php

namespace AppBundle\EventListener;

use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class RegistrationListener implements EventSubscriberInterface
{
    /**
     * @var UrlGeneratorInterface
     */
    private $router;

    /**
     * @param UrlGeneratorInterface $router
     */
    public function __construct(UrlGeneratorInterface $router) {
        $this->router = $router;
    }

    public static function getSubscribedEvents()
    {
        return [
            FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInitialize',
        ];
    }

    public function onRegistrationInitialize(GetResponseUserEvent $event)
    {
        $url = $this->router->generate('fos_user_security_login');
        $response = new RedirectResponse($url);

        $event->setResponse($response);
    }
}
Itis answered 29/6, 2015 at 2:6 Comment(3)
Thanks for the tips...I chose the second solution because I don't want to change the fosuserbundle...Natalia
@Mikhail: How do I do the first one? I tried to place a all.xml without the registration route inside app/Resources/FOSUserBundle/Resources/config/routing but it didn't work?Weeds
You should import FOS routings to your app/config/routing.yml separately. Just add routes for required functionality (security.xml, resetting.xml, profile.xml, change_password.xml) except registration.xml.Itis
S
21

Take a look at the routing configuration imported from

vendor/friendsofsymfony/user-bundle/Resources/config/routing/all.xml

If you want just the basic security actions, just import

@FOSUserBundle/Resources/config/routing/security.xml

instead of

@FOSUserBundle/Resources/config/routing/all.xml

This way you can simply select which components (security, profile, resetting, change_password) you want to use or event import only specific routes from those components.

Syllogistic answered 13/10, 2016 at 17:8 Comment(0)
I
15

There are many ways to solve this issue. You can simply remove fos_user_registration_register route from routing.yml. Or use more complicated solution: set up event listener to FOS\UserBundle\FOSUserEvents::REGISTRATION_INITIALIZE event and redirect user to login page.

services.xml

<service id="app.registration.listener" class="AppBundle\EventListener\RegistrationListener">
    <tag name="kernel.event_subscriber" />
    <argument type="service" id="router" />
</service>

RegistrationListener.php

<?php

namespace AppBundle\EventListener;

use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class RegistrationListener implements EventSubscriberInterface
{
    /**
     * @var UrlGeneratorInterface
     */
    private $router;

    /**
     * @param UrlGeneratorInterface $router
     */
    public function __construct(UrlGeneratorInterface $router) {
        $this->router = $router;
    }

    public static function getSubscribedEvents()
    {
        return [
            FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInitialize',
        ];
    }

    public function onRegistrationInitialize(GetResponseUserEvent $event)
    {
        $url = $this->router->generate('fos_user_security_login');
        $response = new RedirectResponse($url);

        $event->setResponse($response);
    }
}
Itis answered 29/6, 2015 at 2:6 Comment(3)
Thanks for the tips...I chose the second solution because I don't want to change the fosuserbundle...Natalia
@Mikhail: How do I do the first one? I tried to place a all.xml without the registration route inside app/Resources/FOSUserBundle/Resources/config/routing but it didn't work?Weeds
You should import FOS routings to your app/config/routing.yml separately. Just add routes for required functionality (security.xml, resetting.xml, profile.xml, change_password.xml) except registration.xml.Itis
N
6

You can just change app/config/security.yml:

- { path: ^/register, role: ROLE_ADMIN }

Change from the default (IS_AUTHENTICATED_ANONYMOUSLY) to ROLE_ADMIN and it will stop allowing anonymous users from getting to the /register form.

Nellienellir answered 1/3, 2016 at 14:35 Comment(0)
O
3

Another simple solution (the one I used) is to overwrite the registerAction() default FOSUserBundle controller method:

namespace Acme\UserBundle\Controller;

use FOS\UserBundle\Controller\RegistrationController as FOSRegistrationController;
use Symfony\Component\HttpFoundation\Request;

class RegistrationController extends FOSRegistrationController
{
    public function registerAction(Request $request)
    {
        return $this->redirectToRoute('getStarted', array(), 301);
    }
}

Doing this will leave active other routes, as the confirmation page.

I simply overwrote the register action and redirect the user to my first registration page (getStarted).

Orten answered 1/9, 2015 at 13:1 Comment(0)
O
2

If you use the JMSSecurityExtraBundle you can use the denyAll directive like so:

- { path: ^/register, access: denyAll }

Ogbomosho answered 27/2, 2017 at 17:23 Comment(0)
N
1

This is how I solve this issue...

First you have to define your listener in the services.yml file:

services:
    registrationListner:
      class: App\YourBundle\Listener\RegistrationListener
      arguments: [@service_container]
      tags:
    - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest}

Then create your class RegistrationListener:

<?php
namespace App\YourBundle\Listener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class RegistrationListener
{
private $router;

public function __construct(ContainerInterface $container){
    $this->router = $container->get('router');
}

public function onKernelRequest(GetResponseEvent $event)
{

    $route = $event->getRequest()->attributes->get('_route');
    if ($route == 'fos_user_registration_register') {
        //here we're gonna to redirect to you_route for example, I guess in the most cases it will be the index...
        $event->setResponse(new RedirectResponse($this->router->generate('your_route'))); 
    }
}
}

Hope it helps.

Natalia answered 29/6, 2015 at 14:9 Comment(1)
Wow, that is a so much more complex way of going about things than just not importing the registration route.Ore
W
0

You can try to change your routing.yml

fos_user_registration_register:
    path:  /register{trailingSlash}
    defaults: { _controller: AcmeBundle:Default:register, trailingSlash : "/" }
    requirements: { trailingSlash : "[/]{0,1}" }

And in your DefaultController

    public function registerAction(Request $request)
    {
       
        return $this->redirectToRoute('404OrWhatYouWant');
    }
Wanderjahr answered 14/9, 2018 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.