diff --git a/security.rst b/security.rst index d38c9cf731d..421cab187d3 100644 --- a/security.rst +++ b/security.rst @@ -2194,6 +2194,90 @@ Users with ``ROLE_SUPER_ADMIN``, will automatically have ``ROLE_ADMIN``, :doc:`security voter ` that looks for the user roles in the database. +You can also use the special ``*`` placeholder character to define hierarchy dynamically: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/security.yaml + security: + # ... + + role_hierarchy: + ROLE_*: ROLE_USER + ROLE_*_MODERATOR: ROLE_MODERATOR + + ROLE_BLOG_*: ROLE_BLOG_READER + ROLE_BLOG_MODERATOR: [ROLE_BLOG_DELETE_POST, ROLE_BLOG_LOCK_POST] + + ROLE_SHOP_*: ROLE_SHOP_USER + ROLE_SHOP_MODERATOR: [ROLE_SHOP_DELETE_ITEM, ROLE_SHOP_DELETE_REVIEW] + + .. code-block:: xml + + + + + + + + + ROLE_USER + ROLE_MODERATOR + + ROLE_BLOG_READER + ROLE_BLOG_DELETE_POST, ROLE_BLOG_LOCK_POST + + ROLE_SHOP_USER + ROLE_SHOP_DELETE_ITEM, ROLE_SHOP_DELETE_REVIEW + + + + .. code-block:: php + + // config/packages/security.php + use Symfony\Config\SecurityConfig; + + return static function (SecurityConfig $security): void { + // ... + + $security->roleHierarchy('ROLE_*', ['ROLE_USER']); + $security->roleHierarchy('ROLE_*_MODERATOR', ['ROLE_MODERATOR']); + + $security->roleHierarchy('ROLE_BLOG_*', ['ROLE_BLOG_READER']); + $security->roleHierarchy('ROLE_BLOG_MODERATOR', ['ROLE_BLOG_DELETE_POST', 'ROLE_BLOG_LOCK_POST']); + + $security->roleHierarchy('ROLE_SHOP_*', ['ROLE_SHOP_USER']); + $security->roleHierarchy('ROLE_SHOP_MODERATOR', ['ROLE_SHOP_DELETE_ITEM', 'ROLE_SHOP_DELETE_REVIEW']); + }; + +With this configuration, you can easily configure that: + + - Having a role grants ``ROLE_USER``. + - All moderators have ``ROLE_MODERATOR``. + - Anyone with the ``ROLE_BLOG_*`` can access the blog. + - Anyone with the ``ROLE_SHOP_*`` can access the shop. + +Even if a role is not explicitly defined in the hierarchy, if it is matched by a placeholder it will inherit the roles of this placeholder: + + - Users with the ``ROLE_BLOG_ADMIN`` will also have the ``ROLE_BLOG_READER`` + - Users with the ``ROLE_NEWS_MODERATOR`` will also have the ``ROLE_MODERATOR`` + +.. caution:: + + The ``*`` placeholder character can only be used after a ``_`` and before a ``_`` or the end of the role name. That means role names like ``ROLE_BLOG*`` and ``ROLE_*BLOG`` will not be considered as valid placeholders. + +.. versionadded:: 6.4 + + The placeholder syntax was introduced in Symfony 6.4. + .. _security-role-authorization: Add Code to Deny Access