简体   繁体   中英

Get user from a Validator | Symfony2

In my SF2 project I use a CongeValidator class, which contains a if that calls a function from the service CongesService like that :

if (!$this->congeService->RTTCheck($conge)) {}

The function called looks like that :

public function RTTCheck($conge, $user){
    // $user         = $this->get('security.context')->getToken()->getUser();
    $nbJoursConge = $this->getNombreJoursPoses($conge->getCngDateDebut(), $conge->getCngDateFin(), $conge->getCngDateDebutDemi(), $conge->getCngDateFinDemi(), $user);
    $restants     = $this->RTT_restants($user);
    if(fmod($restants, 0.5)==0)
        if($nbJoursConge-floor($restants) >= 0) return true;
    return false;
}

As you can see, my problem is that I need to retrieve the user (the current one, could also be as a parameter) to use that function. But this function is never called in a Controller, only in the Validator :/ So I have no idea on how to get away with murder the user properly.

Do you have any idea how I can manage that ? Thank you in advance.

You have to inject the securityContext or the TokenStorage (depending on you symfony version) in your service.

Then you can call your user like this

$context->getToken()->getUser()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM