|
0
|
1 |
======== |
|
|
2 |
Overview |
|
|
3 |
======== |
|
|
4 |
|
|
|
5 |
This bundle allows you to secure method invocations on your service layer with |
|
|
6 |
annotations. |
|
|
7 |
|
|
|
8 |
Generally, you can secure all public, or protected methods which are non-static, |
|
|
9 |
and non-final. Private methods cannot be secured this way. |
|
|
10 |
|
|
|
11 |
Annotations can also be declared on abstract methods, parent classes, or |
|
|
12 |
interfaces. |
|
|
13 |
|
|
|
14 |
How does it work? |
|
|
15 |
----------------- |
|
|
16 |
The bundle will first collect all available security metadata for your services |
|
|
17 |
from annotations. The metadata will then be used to build proxy classes which |
|
|
18 |
have the requested security checks built-in. These proxy classes will replace |
|
|
19 |
your original service classes. All of that is done automatically for you, you |
|
|
20 |
don't need to manually clear any cache if you make changes to the metadata. |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
Performance |
|
|
24 |
----------- |
|
|
25 |
While there will be virtually no performance difference in your production |
|
|
26 |
environment, the performance in the development environment significantly |
|
|
27 |
depends on your configuration (see the configuration section). |
|
|
28 |
|
|
|
29 |
Generally, you will find that when you change the files of a secure service |
|
|
30 |
the first page load after changing the file will increase. This is because |
|
|
31 |
the cache for this service will need to be rebuilt, and a proxy class possibly |
|
|
32 |
needs to be generated. Subsequent page loads will be very fast. |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
Installation |
|
|
36 |
------------ |
|
|
37 |
Checkout a copy of the code:: |
|
|
38 |
|
|
|
39 |
git submodule add https://github.com/schmittjoh/SecurityExtraBundle.git vendor/bundles/JMS/SecurityExtraBundle |
|
|
40 |
|
|
|
41 |
Then register the bundle with your kernel:: |
|
|
42 |
|
|
|
43 |
// in AppKernel::registerBundles() |
|
|
44 |
$bundles = array( |
|
|
45 |
// ... |
|
|
46 |
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(), |
|
|
47 |
// ... |
|
|
48 |
); |
|
|
49 |
|
|
|
50 |
This bundle also requires the Metadata library:: |
|
|
51 |
|
|
|
52 |
git submodule add https://github.com/schmittjoh/metadata.git vendor/metadata |
|
|
53 |
|
|
|
54 |
Make sure that you also register the namespaces with the autoloader:: |
|
|
55 |
|
|
|
56 |
// app/autoload.php |
|
|
57 |
$loader->registerNamespaces(array( |
|
|
58 |
// ... |
|
|
59 |
'JMS' => __DIR__.'/../vendor/bundles', |
|
|
60 |
'Metadata' => __DIR__.'/../vendor/metadata/src', |
|
|
61 |
// ... |
|
|
62 |
)); |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
Configuration |
|
|
66 |
------------- |
|
|
67 |
|
|
|
68 |
Below, you find the default configuration:: |
|
|
69 |
|
|
|
70 |
# app/config/config.yml |
|
|
71 |
jms_security_extra: |
|
|
72 |
# If you set-up your controllers as services, you must set this to false; |
|
|
73 |
# otherwise your security checks will be performed twice. |
|
|
74 |
secure_controllers: true |
|
|
75 |
|
|
|
76 |
# Whether you want to secure all services (true), or only secure specific |
|
|
77 |
# services (false); see also below |
|
|
78 |
secure_all_services: false |
|
|
79 |
|
|
|
80 |
# Enabling this setting will add an additional special attribute "IS_IDDQD". |
|
|
81 |
# Anybody with this attribute will effectively bypass all security checks. |
|
|
82 |
enable_iddqd_attribute: false |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
By default, security checks are not enabled for any service. You can turn on |
|
|
86 |
security for your services either by securing all services as shown above, or |
|
|
87 |
only for specific services by adding a tag to these services:: |
|
|
88 |
|
|
|
89 |
<service id="foo" class="Bar"> |
|
|
90 |
<tag name="security.secure_service"/> |
|
|
91 |
</service> |
|
|
92 |
|
|
|
93 |
If you enable security for all services, be aware that the first page load will |
|
|
94 |
be very slow depending on how many services you have defined. |
|
|
95 |
|
|
|
96 |
Annotations |
|
|
97 |
----------- |
|
|
98 |
|
|
|
99 |
@Secure |
|
|
100 |
~~~~~~~ |
|
|
101 |
This annotation lets you define who is allowed to invoke a method:: |
|
|
102 |
|
|
|
103 |
<?php |
|
|
104 |
|
|
|
105 |
use JMS\SecurityExtraBundle\Annotation\Secure; |
|
|
106 |
|
|
|
107 |
class MyService |
|
|
108 |
{ |
|
|
109 |
/** |
|
|
110 |
* @Secure(roles="ROLE_USER, ROLE_FOO, ROLE_ADMIN") |
|
|
111 |
*/ |
|
|
112 |
public function secureMethod() |
|
|
113 |
{ |
|
|
114 |
// ... |
|
|
115 |
} |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
@SecureParam |
|
|
119 |
~~~~~~~~~~~~ |
|
|
120 |
This annotation lets you define restrictions for parameters which are passed to |
|
|
121 |
the method. This is only useful if the parameters are domain objects:: |
|
|
122 |
|
|
|
123 |
<?php |
|
|
124 |
|
|
|
125 |
use JMS\SecurityExtraBundle\Annotation\SecureParam; |
|
|
126 |
|
|
|
127 |
class MyService |
|
|
128 |
{ |
|
|
129 |
/** |
|
|
130 |
* @SecureParam(name="comment", permissions="EDIT, DELETE") |
|
|
131 |
* @SecureParam(name="post", permissions="OWNER") |
|
|
132 |
*/ |
|
|
133 |
public function secureMethod($comment, $post) |
|
|
134 |
{ |
|
|
135 |
// ... |
|
|
136 |
} |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
@SecureReturn |
|
|
140 |
~~~~~~~~~~~~~ |
|
|
141 |
This annotation lets you define restrictions for the value which is returned by |
|
|
142 |
the method. This is also only useful if the returned value is a domain object:: |
|
|
143 |
|
|
|
144 |
<?php |
|
|
145 |
|
|
|
146 |
use JMS\SecurityExtraBundle\Annotation\SecureReturn; |
|
|
147 |
|
|
|
148 |
class MyService |
|
|
149 |
{ |
|
|
150 |
/** |
|
|
151 |
* @SecureReturn(permissions="VIEW") |
|
|
152 |
*/ |
|
|
153 |
public function secureMethod() |
|
|
154 |
{ |
|
|
155 |
// ... |
|
|
156 |
|
|
|
157 |
return $domainObject; |
|
|
158 |
} |
|
|
159 |
} |
|
|
160 |
|
|
|
161 |
@RunAs |
|
|
162 |
~~~~~~ |
|
|
163 |
This annotation lets you specifiy roles which are added only for the duration |
|
|
164 |
of the method invocation. These roles will not be taken into consideration |
|
|
165 |
for before, or after invocation access decisions. |
|
|
166 |
|
|
|
167 |
This is typically used to implement a two-tier service layer where you have |
|
|
168 |
public and private services, and private services are only to be invoked |
|
|
169 |
through a specific public service:: |
|
|
170 |
|
|
|
171 |
<?php |
|
|
172 |
|
|
|
173 |
use JMS\SecurityExtraBundle\Annotation\Secure; |
|
|
174 |
use JMS\SecurityExtraBundle\Annotation\RunAs; |
|
|
175 |
|
|
|
176 |
class MyPrivateService |
|
|
177 |
{ |
|
|
178 |
/** |
|
|
179 |
* @Secure(roles="ROLE_PRIVATE_SERVICE") |
|
|
180 |
*/ |
|
|
181 |
public function aMethodOnlyToBeInvokedThroughASpecificChannel() |
|
|
182 |
{ |
|
|
183 |
// ... |
|
|
184 |
} |
|
|
185 |
} |
|
|
186 |
|
|
|
187 |
class MyPublicService |
|
|
188 |
{ |
|
|
189 |
protected $myPrivateService; |
|
|
190 |
|
|
|
191 |
/** |
|
|
192 |
* @Secure(roles="ROLE_USER") |
|
|
193 |
* @RunAs(roles="ROLE_PRIVATE_SERVICE") |
|
|
194 |
*/ |
|
|
195 |
public function canBeInvokedFromOtherServices() |
|
|
196 |
{ |
|
|
197 |
return $this->myPrivateService->aMethodOnlyToBeInvokedThroughASpecificChannel(); |
|
|
198 |
} |
|
|
199 |
} |
|
|
200 |
|
|
|
201 |
@SatisfiesParentSecurityPolicy |
|
|
202 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
|
203 |
This must be defined on a method that overrides a method which has security metadata. |
|
|
204 |
It is there to ensure that you are aware the security of the overridden method cannot |
|
|
205 |
be enforced anymore, and that you must copy over all annotations if you want to keep |
|
|
206 |
them. |