SecurityExtension.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\HttpKernel\DependencyInjection\Extension;
  19. use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension as BaseSecurityExtension;
  20. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  21. use Symfony\Component\DependencyInjection\ContainerBuilder;
  22. use Symfony\Component\DependencyInjection\Reference;
  23. /**
  24. * Enhances the access_control section of the SecurityBundle.
  25. *
  26. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27. */
  28. class SecurityExtension extends Extension
  29. {
  30. private $extension;
  31. public function __construct(BaseSecurityExtension $extension)
  32. {
  33. $this->extension = $extension;
  34. }
  35. public function getAlias()
  36. {
  37. return $this->extension->getAlias();
  38. }
  39. public function getNamespace()
  40. {
  41. return $this->extension->getNamespace();
  42. }
  43. public function getXsdValidationBasePath()
  44. {
  45. return $this->extension->getXsdValidationBasePath();
  46. }
  47. public function getClassesToCompile()
  48. {
  49. return array_merge(parent::getClassesToCompile(), $this->extension->getClassesToCompile());
  50. }
  51. public function load(array $configs, ContainerBuilder $container)
  52. {
  53. $parentConfigs = array();
  54. foreach ($configs as $config) {
  55. if (isset($config['rule'])) {
  56. unset($config['rule']);
  57. }
  58. if (isset($config['access_control'])) {
  59. unset($config['access_control']);
  60. }
  61. $parentConfigs[] = $config;
  62. }
  63. $this->extension->load($parentConfigs, $container);
  64. $config = $this->processConfiguration(new AccessControlConfiguration(), $configs);
  65. $this->createAuthorization($config, $container);
  66. }
  67. public function __call($method, array $args)
  68. {
  69. return call_user_func_array(array($this->extension, $method), $args);
  70. }
  71. private function createAuthorization($config, ContainerBuilder $container)
  72. {
  73. if (!$config['access_control']) {
  74. return;
  75. }
  76. $this->addClassesToCompile(array(
  77. 'Symfony\\Component\\Security\\Http\\AccessMap',
  78. ));
  79. foreach ($config['access_control'] as $access) {
  80. $matcher = $this->invokeParent('createRequestMatcher', array(
  81. $container,
  82. $access['path'],
  83. $access['host'],
  84. count($access['methods']) === 0 ? null : $access['methods'],
  85. $access['ip']
  86. ));
  87. if (isset($access['roles'])) {
  88. $attributes = $access['roles'];
  89. } else {
  90. $def = new DefinitionDecorator('security.expressions.expression');
  91. $def->addArgument($access['access']);
  92. $container->setDefinition($exprId = 'security.expressions.expression.'.sha1($access['access']), $def);
  93. $attributes = array(new Reference($exprId));
  94. }
  95. $container->getDefinition('security.access_map')
  96. ->addMethodCall('add', array($matcher, $attributes, $access['requires_channel']));
  97. }
  98. }
  99. private function invokeParent($method, array $args = array())
  100. {
  101. $ref = new \ReflectionMethod($this->extension, $method);
  102. $ref->setAccessible(true);
  103. return $ref->invokeArgs($this->extension, $args);
  104. }
  105. }