AbstractFactory.php 7.9 KB

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