MethodMetadata.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 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 serialize()
  74. {
  75. return serialize(array(
  76. parent::serialize(),
  77. $this->roles, $this->paramPermissions, $this->returnPermissions,
  78. $this->runAsRoles, $this->satisfiesParentSecurityPolicy,
  79. ));
  80. }
  81. public function unserialize($str)
  82. {
  83. list($parentStr,
  84. $this->roles, $this->paramPermissions, $this->returnPermissions,
  85. $this->runAsRoles, $this->satisfiesParentSecurityPolicy
  86. ) = unserialize($str);
  87. parent::unserialize($parentStr);
  88. }
  89. }