如何在FOSUserBundle中明确禁用注册

Abd*_*ood 16 symfony fosuserbundle

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

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

有小费吗?

Pet*_*ina 21

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

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

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

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

代替

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

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


Mik*_*lov 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>
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您应该分别将FOS路由导入app/config/routing.yml.只需为registration.xml添加所需功能的路由(security.xml,resetting.xml,profile.xml,change_password.xml). (2认同)

the*_*eba 6

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

- { path: ^/register, role: ROLE_ADMIN }
Run Code Online (Sandbox Code Playgroud)

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