如何在FOSUserBundle中明确禁用注册

问题描述 投票:16回答:7

在我的项目中,我只允许一个用户管理网站的内容。首先使用命令行添加此用户。

现在,我想让登记行动无法进入,我不知道怎么办?直到现在,我只是将ROLE_ADMIN放在路径寄存器的访问控制中,以避免访问者抛出它。

有小费吗?

symfony fosuserbundle
7个回答
15
投票

有很多方法可以解决这个问题。您只需从routing.yml中删除fos_user_registration_register路由即可。或者使用更复杂的解决方案:将事件监听器设置为FOS \ UserBundle \ FOSUserEvents :: REGISTRATION_INITIALIZE事件并将用户重定向到登录页面。

的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);
    }
}

21
投票

看一下从中导入的路由配置

供应商/ friendsofsymfony /用户束/资源/配置/路由/ all.xml

如果您只想要基本的安全操作,只需导入即可

@ FOSUserBundle /资源/配置/路由/ security.xml文件

代替

@ FOSUserBundle /资源/配置/路由/ all.xml

这样,您可以简单地选择要使用的组件(安全性,配置文件,重置,change_password)或仅从这些组件导入事件。


6
投票

你可以改变app / config / security.yml:

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

从默认值(IS_AUTHENTICATED_ANONYMOUSLY)更改为ROLE_ADMIN,它将停止允许匿名用户访问/ register表单。


3
投票

另一个简单的解决方案(我使用的解决方案)是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);
    }
}

执行此操作将保留活动的其他路径,作为确认页面。

我只是覆盖了注册操作并将用户重定向到我的第一个注册页面(getStarted)。


2
投票

如果您使用JMSSecurityExtraBundle,您可以使用denyAll指令,如下所示:

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


1
投票

这就是我解决这个问题的方法......

首先,您必须在services.yml文件中定义您的侦听器:

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

然后创建您的类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'))); 
    }
}
}

希望能帮助到你。


0
投票

您可以尝试更改routing.yml

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

在你的DefaultController中

    public function registerAction(Request $request)
    {
       
        return $this->redirectToRoute('404OrWhatYouWant');
    }
© www.soinside.com 2019 - 2024. All rights reserved.