MethodMetadata.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Metadata;
  18. use Metadata\MethodMetadata as BaseMethodMetadata;
  19. /**
  20. * Contains method metadata information
  21. *
  22. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23. */
  24. class MethodMetadata extends BaseMethodMetadata
  25. {
  26. public $roles = array();
  27. public $paramPermissions = array();
  28. public $returnPermissions = array();
  29. public $runAsRoles = array();
  30. public $satisfiesParentSecurityPolicy = false;
  31. /**
  32. * Adds a parameter restriction
  33. *
  34. * @param integer $index 0-based
  35. * @param array $permissions
  36. */
  37. public function addParamPermissions($index, array $permissions)
  38. {
  39. $this->paramPermissions[$index] = $permissions;
  40. }
  41. public function isDeclaredOnInterface()
  42. {
  43. foreach ($this->reflection->getDeclaringClass()->getInterfaces() as $interface) {
  44. if ($interface->hasMethod($this->name)) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. /**
  51. * This allows to merge in metadata from an interface
  52. *
  53. * @param MethodMetadata $method
  54. * @return void
  55. */
  56. public function merge(MethodMetadata $method)
  57. {
  58. if (!$this->roles) {
  59. $this->roles = $method->roles;
  60. }
  61. if (!$this->returnPermissions) {
  62. $this->returnPermissions = $method->returnPermissions;
  63. }
  64. if (!$this->runAsRoles) {
  65. $this->runAsRoles = $method->runAsRoles;
  66. }
  67. foreach ($method->paramPermissions as $index => $permissions) {
  68. if (!isset($this->paramPermissions[$index])) {
  69. $this->paramPermissions[$index] = $permissions;
  70. }
  71. }
  72. }
  73. public function getAsArray()
  74. {
  75. return array(
  76. 'roles' => $this->roles,
  77. 'run_as_roles' => $this->runAsRoles,
  78. 'param_permissions' => $this->paramPermissions,
  79. 'return_permissions' => $this->returnPermissions,
  80. );
  81. }
  82. }