登录后无法访问FOSUserBundle管理区域 [英] FOSUserBundle admin area not accessible after login
问题描述
我正在按照以下说明使用FOSUserBundle用于管理部分和前端:
I am using FOSUserBundle for admin section as well as frontend by following the instructions given at:
https://github.com/FriendsOfSymfony/FOSUserBundle/issues/849
对于前端,一切正常,但当我访问管理区域/admin
时,对于管理部分而言
然后我被重定向到登录页面/admin/login
(是正确的).一旦我提供了管理员用户名和密码,然后按照登录后的默认目标路径
For frontend everything is working fine but for admin section when i access my admin area /admin
then i am redirected to login page /admin/login
(that is correct). Once i provide admin username and password then as per the default target path of after login
default_target_path: /admin/
在security.yml中定义,它重定向到/admin
(这也是正确的),但是我收到403禁止错误
defined in security.yml it is redirecting to /admin
(that is also correct) but i am getting 403 forbidden error
**Access Denied**
403 Forbidden - AccessDeniedHttpException
1 linked Exception:
AccessDeniedException
当我删除以下行时,在我的security.yml中:
In my security.yml when i remove the below line:
- { path: ^/admin/, role: ROLE_ADMIN }
然后我可以在登录后访问/admin区域.
then i am able to access /admin area after login.
我还观察到,每次在前端使用/register
创建新用户时,都会在 fos_user 数据库表的 roles 字段中输入a:0:{}
.现在我想知道:
I also observed that every time when i create a new user using /register
at front end it is entering a:0:{}
in the roles field of fos_user database table. Now i want to know :
我需要在脚本级别进行哪些更改以创建不同类型的用户,例如管理员,普通用户等,以便我删除的上述security.yml代码无需删除即可工作
What changes i will need to make at script level to create different types of users like admin, normal user etc. so that the above code of security.yml that i removed should work without removing
推荐答案
默认情况下,创建的用户具有角色ROLE_USER
,该角色保存在DB中,就像转换为JSON a:0:{}
的空数组一样.在FOSUserBundle中存在一些有用的命令行工具.您应使用提升用户来设置用户ROLE_ADMIN
,如下所示:
By default, created user has role ROLE_USER
which is saved in DB like empty array converted to JSON a:0:{}
. In FOSUserBundle exists some helpful Command Line Tools. You should use Promote a User for set user ROLE_ADMIN
like this:
$ php app/console fos:user:promote username ROLE_ADMIN
此后,您的username
用户将可以访问管理面板,在这里您可以手动提升其他用户.
After that your username
user will have access to admin panel where you can promote other users manually.
要创建具有不同ROLE
类型的用户,您应该为fos_user.registration.initialize
(甚至fos_user.registration.success
)事件编写事件监听器,如下所示:
To create users with diferent ROLE
types you should write event listener for fos_user.registration.initialize
(or even fos_user.registration.success
) event, like this:
class RegistrationListener
{
public function setUserRole(UserEvent $event)
{
$request = $event->getRequest();
if (/* some conditions */) {
$user = $event->getUser();
$user->addRole('ROLE_STH');
}
}
}
使用此侦听器设置ROLE_ADMIN
时请小心. 升级用户命令旨在添加像ROLE_ADMIN
这样的角色.
Please be careful with using this listener for setting ROLE_ADMIN
. Promote a User command is intended to add role like ROLE_ADMIN
.
这篇关于登录后无法访问FOSUserBundle管理区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!