如何在FOSUserBundle

时间:2015-06-29 00:37:43

标签: symfony fosuserbundle

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

现在,我想让注册操作无法访问,我不知道怎么办? 直到现在,我只是将ROLE_ADMIN放在路由寄存器的访问控制中,以避免访问者抛弃它。

任何提示?

6 个答案:

答案 0 :(得分:21)

查看从

导入的路由配置
  

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

如果您只想要基本的安全措施,只需导入

即可
  

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

而不是

  

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

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

答案 1 :(得分: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);
    }
}

答案 2 :(得分:6)

您只需更改app / config / security.yml:

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

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

答案 3 :(得分: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)。

答案 4 :(得分:2)

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

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

答案 5 :(得分:1)

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

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

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

然后创建您的RegisterListener类:

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

希望它有所帮助。

相关问题