PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/hwi/oauth-bundle/DependencyInjection/HWIOAuthExtension.php

https://gitlab.com/Snizer/PI-DEV-TUNISIAMALL3A6-WEB
PHP | 181 lines | 112 code | 28 blank | 41 comment | 18 complexity | f70feca5cf153f1249d07c3bd7169e68 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the HWIOAuthBundle package.
  4. *
  5. * (c) Hardware.Info <opensource@hardware.info>
  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 HWI\Bundle\OAuthBundle\DependencyInjection;
  11. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  12. use Symfony\Component\Config\Definition\Processor;
  13. use Symfony\Component\Config\FileLocator;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  16. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  19. /**
  20. * HWIOAuthExtension
  21. *
  22. * @author Geoffrey Bachelet <geoffrey.bachelet@gmail.com>
  23. * @author Alexander <iam.asm89@gmail.com>
  24. */
  25. class HWIOAuthExtension extends Extension
  26. {
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function load(array $configs, ContainerBuilder $container)
  31. {
  32. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/'));
  33. $loader->load('oauth.xml');
  34. $loader->load('templating.xml');
  35. $loader->load('twig.xml');
  36. $loader->load('http_client.xml');
  37. $processor = new Processor();
  38. $config = $processor->processConfiguration(new Configuration(), $configs);
  39. // setup http client settings
  40. $httpClient = $container->getDefinition('hwi_oauth.http_client');
  41. $httpClient->addMethodCall('setVerifyPeer', array($config['http_client']['verify_peer']));
  42. $httpClient->addMethodCall('setTimeout', array($config['http_client']['timeout']));
  43. $httpClient->addMethodCall('setMaxRedirects', array($config['http_client']['max_redirects']));
  44. $httpClient->addMethodCall('setIgnoreErrors', array($config['http_client']['ignore_errors']));
  45. if (isset($config['http_client']['proxy']) && $config['http_client']['proxy'] != '') {
  46. $httpClient->addMethodCall('setProxy', array($config['http_client']['proxy']));
  47. }
  48. // set current firewall
  49. if (empty($config['firewall_names']) && !isset($config['firewall_name'])) {
  50. throw new InvalidConfigurationException('The child node "firewall_name" or "firewall_names" at path "hwi_oauth" must be configured.');
  51. } elseif (!empty($config['firewall_names']) && isset($config['firewall_name'])) {
  52. $config['firewall_names'] = array_merge(array($config['firewall_name']), $config['firewall_names']);
  53. } elseif (empty($config['firewall_names']) && isset($config['firewall_name'])) {
  54. @trigger_error('The child node "firewall_name" at path "hwi_oauth" is deprecated since version 0.4.0 and will be removed in version 0.5.0. Use "firewall_names" instead.', E_USER_DEPRECATED);
  55. $config['firewall_names'] = array($config['firewall_name']);
  56. }
  57. $container->setParameter('hwi_oauth.firewall_names', $config['firewall_names']);
  58. // set target path parameter
  59. $container->setParameter('hwi_oauth.target_path_parameter', $config['target_path_parameter']);
  60. // set use referer parameter
  61. $container->setParameter('hwi_oauth.use_referer', $config['use_referer']);
  62. // set failed auth path
  63. $container->setParameter('hwi_oauth.failed_auth_path', $config['failed_auth_path']);
  64. // setup services for all configured resource owners
  65. $resourceOwners = array();
  66. foreach ($config['resource_owners'] as $name => $options) {
  67. $resourceOwners[$name] = $name;
  68. $this->createResourceOwnerService($container, $name, $options);
  69. }
  70. $container->setParameter('hwi_oauth.resource_owners', $resourceOwners);
  71. $oauthUtils = $container->getDefinition('hwi_oauth.security.oauth_utils');
  72. foreach ($config['firewall_names'] as $firewallName) {
  73. $oauthUtils->addMethodCall('addResourceOwnerMap', array(new Reference('hwi_oauth.resource_ownermap.'.$firewallName)));
  74. }
  75. // Symfony <2.6 BC
  76. // Go back to basic xml config after
  77. if (interface_exists('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')) {
  78. $oauthUtils->replaceArgument(1, new Reference('security.authorization_checker'));
  79. } else {
  80. $oauthUtils->replaceArgument(1, new Reference('security.context'));
  81. }
  82. if (isset($config['fosub'])) {
  83. $container
  84. ->setDefinition('hwi_oauth.user.provider.fosub_bridge', new DefinitionDecorator('hwi_oauth.user.provider.fosub_bridge.def'))
  85. ->addArgument($config['fosub']['properties'])
  86. ;
  87. }
  88. // check of the connect controllers etc should be enabled
  89. if (isset($config['connect'])) {
  90. $container->setParameter('hwi_oauth.connect', true);
  91. if (isset($config['fosub'])) {
  92. // setup fosub bridge services
  93. $container->setAlias('hwi_oauth.account.connector', 'hwi_oauth.user.provider.fosub_bridge');
  94. $container
  95. ->setDefinition('hwi_oauth.registration.form.handler.fosub_bridge', new DefinitionDecorator('hwi_oauth.registration.form.handler.fosub_bridge.def'))
  96. ->addArgument($config['fosub']['username_iterations'])
  97. ->setScope('request')
  98. ;
  99. $container->setAlias('hwi_oauth.registration.form.handler', 'hwi_oauth.registration.form.handler.fosub_bridge');
  100. // enable compatibility with FOSUserBundle 1.3.x and 2.x
  101. if (interface_exists('FOS\UserBundle\Form\Factory\FactoryInterface')) {
  102. $container->setAlias('hwi_oauth.registration.form.factory', 'fos_user.registration.form.factory');
  103. } else {
  104. $container->setAlias('hwi_oauth.registration.form', 'fos_user.registration.form');
  105. }
  106. }
  107. foreach ($config['connect'] as $key => $serviceId) {
  108. if ('confirmation' === $key) {
  109. $container->setParameter('hwi_oauth.connect.confirmation', $config['connect']['confirmation']);
  110. continue;
  111. }
  112. $container->setAlias('hwi_oauth.'.str_replace('_', '.', $key), $serviceId);
  113. }
  114. // setup custom services
  115. } else {
  116. $container->setParameter('hwi_oauth.connect', false);
  117. }
  118. $container->setParameter('hwi_oauth.templating.engine', $config['templating_engine']);
  119. $container->setAlias('hwi_oauth.user_checker', 'security.user_checker');
  120. }
  121. /**
  122. * Creates a resource owner service.
  123. *
  124. * @param ContainerBuilder $container The container builder
  125. * @param string $name The name of the service
  126. * @param array $options Additional options of the service
  127. */
  128. public function createResourceOwnerService(ContainerBuilder $container, $name, array $options)
  129. {
  130. // alias services
  131. if (isset($options['service'])) {
  132. // set the appropriate name for aliased services, compiler pass depends on it
  133. $container->setAlias('hwi_oauth.resource_owner.'.$name, $options['service']);
  134. } else {
  135. $type = $options['type'];
  136. unset($options['type']);
  137. $definition = new DefinitionDecorator('hwi_oauth.abstract_resource_owner.'.Configuration::getResourceOwnerType($type));
  138. $definition->setClass("%hwi_oauth.resource_owner.$type.class%");
  139. $container->setDefinition('hwi_oauth.resource_owner.'.$name, $definition);
  140. $definition
  141. ->replaceArgument(2, $options)
  142. ->replaceArgument(3, $name)
  143. ;
  144. }
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function getAlias()
  150. {
  151. return 'hwi_oauth';
  152. }
  153. }