FOSUserBundle admin area not accessible after login
我正在按照以下给出的说明将FOSUserBundle用于管理部分和前端
1 | https://github.com/FriendsOfSymfony/FOSUserBundle/issues/849 |
对于前端,一切正常,但当我访问管理区域
然后我被重定向到登录页面
1 | default_target_path: /admin/ |
在security.yml中定义的
它将重定向到
1 2 3 4 | **Access Denied** 403 Forbidden - AccessDeniedHttpException 1 linked Exception: AccessDeniedException |
当我删除以下行时,在我的security.yml中:
1 | - { path: ^/admin/, role: ROLE_ADMIN } |
然后我可以在登录后访问/ admin区域。
我还观察到,每次在前端使用
我将需要在脚本级别进行哪些更改以创建不同类型的用户(例如admin,普通用户等),以便我删除的上述security.yml代码在不删除的情况下也可以工作
默认情况下,创建的用户具有角色
1 | $ php app/console fos:user:promote username ROLE_ADMIN |
之后,您的
要创建具有不同
1 2 3 4 5 6 7 8 9 10 11 | class RegistrationListener { public function setUserRole(UserEvent $event) { $request = $event->getRequest(); if (/* some conditions */) { $user = $event->getUser(); $user->addRole('ROLE_STH'); } } } |
请谨慎使用此监??听器设置
这样的角色
@neeraj,作为对您评论的回答,在登录后无法访问FOSUserBundle管理区域
正如我所知,仅使用security.yml是不可能的,但是您可以使用侦听器,没什么大不了的。
在捆绑包中创建文件夹EventListener,然后创建SecurityListener.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php namespace Your\ ameBundle\\EventListener; use Symfony\\Bundle\\FrameworkBundle\ outing\ outer; 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\ edirectResponse; 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中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 | <parameters> <parameter key="yourbundle.listener.login.class">Your\ ameBundle\\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"/> </service> </services> |