php - 在不登录用户的情况下使用 FOSUserBundle 注册用户

标签 php symfony fosuserbundle

我正在尝试使用 FOSUserBundle 进行注册。我希望管理员能够注册新用户,我只需将注册更改为防火墙路由(/admin/register 前缀)即可完成此操作。

我创建了一个自定义注册表单类型,并按照此处的说明将其注册为服务:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md

然后我 Hook 到基于此的注册事件:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/controller_events.md

我的听众看起来像这样:

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;


class RegistrationListener implements EventSubscriberInterface
{
    private $router;

    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_COMPLETED => 'onRegisterDone',
            FOSUserEvents::REGISTRATION_CONFIRMED => 'onRegisterDone',
            FOSUserEvents::RESETTING_RESET_COMPLETED => 'onRegisterDone',
        );
    }

    public function onRegisterDone(FilterUserResponseEvent $event)
    {
        $url = $this->router->generate('admin_panel');

        //$event->setResponse(new RedirectResponse($url));
    }
}

FilterUserResponseEvent 没有 setResponse 方法,所以我只是让它运行。我认为订阅此事件会覆盖默认的 FOS\UserBundle\EventListener\AuthenticationListener 订阅事件并阻止用户登录,但新用户仍会登录。

是否可以阻止身份验证,或者我应该简单地创建一个新表单来调用调用用户管理器的操作?

最佳答案

有同样的问题,这对我有用。

在覆盖的注册 Controller 中注释掉这一行:

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

public function registerAction(Request $request)
{
    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
    $formFactory = $this->container->get('fos_user.registration.form.factory');
    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
    $userManager = $this->container->get('fos_user.user_manager');
    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->container->get('event_dispatcher');

    $user = $userManager->createUser();
    $user->setEnabled(true);

    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, new UserEvent($user, $request));

    $form = $formFactory->createForm();
    $form->setData($user);

    if ('POST' === $request->getMethod()) {
        $form->bind($request);

        if ($form->isValid()) {
            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

            $userManager->updateUser($user);

            if (null === $response = $event->getResponse()) {
                $url = $this->container->get('router')->generate('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }

            //comment below line to prevent login user after registration
            //$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }
    }

    return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
            'form' => $form->createView(),
    ));
}

关于php - 在不登录用户的情况下使用 FOSUserBundle 注册用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038775/

相关文章:

php - 在cakephp中使用别名时如何指定查询选择的字段类型?

php - 如何将 php 输出行保持在一行中

php - 使用 Symfony2 中的 Doctrine ORM 注释添加 "indirectly associated"实体作为成员

php - 如何仅禁用一个 UserType 而不是另一个?

php - 如何使用opencart向数据库中插入数据?

php - Codeigniter 在 config/database.php 之外创建数据库和表

mysql - Propel 1.6 如何在 symfony2 中自动生成 Base/Peer/Query 模型类

symfony - 未定义的方法 registerNamespaces() symfony2

symfony - 如何使用自定义函数 Twig-symfony

symfony - FOSUser Bundle..覆盖表单类型时无法加载类型 “X”错误