0

我按照以下说明将 FOSUserBundle 用于管理部分和前端:

https://github.com/FriendsOfSymfony/FOSUserBundle/issues/849

对于前端,一切正常,但对于管理部分,当我访问我的管理区域/admin 时,我被重定向到登录页面/admin/login(这是正确的)。一旦我提供管理员用户名和密码,然后按照登录后的默认目标路径

default_target_path: /admin/

在 security.yml 中定义它正在重定向到/admin(这也是正确的)但我收到 403 禁止错误

**Access Denied**
403 Forbidden - AccessDeniedHttpException
1 linked Exception:
    AccessDeniedException     

当我删除以下行时,在我的 security.yml 中:

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

然后我可以在登录后访问 /admin 区域。

我还观察到,每次我/register在前端使用创建新用户时,它都会输入fos_user数据库表a:0:{}角色字段。现在我想知道:

我需要在脚本级别进行哪些更改以创建不同类型的用户,如管理员、普通用户等,以便我删除的上述 security.yml 代码可以在不删除的情况下工作

4

2 回答 2

7

默认情况下,创建的用户具有ROLE_USER保存在数据库中的角色,例如转换为 JSON 的空数组a:0:{}。在 FOSUserBundle 中存在一些有用的命令行工具。您应该像这样对设置用户使用提升用户ROLE_ADMIN

$ php app/console fos:user:promote username ROLE_ADMIN

之后,您的username用户将有权访问管理面板,您可以在其中手动提升其他用户。

要创建具有不同类型的用户,您应该为(甚至)事件ROLE编写事件侦听器,如下所示:fos_user.registration.initializefos_user.registration.success

class RegistrationListener
{
    public function setUserRole(UserEvent $event)
    {
        $request = $event->getRequest();
        if (/* some conditions */) {
            $user = $event->getUser();
            $user->addRole('ROLE_STH');
        }
    }
}

请小心使用此侦听器进行设置ROLE_ADMIN提升用户命令旨在添加类似ROLE_ADMIN.

于 2013-09-13T06:43:05.233 回答
2

@neeraj,作为对您在此处的评论的回答FOSUserBundle 管理区域在登录后无法访问, 因为我知道仅使用 security.yml 是不可能的,但您可以使用侦听器,没什么可做的。

在您的 Bundle 中创建文件夹 EventListener,然后创建 SecurityListener.php

<?php

namespace Your\NameBundle\EventListener;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;

class SecurityListener
{
    protected $router;
    protected $security;
    protected $dispatcher;

    public function __construct(Router $router, SecurityContext $security, EventDispatcher $dispatcher)
    {
        $this->router = $router;
        $this->security = $security;
        $this->dispatcher = $dispatcher;
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $this->dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse'));
    }

    public function onKernelResponse(FilterResponseEvent $event)
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            $response = new RedirectResponse($this->router->generate('YOURCoreBundle_adminpage'));
        } elseif ($this->security->isGranted('ROLE_USER')) {
            $response = new RedirectResponse($this->router->generate('YOURBundle_userpage'));
        } else {
            $response = new RedirectResponse($this->router->generate('YOURCoreBundle_homepage'));
        }

        $event->setResponse($response);
    }
}

并在 services.xml 添加

<parameters>
    <parameter key="yourbundle.listener.login.class">Your\NameBundle\EventListener\SecurityListener</parameter>
</parameters>

<services>
    <service id="yourbundle.listener.login" class="%yourbundle.listener.login.class%">
        <tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin"/>
        <argument type="service" id="router"/>
        <argument type="service" id="security.context"/>
        <argument type="service" id="event_dispatcher"/>
    </service>
</services>
于 2013-09-14T13:58:13.343 回答