Skip to content

Commit 5ee57d9

Browse files
Merge with existing config code block
1 parent 956837b commit 5ee57d9

File tree

1 file changed

+17
-49
lines changed

1 file changed

+17
-49
lines changed

security/access_control.rst

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Take the following ``access_control`` entries as an example:
5151
# ips can be comma-separated, which is especially useful when using env variables
5252
- { path: '^/admin', roles: ROLE_USER_IP, ips: '%env(TRUSTED_IPS)%' }
5353
- { path: '^/admin', roles: ROLE_USER_IP, ips: [127.0.0.1, ::1, '%env(TRUSTED_IPS)%'] }
54+
55+
# Request matchers can be used to define access control rules
56+
- { roles: ROLE_USER, request_matcher: App\Security\RequestMatcher\MyRequestMatcher }
5457
5558
.. code-block:: xml
5659
@@ -82,6 +85,9 @@ Take the following ``access_control`` entries as an example:
8285
<ip>::1</ip>
8386
<ip>%env(TRUSTED_IPS)%</ip>
8487
</rule>
88+
89+
<!-- Request matchers can be used to define access control rules -->
90+
<rule role="ROLE_USER" request-matcher="App\Security\RequestMatcher\MyRequestMatcher"/>
8591
</config>
8692
</srv:container>
8793
@@ -127,11 +133,21 @@ Take the following ``access_control`` entries as an example:
127133
->roles(['ROLE_USER_IP'])
128134
->ips(['127.0.0.1', '::1', '%env(TRUSTED_IPS)%'])
129135
;
136+
137+
// Request matchers can be used to define access control rules
138+
$security->accessControl()
139+
->roles(['ROLE_USER'])
140+
->requestMatcher('App\Security\RequestMatcher\MyRequestMatcher')
141+
;
130142
};
131143
132144
.. versionadded:: 5.2
133145

134146
Support for comma-separated IP addresses was introduced in Symfony 5.2.
147+
148+
.. versionadded:: 6.1
149+
150+
Support for access control rule definition based on a RequestMatcher was introduced in Symfony 6.1.
135151

136152
For each incoming request, Symfony will decide which ``access_control``
137153
to use based on the URI, the client's IP address, the incoming host name,
@@ -168,55 +184,7 @@ if ``ip``, ``port``, ``host`` or ``method`` are not specified for an entry, that
168184

169185
Matching the URI is done without ``$_GET`` parameters.
170186
:ref:`Deny access in PHP code <security-securing-controller>` if you want
171-
to disallow access based on ``$_GET`` parameter values.
172-
173-
.. versionadded:: 6.1
174-
175-
From verison 6.1 and up, if you have a custom request matcher, you can use the ``request_matcher`` option to specify the service reference to an access control item. Symfony will use this service as the request matcher for this item and won't take into account other matching options.
176-
177-
.. configuration-block::
178-
179-
.. code-block:: yaml
180-
181-
# config/packages/security.yaml
182-
security:
183-
# ...
184-
access_control:
185-
- { roles: ROLE_USER, request_matcher: App\Security\RequestMatcher\MyRequestMatcher }
186-
187-
.. code-block:: xml
188-
189-
<!-- config/packages/security.xml -->
190-
<?xml version="1.0" encoding="UTF-8" ?>
191-
<srv:container xmlns="http://symfony.com/schema/dic/security"
192-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
193-
xmlns:srv="http://symfony.com/schema/dic/services"
194-
xsi:schemaLocation="http://symfony.com/schema/dic/services
195-
https://symfony.com/schema/dic/services/services-1.0.xsd
196-
http://symfony.com/schema/dic/security
197-
https://symfony.com/schema/dic/security/security-1.0.xsd">
198-
199-
<config>
200-
<!-- ... -->
201-
<rule role="ROLE_USER" request-matcher="App\Security\RequestMatcher\MyRequestMatcher"/>
202-
</config>
203-
</srv:container>
204-
205-
.. code-block:: php
206-
207-
// config/packages/security.php
208-
use Symfony\Component\DependencyInjection\ContainerBuilder;
209-
use Symfony\Config\SecurityConfig;
210-
211-
return static function (ContainerBuilder $container, SecurityConfig $security) {
212-
// ...
213-
214-
$security->accessControl()
215-
->roles(['ROLE_USER'])
216-
->requestMatcher('App\Security\RequestMatcher\MyRequestMatcher')
217-
;
218-
};
219-
187+
to disallow access based on ``$_GET`` parameter values.
220188

221189
.. _security-access-control-enforcement-options:
222190

0 commit comments

Comments
 (0)