需要在不注销的情况下更新用户的角色。我使用的是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();
}
...
帮助我在不注销的情况下升级角色。
答案 0 :(得分: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);
仅当$ ca == $ this-> getUser()
时,它才有效在securirty.yml中添加的正确变体: 安全: always_authenticate_before_granting:true