Skip to content

Allow specifying attributes for RequestMatcher #17388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions security/access_control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ options are used for matching:
* ``host``: a regular expression
* ``methods``: one or many HTTP methods
* ``request_matcher``: a service implementing ``RequestMatcherInterface``
* ``attributes``: an array, which can be used to specify one or more :ref:`request attributes <accessing-request-data>` that must match exactly
* ``route``: a route name

.. versionadded:: 6.1

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

.. versionadded:: 6.2

The ``route`` and ``attributes`` options were introduced in Symfony 6.2.

Take the following ``access_control`` entries as an example:

.. configuration-block::
Expand All @@ -60,6 +66,10 @@ Take the following ``access_control`` entries as an example:
# for custom matching needs, use a request matcher service
- { roles: ROLE_USER, request_matcher: App\Security\RequestMatcher\MyRequestMatcher }

# require ROLE_ADMIN for 'admin' route. You can use the shortcut "route: "xxx", instead of "attributes": ["_route": "xxx"]
- { attributes: {'_route': 'admin'}, roles: ROLE_ADMIN }
- { route: 'admin', roles: ROLE_ADMIN }

.. code-block:: xml

<!-- config/packages/security.xml -->
Expand Down Expand Up @@ -93,6 +103,12 @@ Take the following ``access_control`` entries as an example:

<!-- for custom matching needs, use a request matcher service -->
<rule role="ROLE_USER" request-matcher="App\Security\RequestMatcher\MyRequestMatcher"/>

<!-- require ROLE_ADMIN for 'admin' route. You can use the shortcut route="xxx" -->
<rule role="ROLE_ADMIN">
<attribute key="_route">admin</attribute>
</rule>
<rule route="admin" role="ROLE_ADMIN"/>
</config>
</srv:container>

Expand Down Expand Up @@ -144,6 +160,17 @@ Take the following ``access_control`` entries as an example:
->roles(['ROLE_USER'])
->requestMatcher('App\Security\RequestMatcher\MyRequestMatcher')
;

// require ROLE_ADMIN for 'admin' route. You can use the shortcut route('xxx') mehtod,
// instead of attributes(['_route' => 'xxx']) method
$security->accessControl()
->roles(['ROLE_ADMIN'])
->attributes(['_route' => 'admin'])
;
$security->accessControl()
->roles(['ROLE_ADMIN'])
->route('admin')
;
};

For each incoming request, Symfony will decide which ``access_control``
Expand Down