LoginManager.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Security;
  11. use FOS\UserBundle\Model\UserInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  15. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  16. use Symfony\Component\Security\Core\SecurityContextInterface;
  17. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  18. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  19. /**
  20. * Abstracts process for manually logging in a user.
  21. *
  22. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23. */
  24. class LoginManager implements LoginManagerInterface
  25. {
  26. private $securityContext;
  27. private $userChecker;
  28. private $sessionStrategy;
  29. private $container;
  30. public function __construct(SecurityContextInterface $context, UserCheckerInterface $userChecker,
  31. SessionAuthenticationStrategyInterface $sessionStrategy,
  32. ContainerInterface $container)
  33. {
  34. $this->securityContext = $context;
  35. $this->userChecker = $userChecker;
  36. $this->sessionStrategy = $sessionStrategy;
  37. $this->container = $container;
  38. }
  39. final public function loginUser($firewallName, UserInterface $user, Response $response = null)
  40. {
  41. $this->userChecker->checkPostAuth($user);
  42. $token = $this->createToken($firewallName, $user);
  43. if ($this->container->isScopeActive('request')) {
  44. $this->sessionStrategy->onAuthentication($this->container->get('request'), $token);
  45. if (null !== $response) {
  46. $rememberMeServices = null;
  47. if ($this->container->has('security.authentication.rememberme.services.persistent.'.$firewallName)) {
  48. $rememberMeServices = $this->container->get('security.authentication.rememberme.services.persistent.'.$firewallName);
  49. } elseif ($this->container->has('security.authentication.rememberme.services.simplehash.'.$firewallName)) {
  50. $rememberMeServices = $this->container->get('security.authentication.rememberme.services.simplehash.'.$firewallName);
  51. }
  52. if ($rememberMeServices instanceof RememberMeServicesInterface) {
  53. $rememberMeServices->loginSuccess($this->container->get('request'), $response, $token);
  54. }
  55. }
  56. }
  57. $this->securityContext->setToken($token);
  58. }
  59. protected function createToken($firewall, UserInterface $user)
  60. {
  61. return new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
  62. }
  63. }