When are user roles refreshed and how to force it?

后端 未结 8 1027
梦谈多话
梦谈多话 2020-12-14 02:57

First off, I\'m not using FOSUserBundle and I can\'t because I\'m porting a legacy system which has its own Model layer (no Doctrine/Mongo/whatsoever here) and other very cu

8条回答
  •  既然无缘
    2020-12-14 03:38

    I achieve this behaviour by implementing my own EntityUserProvider and overriding loadByUsername($username) method :

       /**
        * Load an user from its username
        * @param string $username
        * @return UserInterface
        */
       public function loadUserByUsername($username)
       {
          $user = $this->repository->findOneByEmailJoinedToCustomerAccount($username);
    
          if (null === $user)
          {
             throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
          }
    
          //Custom function to definassigned roles to an user
          $roles = $this->loadRolesForUser($user);
    
          //Set roles to the user entity
          $user->setRoles($roles);
    
          return $user;
       }
    

    The trick is to call setRoles each time you call loadByUsername ... Hope it helps

提交回复
热议问题