如何绝对禁用FOSUserBundle中的注册 [英] How to definitely disable registration in FOSUserBundle
问题描述
在我的项目中,我只允许一个用户管理网站的内容.首先,将使用命令行添加该用户.
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.
现在,我想使注册操作不可访问,并且我不知道该怎么办? 到现在为止,我只是将ROLE_ADMIN放置在路线寄存器的访问控制中,以避免访问者将其扔掉.
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.
有什么提示吗?
推荐答案
有很多方法可以解决此问题.您可以简单地从routing.yml中删除fos_user_registration_register路由.或使用更复杂的解决方案:将事件侦听器设置为FOS \ UserBundle \ FOSUserEvents :: REGISTRATION_INITIALIZE事件,并将用户重定向到登录页面.
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
services.xml
<service id="app.registration.listener" class="AppBundle\EventListener\RegistrationListener">
<tag name="kernel.event_subscriber" />
<argument type="service" id="router" />
</service>
RegistrationListener.php
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);
}
}
这篇关于如何绝对禁用FOSUserBundle中的注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!