0

Need to update the role of the user without logout. I'm using sonata admin bundle:

class CompanyAdmin extends Admin
{
...
public function postPersist($company)
    {       
        $this->checkCompanyAdmin($company);
    }
...
protected function checkCompanyAdmin($company){
        $companyAdmins = $company->getCompanyAdmins();
        if($companyAdmins) {
            $companyDefaultGroup = $company->getDefaultGroup();
            foreach ($companyAdmins as $ca) {
                $ca->addRole('ROLE_COMPANY_'.$company->getId().'_ADMIN');
                $ca->addRole('ROLE_PARTNER');
                $ca->setCompany($company);          
                //***
                if($companyDefaultGroup) {
                    $ca->addGroup($companyDefaultGroup);
                }
            }
        }
        $this->em->flush();
    }   
...

Help me to upgrade the roles without logout.

PuLWaR
  • 1
  • 3

1 Answers1

0
$token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($ca,null,'main',$ca->getRoles());
$this->getConfigurationPool()->getContainer()->get('security.context')->setToken($token);
$this->getConfigurationPool()->getContainer()->get('fos_user.user_manager')->refreshUser($ca);

It is works right only if $ca==$this->getUser()

The right variant it is to add in securirty.yml: security: always_authenticate_before_granting: true

PuLWaR
  • 1
  • 3