The UserChecker in Symfony2 is a necessary class for user authentication.
Let’s suppose you need to authenticate a user just inside a controller, you should define a function like the following:
protected function authenticateUser(User $user) {
$user_checker = ...//Get here the user checker
try {
$user_checker->checkPostAuth($user);
} catch (AccountStatusException $e) {
return;
}
$providerKey = $this->container->getParameter('firewall_name');
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->container->get('security.context')->setToken($token);
}
Notice that the UserChecker is defined as a non-public service in Symfony2:
<service id="security.user_checker" class="%security.user_checker.class%" public="false" />
How to get the UserChecker in a controller?
Thanks in advance
You can “get” a private service by using an alias. See:
http://symfony.com/doc/current/components/dependency_injection/advanced.html