FOSUserBundle admin area not accessible after login

后端 未结 2 2021
清歌不尽
清歌不尽 2021-01-07 17:05

I am using FOSUserBundle for admin section as well as frontend by following the instructions given at:

https://github.com/FriendsOfSymfony/FOSUserBundle/issu         


        
2条回答
  •  猫巷女王i
    2021-01-07 18:04

    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
    

    After that your username user will have access to admin panel where you can promote other users manually.

    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');
            }
        }
    }
    

    Please be careful with using this listener for setting ROLE_ADMIN. Promote a User command is intended to add role like ROLE_ADMIN.

提交回复
热议问题