AccessControlConfiguration.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Config\Definition\ConfigurationInterface;
  19. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  20. /**
  21. * Enhances the access_control section configuration.
  22. *
  23. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  24. */
  25. class AccessControlConfiguration implements ConfigurationInterface
  26. {
  27. /**
  28. * Generates the configuration tree builder.
  29. *
  30. * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
  31. */
  32. public function getConfigTreeBuilder()
  33. {
  34. $tb = new TreeBuilder();
  35. $rootNode = $tb->root('security');
  36. $rootNode
  37. ->ignoreExtraKeys()
  38. ->fixXmlConfig('rule', 'access_control')
  39. ->children()
  40. ->arrayNode('access_control')
  41. ->cannotBeOverwritten()
  42. ->prototype('array')
  43. ->fixXmlConfig('role')
  44. ->validate()
  45. ->always(function($v) {
  46. if (!empty($v['roles']) && isset($v['access'])) {
  47. throw new \Exception('"roles", and "access" cannot be set at the same time.');
  48. }
  49. if (empty($v['roles'])) {
  50. unset($v['roles']);
  51. }
  52. return $v;
  53. })
  54. ->end()
  55. ->children()
  56. ->scalarNode('requires_channel')->defaultNull()->end()
  57. ->scalarNode('path')->defaultNull()->end()
  58. ->scalarNode('host')->defaultNull()->end()
  59. ->scalarNode('ip')->defaultNull()->end()
  60. ->arrayNode('methods')
  61. ->beforeNormalization()->ifString()->then(function($v) { return preg_split('/\s*,\s*/', $v); })->end()
  62. ->prototype('scalar')->end()
  63. ->end()
  64. ->arrayNode('roles')
  65. ->beforeNormalization()->ifString()->then(function($v) { return preg_split('/\s*,\s*/', $v); })->end()
  66. ->prototype('scalar')->end()
  67. ->end()
  68. ->scalarNode('access')->end()
  69. ->end()
  70. ->end()
  71. ->end()
  72. ->end()
  73. ;
  74. return $tb;
  75. }
  76. }