AccountBlockService.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\UserBundle\Block;
  11. use Sonata\BlockBundle\Block\BlockContextInterface;
  12. use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
  13. use Sonata\UserBundle\Model\UserInterface;
  14. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\SecurityContextInterface;
  19. /**
  20. * Render a block with the connection option or the login name.
  21. *
  22. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  23. */
  24. class AccountBlockService extends AbstractAdminBlockService
  25. {
  26. /**
  27. * @var TokenStorageInterface|SecurityContextInterface
  28. */
  29. private $tokenStorage;
  30. /**
  31. * NEXT_MAJOR: Go back to type hinting check when bumping requirements to SF 2.6+.
  32. *
  33. * @param string $name
  34. * @param EngineInterface $templating
  35. * @param TokenStorageInterface|SecurityContextInterface $tokenStorage
  36. */
  37. public function __construct($name, EngineInterface $templating, $tokenStorage)
  38. {
  39. parent::__construct($name, $templating);
  40. if (!$tokenStorage instanceof TokenStorageInterface && !$tokenStorage instanceof SecurityContextInterface) {
  41. throw new \InvalidArgumentException(
  42. 'Argument 3 should be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface or Symfony\Component\Security\Core\SecurityContextInterface'
  43. );
  44. }
  45. $this->tokenStorage = $tokenStorage;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function execute(BlockContextInterface $blockContext, Response $response = null)
  51. {
  52. $user = false;
  53. if ($this->tokenStorage->getToken()) {
  54. $user = $this->tokenStorage->getToken()->getUser();
  55. }
  56. if (!$user instanceof UserInterface) {
  57. $user = false;
  58. }
  59. return $this->renderPrivateResponse($blockContext->getTemplate(), array(
  60. 'user' => $user,
  61. 'block' => $blockContext->getBlock(),
  62. 'context' => $blockContext,
  63. ));
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function configureSettings(OptionsResolver $resolver)
  69. {
  70. $resolver->setDefaults(array(
  71. 'template' => 'SonataUserBundle:Block:account.html.twig',
  72. 'ttl' => 0,
  73. ));
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getName()
  79. {
  80. return 'Account Block';
  81. }
  82. }