ClassMetadata.php 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Metadata;
  18. use JMS\SecurityExtraBundle\Exception\RuntimeException;
  19. use JMS\SecurityExtraBundle\Exception\InvalidArgumentException;
  20. use Metadata\MethodMetadata;
  21. use Metadata\MergeableInterface;
  22. use Metadata\MergeableClassMetadata;
  23. /**
  24. * Contains class metadata information
  25. *
  26. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27. */
  28. class ClassMetadata extends MergeableClassMetadata
  29. {
  30. public function addMethodMetadata(MethodMetadata $metadata)
  31. {
  32. if ($this->reflection->isFinal()) {
  33. throw new RuntimeException(sprintf('Class "%s" is declared final, and cannot be secured.', $reflection->name));
  34. }
  35. if ($metadata->reflection->isStatic()) {
  36. throw new RuntimeException(sprintf('Method "%s::%s" is declared static and cannot be secured.', $metadata->reflection->class, $metadata->reflection->name));
  37. }
  38. if ($metadata->reflection->isFinal()) {
  39. throw new RuntimeException(sprintf('Method "%s::%s" is declared final and cannot be secured.', $metadata->reflection->class, $metadata->reflection->name));
  40. }
  41. parent::addMethodMetadata($metadata);
  42. }
  43. public function merge(MergeableInterface $metadata)
  44. {
  45. if (!$metadata instanceof ClassMetadata) {
  46. throw new InvalidArgumentException('$metadata must be an instance of ClassMetadata.');
  47. }
  48. foreach ($this->methodMetadata as $name => $methodMetadata) {
  49. // check if metadata was declared on an interface
  50. if (!$metadata->reflection->hasMethod($name)) {
  51. continue;
  52. }
  53. if ($metadata->reflection->getMethod($name)->getDeclaringClass()->name
  54. !== $methodMetadata->class) {
  55. if (!isset($metadata->methodMetadata[$name])) {
  56. if ($methodMetadata->reflection->isAbstract()) {
  57. continue;
  58. }
  59. throw new RuntimeException(sprintf(
  60. 'You have overridden a secured method "%s::%s" in "%s". '
  61. .'Please copy over the applicable security metadata, and '
  62. .'also add @SatisfiesParentSecurityPolicy.',
  63. $methodMetadata->reflection->class,
  64. $name,
  65. $metadata->reflection->name
  66. ));
  67. }
  68. if (!$metadata->methodMetadata[$name]->satisfiesParentSecurityPolicy) {
  69. throw new RuntimeException(sprintf('Unresolved security metadata conflict for method "%s::%s" in "%s". Please copy the respective annotations, and add @SatisfiesParentSecurityPolicy to the child method.', $metadata->reflection->name, $name, $methodMetadata->reflection->getDeclaringClass()->getFilename()));
  70. }
  71. }
  72. }
  73. parent::merge($metadata);
  74. }
  75. public function isProxyRequired()
  76. {
  77. return !empty($this->methodMetadata);
  78. }
  79. }