2013-02-26 3 views
2

loginSuccess가 사용자 동작에 의존하는 매개 변수에 따라 발생하면 일부 역할을 사용자에게 설정하고 싶습니다.Symfony 2.1.7 - 사용자가 인증 된 후 특정 역할을 설정하는 보안 토큰 업데이트

나는 모습이 있고 역할이 컨테이너를 통해 액세스 할 수있는 security.content.token에 저장되어있는 것 같습니다 :

$this->container->get('security.context')->getToken()

나는 역할이 토큰에 저장되어있는 것을 볼 수 있습니다 (내 경우에 FacebookUserToken에서 FOSFacebookBundle)

다른 요구 사항은 내가 Roles을 설정할 수없고 사용자를 로그 아웃 할 수 없다는 것입니다. 동일한 세션에 있어야합니다.

그럴 수 있습니까?

답변

5

나는이 질문을 발견했습니다 Get security token for non-logged user with Symfony

제가 새로운 보안 토큰을 설정할 수 있다고 생각하는 데 도움 (대신 기존의 역할을 업데이트하려고합니다). 내 사용자의 역할은 User 테이블에 저장되지 않으므로 이해할 수 있습니다.

public function strangeAction() 
{ 
    // Get your User, however you normally get it 
    $user = $userRepository->find($id); 

    // Save the original token in the session (just in case I need to reverse it) 
    $originalToken = $this->get("security.context")->getToken(); 
    $this->getRequest()->getSession()->set('original.security.token', $originalToken); 

    // Create my new custom token (loading the roles of the user) 
    $token = new UsernamePasswordToken($user, null, "main", $user->getRolesMagically()); 

    // Update the security context with the new token 
    $this->get("security.context")->setToken($token); 

    // Now you have access to isGranted() 
    if ($this->get("security.context")->isGranted("ROLE_SOMETHING")) 
} 

이 솔루션에 대한 확신이 들지만 가능하면 입력을 좀 더 원합니다.

즉. 왜 이렇게하지 않아야합니까?