如何在FOSUserBundle中明确禁止注册[英] How to definitely disable registration in FOSUserBundle

本文是小编为大家收集整理的关于如何在FOSUserBundle中明确禁止注册的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

现在,我想获得无法访问的注册操作,我不知道如何? 到目前为止,我只是将角色_ADMIN放入路由寄存器的访问控制中,以避免访问者可以将其投掷.

有任何技巧?

推荐答案

有很多方法可以解决此问题.您可以简单地将fos_user_registration_register路由从routing.yml中删除.或使用更复杂的解决方案:将事件侦听器设置为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);
    }
}

其他推荐答案

看一下从

导入的路由配置

供应商/friendsofsymfony/user-bundle/resources/config/config/routing/all.xml

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

@fosuserbundle/resources/config/config/routing/security.xml

而不是

@fosuserbundle/resources/config/routing/all.xml

这样,您只需选择要使用的组件(安全性,配置文件,重置,更改_Password)或仅从这些组件导入特定路由的组件.

其他推荐答案

您只需更改应用程序/config/security.yml:

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

更改默认值(is_authentication_anonyly)到 cool_admin ,它将停止允许匿名用户到达/寄存器表单.

本文地址:https://www.itbaoku.cn/post/2090772.html

问题描述

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?

推荐答案

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

其他推荐答案

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.

其他推荐答案

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.