Skip to content

Commit 0b08f81

Browse files
mdoutreluingneOskarStark
authored andcommitted
Allow specifying attributes for RequestMatcher
1 parent 259530a commit 0b08f81

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

security/access_control.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ options are used for matching:
3030
* ``host``: a regular expression
3131
* ``methods``: one or many HTTP methods
3232
* ``request_matcher``: a service implementing ``RequestMatcherInterface``
33+
* ``attributes``: an array, which can be used to specify one or more :ref:`request attributes <accessing-request-data>` that must match exactly
34+
* ``route``: a route name
3335

3436
.. versionadded:: 6.1
3537

3638
The ``request_matcher`` option was introduced in Symfony 6.1.
3739

40+
.. versionadded:: 6.2
41+
42+
The ``route`` and ``attributes`` options were introduced in Symfony 6.2.
43+
3844
Take the following ``access_control`` entries as an example:
3945

4046
.. configuration-block::
@@ -60,6 +66,10 @@ Take the following ``access_control`` entries as an example:
6066
# for custom matching needs, use a request matcher service
6167
- { roles: ROLE_USER, request_matcher: App\Security\RequestMatcher\MyRequestMatcher }
6268
69+
# require ROLE_ADMIN for 'admin' route. You can use the shortcut "route: "xxx", instead of "attributes": ["_route": "xxx"]
70+
- { attributes: {'_route': 'admin'}, roles: ROLE_ADMIN }
71+
- { route: 'admin', roles: ROLE_ADMIN }
72+
6373
.. code-block:: xml
6474
6575
<!-- config/packages/security.xml -->
@@ -93,6 +103,12 @@ Take the following ``access_control`` entries as an example:
93103
94104
<!-- for custom matching needs, use a request matcher service -->
95105
<rule role="ROLE_USER" request-matcher="App\Security\RequestMatcher\MyRequestMatcher"/>
106+
107+
<!-- require ROLE_ADMIN for 'admin' route. You can use the shortcut route="xxx" -->
108+
<rule role="ROLE_ADMIN">
109+
<attribute key="_route">admin</attribute>
110+
</rule>
111+
<rule route="admin" role="ROLE_ADMIN"/>
96112
</config>
97113
</srv:container>
98114
@@ -144,6 +160,17 @@ Take the following ``access_control`` entries as an example:
144160
->roles(['ROLE_USER'])
145161
->requestMatcher('App\Security\RequestMatcher\MyRequestMatcher')
146162
;
163+
164+
// require ROLE_ADMIN for 'admin' route. You can use the shortcut route('xxx') mehtod,
165+
// instead of attributes(['_route' => 'xxx']) method
166+
$security->accessControl()
167+
->roles(['ROLE_ADMIN'])
168+
->attributes(['_route' => 'admin'])
169+
;
170+
$security->accessControl()
171+
->roles(['ROLE_ADMIN'])
172+
->route('admin')
173+
;
147174
};
148175
149176
For each incoming request, Symfony will decide which ``access_control``

0 commit comments

Comments
 (0)