Managing users/roles/groups in FOSUserBundle

后端 未结 2 772
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 09:44

I am developing a simple CRUD to manage users/roles/groups of the application in which I am working. To manage users I\'m using FOSUserBundle. What I want to do

2条回答
  •  没有蜡笔的小新
    2020-12-28 10:36

    I added the functionality to add default group to the user during registration by overriding the confirmAction in Registration Controller

    What I did is I overrided the Registration Controller in my project Bundle by defining the parent to FosUserBUndle .

    Then created a function confirmedAction and in the body of the function added this code

    $repository = $em->getRepository('AdminAdminBundle:Group');
    $group = $repository->findOneByName('staff');
    
    $em = $this->getDoctrine()->getEntityManager();
    $user = $this->getUser();
    $user->addGroup($group);
    
    $userManager = $this->get('fos_user.user_manager');
    
    $userManager->updateUser($user);
    
    if (!is_object($user) || !$user instanceof FOS\UserBundle\Model\UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }
    
    return $this->render('FOSUserBundle:Registration:confirmed.html.twig',
                          ['user' => $user]);
    

    And it perfectly saved in db with group assignment.

    Hope this will help some one in need as there is little information about the implementation in official fosuserbundle doc.

提交回复
热议问题