AbstractFactory.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\ChildDefinition;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * AbstractFactory is the base class for all classes inheriting from
  17. * AbstractAuthenticationListener.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  21. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22. */
  23. abstract class AbstractFactory implements SecurityFactoryInterface
  24. {
  25. protected $options = [
  26. 'check_path' => '/login_check',
  27. 'use_forward' => false,
  28. 'require_previous_session' => false,
  29. 'login_path' => '/login',
  30. ];
  31. protected $defaultSuccessHandlerOptions = [
  32. 'always_use_default_target_path' => false,
  33. 'default_target_path' => '/',
  34. 'login_path' => '/login',
  35. 'target_path_parameter' => '_target_path',
  36. 'use_referer' => false,
  37. ];
  38. protected $defaultFailureHandlerOptions = [
  39. 'failure_path' => null,
  40. 'failure_forward' => false,
  41. 'login_path' => '/login',
  42. 'failure_path_parameter' => '_failure_path',
  43. ];
  44. public function create(ContainerBuilder $container, string $id, array $config, string $userProviderId, ?string $defaultEntryPointId)
  45. {
  46. // authentication provider
  47. $authProviderId = $this->createAuthProvider($container, $id, $config, $userProviderId);
  48. // authentication listener
  49. $listenerId = $this->createListener($container, $id, $config, $userProviderId);
  50. // add remember-me aware tag if requested
  51. if ($this->isRememberMeAware($config)) {
  52. $container
  53. ->getDefinition($listenerId)
  54. ->addTag('security.remember_me_aware', ['id' => $id, 'provider' => $userProviderId])
  55. ;
  56. }
  57. // create entry point if applicable (optional)
  58. $entryPointId = $this->createEntryPoint($container, $id, $config, $defaultEntryPointId);
  59. return [$authProviderId, $listenerId, $entryPointId];
  60. }
  61. public function addConfiguration(NodeDefinition $node)
  62. {
  63. $builder = $node->children();
  64. $builder
  65. ->scalarNode('provider')->end()
  66. ->booleanNode('remember_me')->defaultTrue()->end()
  67. ->scalarNode('success_handler')->end()
  68. ->scalarNode('failure_handler')->end()
  69. ;
  70. foreach (array_merge($this->options, $this->defaultSuccessHandlerOptions, $this->defaultFailureHandlerOptions) as $name => $default) {
  71. if (\is_bool($default)) {
  72. $builder->booleanNode($name)->defaultValue($default);
  73. } else {
  74. $builder->scalarNode($name)->defaultValue($default);
  75. }
  76. }
  77. }
  78. final public function addOption(string $name, $default = null)
  79. {
  80. $this->options[$name] = $default;
  81. }
  82. /**
  83. * Subclasses must return the id of a service which implements the
  84. * AuthenticationProviderInterface.
  85. *
  86. * @return string never null, the id of the authentication provider
  87. */
  88. abstract protected function createAuthProvider(ContainerBuilder $container, string $id, array $config, string $userProviderId);
  89. /**
  90. * Subclasses must return the id of the abstract listener template.
  91. *
  92. * Listener definitions should inherit from the AbstractAuthenticationListener
  93. * like this:
  94. *
  95. * <service id="my.listener.id"
  96. * class="My\Concrete\Classname"
  97. * parent="security.authentication.listener.abstract"
  98. * abstract="true" />
  99. *
  100. * In the above case, this method would return "my.listener.id".
  101. *
  102. * @return string
  103. */
  104. abstract protected function getListenerId();
  105. /**
  106. * Subclasses may create an entry point of their as they see fit. The
  107. * default implementation does not change the default entry point.
  108. *
  109. * @return string|null the entry point id
  110. */
  111. protected function createEntryPoint(ContainerBuilder $container, string $id, array $config, ?string $defaultEntryPointId)
  112. {
  113. return $defaultEntryPointId;
  114. }
  115. /**
  116. * Subclasses may disable remember-me features for the listener, by
  117. * always returning false from this method.
  118. *
  119. * @return bool Whether a possibly configured RememberMeServices should be set for this listener
  120. */
  121. protected function isRememberMeAware(array $config)
  122. {
  123. return $config['remember_me'];
  124. }
  125. protected function createListener(ContainerBuilder $container, string $id, array $config, string $userProvider)
  126. {
  127. $listenerId = $this->getListenerId();
  128. $listener = new ChildDefinition($listenerId);
  129. $listener->replaceArgument(4, $id);
  130. $listener->replaceArgument(5, new Reference($this->createAuthenticationSuccessHandler($container, $id, $config)));
  131. $listener->replaceArgument(6, new Reference($this->createAuthenticationFailureHandler($container, $id, $config)));
  132. $listener->replaceArgument(7, array_intersect_key($config, $this->options));
  133. $listenerId .= '.'.$id;
  134. $container->setDefinition($listenerId, $listener);
  135. return $listenerId;
  136. }
  137. protected function createAuthenticationSuccessHandler(ContainerBuilder $container, string $id, array $config)
  138. {
  139. $successHandlerId = $this->getSuccessHandlerId($id);
  140. $options = array_intersect_key($config, $this->defaultSuccessHandlerOptions);
  141. if (isset($config['success_handler'])) {
  142. $successHandler = $container->setDefinition($successHandlerId, new ChildDefinition('security.authentication.custom_success_handler'));
  143. $successHandler->replaceArgument(0, new Reference($config['success_handler']));
  144. $successHandler->replaceArgument(1, $options);
  145. $successHandler->replaceArgument(2, $id);
  146. } else {
  147. $successHandler = $container->setDefinition($successHandlerId, new ChildDefinition('security.authentication.success_handler'));
  148. $successHandler->addMethodCall('setOptions', [$options]);
  149. $successHandler->addMethodCall('setFirewallName', [$id]);
  150. }
  151. return $successHandlerId;
  152. }
  153. protected function createAuthenticationFailureHandler(ContainerBuilder $container, string $id, array $config)
  154. {
  155. $id = $this->getFailureHandlerId($id);
  156. $options = array_intersect_key($config, $this->defaultFailureHandlerOptions);
  157. if (isset($config['failure_handler'])) {
  158. $failureHandler = $container->setDefinition($id, new ChildDefinition('security.authentication.custom_failure_handler'));
  159. $failureHandler->replaceArgument(0, new Reference($config['failure_handler']));
  160. $failureHandler->replaceArgument(1, $options);
  161. } else {
  162. $failureHandler = $container->setDefinition($id, new ChildDefinition('security.authentication.failure_handler'));
  163. $failureHandler->addMethodCall('setOptions', [$options]);
  164. }
  165. return $id;
  166. }
  167. protected function getSuccessHandlerId(string $id)
  168. {
  169. return 'security.authentication.success_handler.'.$id.'.'.str_replace('-', '_', $this->getKey());
  170. }
  171. protected function getFailureHandlerId(string $id)
  172. {
  173. return 'security.authentication.failure_handler.'.$id.'.'.str_replace('-', '_', $this->getKey());
  174. }
  175. }