Menu Permissions
This guide demonstrates a Spring Boot application that implements menu-based access control using jCasbin. The approach shown here serves as a foundation for building menu permission middleware that can be adapted to other Casbin-supported languages like Go and Python.
1. Файли конфігурації
Configure role permissions and menu hierarchies in the policy.csv file. For a complete working example, see the jCasbin menu permission repository.
1.1 Огляд
The policy.csv file enables granular access control by defining role-based permissions for menu items, user-role assignments, and hierarchical menu structures. This configuration combines three elements: which roles can access which menu items, which users belong to which roles, and how menus relate to each other in the navigation hierarchy.
1.2 Визначення дозволів (Політики)
- Policy Rules: Each policy line starts with
pand defines whether a role (sub) has permission to perform an action (act) on a menu item (obj). The effect (eft) is eitherallowordeny.
Приклади:
p, ROLE_ROOT, SystemMenu, read, allowgrantsROLE_ROOTread access toSystemMenu.p, ROLE_ROOT, UserMenu, read, denydeniesROLE_ROOTread access toUserMenu.
1.3 Ролі та асоціації користувачів
- Role Inheritance: Lines starting with
gdefine user-role assignments and role inheritance chains. Users automatically inherit permissions from all their assigned roles.
Приклади:
g, user, ROLE_USERassigns the user namedusertoROLE_USER.g, ROLE_ADMIN, ROLE_USERmakesROLE_ADMINinherit all permissions fromROLE_USER.
1.4 Ієрархія пунктів меню
- Menu Relationships: Lines starting with
g2define parent-child relationships between menu items, establishing the menu structure.
Приклади:
g2, UserSubMenu_allow, UserMenumakesUserSubMenu_allowa child ofUserMenu.g2, (NULL), SystemMenumarksSystemMenuas a top-level menu with no parent.
1.5 Спадкування дозволів меню та стандартні правила
jCasbin applies specific inheritance rules when determining menu access based on parent-child relationships:
Спадкування дозволів батьківського меню:
When a parent menu has explicit allow permission, all child menus inherit allow by default unless explicitly set to deny. Granting access to a parent menu automatically grants access to its children.
Обробка батьківських меню без прямих налаштувань дозволів:
When a parent menu has no explicit permission but contains at least one child menu with explicit allow permission, the parent menu is implicitly granted allow status. This ensures users can reach the accessible child menus.