JMSSecurityExtraExtension.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SecurityExtraBundle\DependencyInjection;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. use JMS\SecurityExtraBundle\Exception\RuntimeException;
  20. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  21. use Symfony\Component\Config\FileLocator;
  22. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. /**
  25. * JMSSecurityExtraExtension.
  26. *
  27. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  28. */
  29. class JMSSecurityExtraExtension extends Extension
  30. {
  31. public function load(array $configs, ContainerBuilder $container)
  32. {
  33. $bundles = $container->getParameter('kernel.bundles');
  34. if (!isset($bundles['JMSAopBundle'])) {
  35. throw new RuntimeException('The JMSSecurityExtraBundle requires the JMSAopBundle, please make sure to enable it in your AppKernel.');
  36. }
  37. $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
  38. $loader = new XmlFileLoader($container, new FileLocator(array(__DIR__.'/../Resources/config/')));
  39. $loader->load('services.xml');
  40. $container->setParameter('security.access.secure_all_services', $config['secure_all_services']);
  41. $cacheDir = $container->getParameterBag()->resolveValue($config['cache_dir']);
  42. if (!is_dir($cacheDir)) {
  43. if (false === @mkdir($cacheDir, 0777, true)) {
  44. throw new RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir));
  45. }
  46. }
  47. $container->setParameter('security.extra.cache_dir', $cacheDir);
  48. if ($config['expressions']) {
  49. $loader->load('security_expressions.xml');
  50. if (!is_dir($cacheDir.'/expressions')) {
  51. if (false === @mkdir($cacheDir.'/expressions', 0777, true)) {
  52. throw new RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir.'/expressions'));
  53. }
  54. }
  55. $container->getDefinition('security.expressions.voter')
  56. ->addMethodCall('setCacheDir', array($cacheDir.'/expressions'));
  57. }
  58. $disableAllVoters = !isset($config['voters']);
  59. $container->setParameter('security.authenticated_voter.disabled',
  60. $disableAllVoters || $config['voters']['disable_authenticated']);
  61. $container->setParameter('security.role_voter.disabled',
  62. $disableAllVoters || $config['voters']['disable_role']);
  63. $container->setParameter('security.acl_voter.disabled',
  64. $disableAllVoters || $config['voters']['disable_acl']);
  65. if ($config['enable_iddqd_attribute']) {
  66. $container
  67. ->getDefinition('security.extra.iddqd_voter')
  68. ->addTag('security.voter')
  69. ;
  70. // FIXME: Also add an iddqd after invocation provider
  71. }
  72. if ($config['method_access_control']) {
  73. $driverDef = $container->getDefinition('security.extra.driver_chain');
  74. $args = $driverDef->getArguments();
  75. array_unshift($args[0], new Reference('security.extra.config_driver'));
  76. $driverDef->setArguments($args);
  77. $container->setParameter('security.access.method_access_control',
  78. $config['method_access_control']);
  79. }
  80. if (isset($config['util']['secure_random'])) {
  81. $loader->load('security_secure_random.xml');
  82. $this->configureSecureRandom($config['util']['secure_random'], $container);
  83. }
  84. }
  85. private function configureSecureRandom(array $config, ContainerBuilder $container)
  86. {
  87. if (isset($config['seed_provider'])) {
  88. $container
  89. ->getDefinition('security.util.secure_random')
  90. ->addMethodCall('setSeedProvider', array(new Reference($config['seed_provider'])))
  91. ;
  92. $container->setAlias('security.util.secure_random_seed_provider', $config['seed_provider']);
  93. } elseif (isset($config['connection'])) {
  94. $container
  95. ->getDefinition('security.util.secure_random')
  96. ->addMethodCall('setConnection', array(new Reference($this->getDoctrineConnectionId($config['connection'])), $config['table_name']))
  97. ;
  98. $container->setAlias('security.util.secure_random_connection', $this->getDoctrineConnectionId($config['connection']));
  99. $container->setParameter('security.util.secure_random_table', $config['table_name']);
  100. $container
  101. ->getDefinition('security.util.secure_random_schema_listener')
  102. ->addTag('doctrine.event_listener', array('connection' => $config['connection'], 'event' => 'postGenerateSchema', 'lazy' => true))
  103. ;
  104. $container
  105. ->getDefinition('security.util.secure_random_schema')
  106. ->replaceArgument(0, $config['table_name'])
  107. ;
  108. }
  109. }
  110. private function getDoctrineConnectionId($guess)
  111. {
  112. return "doctrine.dbal.{$guess}_connection";
  113. }
  114. }