ServiceAnalyzer.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /*
  3. * Copyright 2010 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\Analysis;
  18. use Doctrine\Common\Annotations\Reader;
  19. use JMS\SecurityExtraBundle\Metadata\Driver\AnnotationDriver;
  20. use JMS\SecurityExtraBundle\Metadata\MethodMetadata;
  21. use JMS\SecurityExtraBundle\Metadata\ClassMetadata;
  22. use JMS\SecurityExtraBundle\Metadata\ServiceMetadata;
  23. use Metadata\Driver\DriverChain;
  24. use \ReflectionClass;
  25. /**
  26. * Analyzes a service class including parent classes. The gathered information
  27. * is then used to built a proxy class if necessary.
  28. *
  29. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  30. */
  31. class ServiceAnalyzer
  32. {
  33. private $reflection;
  34. private $files;
  35. private $driver;
  36. private $pdepend;
  37. private $analyzed;
  38. private $hierarchy;
  39. private $metadata;
  40. public function __construct($class, Reader $reader)
  41. {
  42. $this->reflection = new ReflectionClass($class);
  43. $this->files = array();
  44. $this->hierarchy = array();
  45. $this->driver = new DriverChain(array(
  46. new AnnotationDriver($reader),
  47. ));
  48. $this->analyzed = false;
  49. }
  50. public function analyze()
  51. {
  52. if (true === $this->analyzed) {
  53. return;
  54. }
  55. $this->collectFiles();
  56. $this->buildClassHierarchy();
  57. $this->collectServiceMetadata();
  58. if ($this->metadata->isProxyRequired()) {
  59. $this->normalizeMetadata();
  60. $this->analyzeControlFlow();
  61. }
  62. $this->analyzed = true;
  63. }
  64. public function getFiles()
  65. {
  66. if (!$this->analyzed) {
  67. throw new \LogicException('Data not yet available, run analyze() first.');
  68. }
  69. return $this->files;
  70. }
  71. public function getMetadata()
  72. {
  73. if (!$this->analyzed) {
  74. throw new \LogicException('Data not yet available, run analyze() first.');
  75. }
  76. return $this->metadata;
  77. }
  78. private function buildClassHierarchy()
  79. {
  80. $hierarchy = array();
  81. $class = $this->reflection;
  82. // add classes
  83. while (false !== $class) {
  84. $hierarchy[] = $class;
  85. $class = $class->getParentClass();
  86. }
  87. // add interfaces
  88. $addedInterfaces = array();
  89. $newHierarchy = array();
  90. foreach (array_reverse($hierarchy) as $class) {
  91. foreach ($class->getInterfaces() as $interface) {
  92. if (isset($addedInterfaces[$interface->getName()])) {
  93. continue;
  94. }
  95. $addedInterfaces[$interface->getName()] = true;
  96. $newHierarchy[] = $interface;
  97. }
  98. $newHierarchy[] = $class;
  99. }
  100. $this->hierarchy = array_reverse($newHierarchy);
  101. }
  102. private function collectFiles()
  103. {
  104. $this->files[] = $this->reflection->getFileName();
  105. foreach ($this->reflection->getInterfaces() as $interface) {
  106. if (false !== $filename = $interface->getFileName()) {
  107. $this->files[] = $filename;
  108. }
  109. }
  110. $parent = $this->reflection;
  111. while (false !== $parent = $parent->getParentClass()) {
  112. if (false !== $filename = $parent->getFileName()) {
  113. $this->files[] = $filename;
  114. }
  115. }
  116. }
  117. private function normalizeMetadata()
  118. {
  119. $secureMethods = array();
  120. foreach ($this->metadata->classMetadata as $class) {
  121. if ($class->reflection->isFinal()) {
  122. throw new \RuntimeException('Final classes cannot be secured.');
  123. }
  124. foreach ($class->methodMetadata as $name => $method) {
  125. if ($method->reflection->isStatic() || $method->reflection->isFinal()) {
  126. throw new \RuntimeException('Annotations cannot be defined on final, or static methods.');
  127. }
  128. if (!isset($secureMethods[$name])) {
  129. $this->metadata->addMethodMetadata($method);
  130. $secureMethods[$name] = $method;
  131. } else if ($method->reflection->isAbstract()) {
  132. $secureMethods[$name]->merge($method);
  133. } else if (false === $secureMethods[$name]->satisfiesParentSecurityPolicy
  134. && $method->reflection->getDeclaringClass()->getName() !== $secureMethods[$name]->reflection->getDeclaringClass()->getName()) {
  135. 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.', $secureMethods[$name]->reflection->getDeclaringClass()->getName(), $name, $secureMethods[$name]->reflection->getDeclaringClass()->getFileName()));
  136. }
  137. }
  138. }
  139. foreach ($secureMethods as $name => $method) {
  140. if ($method->reflection->isAbstract()) {
  141. $previous = null;
  142. $abstractClass = $method->reflection->getDeclaringClass()->getName();
  143. foreach ($this->hierarchy as $refClass) {
  144. if ($abstractClass === $fqcn = $refClass->getName()) {
  145. $methodMetadata = new MethodMetadata($previous->getName(), $name);
  146. $methodMetadata->merge($method);
  147. $this->metadata->addMethodMetadata($methodMetadata);
  148. continue 2;
  149. }
  150. if (!$refClass->isInterface() && $this->hasMethod($refClass, $name)) {
  151. $previous = $refClass;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * We only perform a very lightweight control flow analysis. If we stumble upon
  159. * something suspicous, we will simply break, and require additional metadata
  160. * to resolve the situation.
  161. *
  162. * @throws \RuntimeException
  163. * @return void
  164. */
  165. private function analyzeControlFlow()
  166. {
  167. $secureMethods = $this->metadata->methodMetadata;
  168. $rootClass = $this->hierarchy[0];
  169. while (true) {
  170. foreach ($rootClass->getMethods() as $method) {
  171. if (!$this->hasMethod($rootClass, $method->getName())) {
  172. continue;
  173. }
  174. if (!isset($secureMethods[$name = $method->getName()])) {
  175. continue;
  176. }
  177. if ($secureMethods[$name]->reflection->getDeclaringClass()->getName() !== $rootClass->getName()) {
  178. throw new \RuntimeException(sprintf(
  179. 'You have overridden a secured method "%s::%s" in "%s". '
  180. .'Please copy over the applicable security metadata, and '
  181. .'also add @SatisfiesParentSecurityPolicy.',
  182. $secureMethods[$name]->reflection->getDeclaringClass()->getName(),
  183. $name,
  184. $rootClass->getName()
  185. ));
  186. }
  187. unset($secureMethods[$method->getName()]);
  188. }
  189. if (null === $rootClass = $rootClass->getParentClass()) {
  190. break;
  191. }
  192. if (0 === count($secureMethods)) {
  193. break;
  194. }
  195. }
  196. }
  197. private function collectServiceMetadata()
  198. {
  199. $this->metadata = new ServiceMetadata();
  200. $classMetadata = null;
  201. foreach ($this->hierarchy as $reflectionClass) {
  202. if (null === $classMetadata) {
  203. $classMetadata = new ClassMetadata($reflectionClass->getName());
  204. }
  205. if (null !== $aMetadata = $this->driver->loadMetadataForClass($reflectionClass)) {
  206. if ($reflectionClass->isInterface()) {
  207. $classMetadata->merge($aMetadata);
  208. } else {
  209. $this->metadata->addClassMetadata($classMetadata);
  210. $classMetadata = $aMetadata;
  211. }
  212. }
  213. }
  214. $this->metadata->addClassMetadata($classMetadata);
  215. }
  216. private function hasMethod(\ReflectionClass $class, $name)
  217. {
  218. if (!$class->hasMethod($name)) {
  219. return false;
  220. }
  221. return $class->getName() === $class->getMethod($name)->getDeclaringClass()->getName();
  222. }
  223. }