HttpBasicFactory.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
  11. use Symfony\Component\Config\Definition\Builder\NodeDefinition;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * HttpBasicFactory creates services for HTTP basic authentication.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class HttpBasicFactory implements SecurityFactoryInterface
  21. {
  22. public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
  23. {
  24. $provider = 'security.authentication.provider.dao.'.$id;
  25. $container
  26. ->setDefinition($provider, new DefinitionDecorator('security.authentication.provider.dao'))
  27. ->replaceArgument(0, new Reference($userProvider))
  28. ->replaceArgument(1, new Reference('security.user_checker.'.$id))
  29. ->replaceArgument(2, $id)
  30. ;
  31. // entry point
  32. $entryPointId = $this->createEntryPoint($container, $id, $config, $defaultEntryPoint);
  33. // listener
  34. $listenerId = 'security.authentication.listener.basic.'.$id;
  35. $listener = $container->setDefinition($listenerId, new DefinitionDecorator('security.authentication.listener.basic'));
  36. $listener->replaceArgument(2, $id);
  37. $listener->replaceArgument(3, new Reference($entryPointId));
  38. $listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
  39. return array($provider, $listenerId, $entryPointId);
  40. }
  41. public function getPosition()
  42. {
  43. return 'http';
  44. }
  45. public function getKey()
  46. {
  47. return 'http-basic';
  48. }
  49. public function addConfiguration(NodeDefinition $node)
  50. {
  51. $node
  52. ->children()
  53. ->scalarNode('provider')->end()
  54. ->scalarNode('realm')->defaultValue('Secured Area')->end()
  55. ->end()
  56. ;
  57. }
  58. protected function createEntryPoint($container, $id, $config, $defaultEntryPoint)
  59. {
  60. if (null !== $defaultEntryPoint) {
  61. return $defaultEntryPoint;
  62. }
  63. $entryPointId = 'security.authentication.basic_entry_point.'.$id;
  64. $container
  65. ->setDefinition($entryPointId, new DefinitionDecorator('security.authentication.basic_entry_point'))
  66. ->addArgument($config['realm'])
  67. ;
  68. return $entryPointId;
  69. }
  70. }