사용자 엔티티에 "가입"이라는 올바른 관계가 있다고 가정합니다. 로, 때로는
$sc = $this->get('security.context')
$user = $sc->getToken()->getUser();
$user->setRole('ROLE_NEW');
// Assuming that "main" is your firewall name :
$token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($user, null, 'main', $user->getRoles());
$sc->setToken($token);
그러나 페이지 변경 후
는, 공급자의 refreshUser 함수가 호출되고 : 역할을 변경
public function getRoles()
{
$todayDate = new DateTime();
$activesSubscriptions = $this->subscriptions->filter(function($entity) use ($todayDate) {
return (($todayDate >= $entity->dateBegin()) && ($todayDate < $entity->dateEnd()));
});
if (!isEmpty($activesSubscriptions)) {
return array('ROLE_OK');
}
return array('ROLE_KO');
}
을 수행 할 수 있습니다 :
당신은 아마 뭔가를 시도 할 수 있습니다 EntityUserProvider의 경우이 역할은 쿼리로 덮어 씁니다. 이 문제를 방지하려면 사용자 지정 공급자가 필요합니다.
@ webda2l 내 질문에 대해 이해할 수 없습니다. –
죄송합니다. 좀 더 이해하기 쉽게하려고 노력하겠습니다. 쿼리를 유도 한 Voter 클래스는 사용자 또는 각 페이지로드시 한 번만 호출됩니까? 이 마지막 경우에 쿼리 반복을 피하기 위해 투표 기능에서 세션을 관리하는 것이 가장 좋습니다. 그렇지 않습니까? – webda2l
캐싱 메커니즘을 추가하거나 적절하게 최적화 할 수 있습니다. –