Symfony2, Sonata. Update roles without logout

Symfony2, Sonata. Update roles without logout,symfony,sonata-admin,user-roles,Symfony,Sonata Admin,User Roles,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-&

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.


#1

$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


#2

I found te solution: svistelki.ru/symfony2-kak-obnovit-roli-polzovatelya