annotations.rst 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. Annotations
  2. -----------
  3. @PreAuthorize
  4. ~~~~~~~~~~~~~
  5. This annotation lets you define an expression (see the expression language
  6. paragraph) which is executed prior to invoking a method:
  7. .. code-block :: php
  8. <?php
  9. use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
  10. class MyService
  11. {
  12. /** @PreAuthorize("hasRole('A') or (hasRole('B') and hasRole('C'))") */
  13. public function secureMethod()
  14. {
  15. // ...
  16. }
  17. }
  18. .. tip ::
  19. If you like to secure all actions of the controller with the same rule, you
  20. may also specify @PreAuthorize on the class itself. Caution though, this
  21. rule is only applied to the methods which are declared in the class.
  22. @Secure
  23. ~~~~~~~
  24. This annotation lets you define who is allowed to invoke a method:
  25. .. code-block :: php
  26. <?php
  27. use JMS\SecurityExtraBundle\Annotation\Secure;
  28. class MyService
  29. {
  30. /**
  31. * @Secure(roles="ROLE_USER, ROLE_FOO, ROLE_ADMIN")
  32. */
  33. public function secureMethod()
  34. {
  35. // ...
  36. }
  37. }
  38. @SecureParam
  39. ~~~~~~~~~~~~
  40. This annotation lets you define restrictions for parameters which are passed to
  41. the method. This is only useful if the parameters are domain objects:
  42. .. code-block :: php
  43. <?php
  44. use JMS\SecurityExtraBundle\Annotation\SecureParam;
  45. class MyService
  46. {
  47. /**
  48. * @SecureParam(name="comment", permissions="EDIT, DELETE")
  49. * @SecureParam(name="post", permissions="OWNER")
  50. */
  51. public function secureMethod($comment, $post)
  52. {
  53. // ...
  54. }
  55. }
  56. @SecureReturn
  57. ~~~~~~~~~~~~~
  58. This annotation lets you define restrictions for the value which is returned by
  59. the method. This is also only useful if the returned value is a domain object:
  60. .. code-block :: php
  61. <?php
  62. use JMS\SecurityExtraBundle\Annotation\SecureReturn;
  63. class MyService
  64. {
  65. /**
  66. * @SecureReturn(permissions="VIEW")
  67. */
  68. public function secureMethod()
  69. {
  70. // ...
  71. return $domainObject;
  72. }
  73. }
  74. @RunAs
  75. ~~~~~~
  76. This annotation lets you specifiy roles which are added only for the duration
  77. of the method invocation. These roles will not be taken into consideration
  78. for before, or after invocation access decisions.
  79. This is typically used to implement a two-tier service layer where you have
  80. public and private services, and private services are only to be invoked
  81. through a specific public service:
  82. .. code-block :: php
  83. <?php
  84. use JMS\SecurityExtraBundle\Annotation\Secure;
  85. use JMS\SecurityExtraBundle\Annotation\RunAs;
  86. class MyPrivateService
  87. {
  88. /**
  89. * @Secure(roles="ROLE_PRIVATE_SERVICE")
  90. */
  91. public function aMethodOnlyToBeInvokedThroughASpecificChannel()
  92. {
  93. // ...
  94. }
  95. }
  96. class MyPublicService
  97. {
  98. protected $myPrivateService;
  99. /**
  100. * @Secure(roles="ROLE_USER")
  101. * @RunAs(roles="ROLE_PRIVATE_SERVICE")
  102. */
  103. public function canBeInvokedFromOtherServices()
  104. {
  105. return $this->myPrivateService->aMethodOnlyToBeInvokedThroughASpecificChannel();
  106. }
  107. }
  108. @SatisfiesParentSecurityPolicy
  109. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  110. This must be defined on a method that overrides a method which has security metadata.
  111. It is there to ensure that you are aware the security of the overridden method cannot
  112. be enforced anymore, and that you must copy over all annotations if you want to keep
  113. them.