Menu Permissions
This page shows a Spring Boot example that uses jCasbin for menu-based access control. The same pattern (policy + role assignments + menu hierarchy) can be applied in other languages supported by Casbin.
1. Configuration
Define role–menu permissions, user–role assignments, and menu hierarchy in policy.csv. Full example: jCasbin menu permission.
1.1 نظرة عامة
The policy file defines: (1) which roles can access which menu items, (2) which users have which roles, and (3) parent–child relationships between menu items. Together this gives role-based, hierarchical menu visibility.
1.2 Permission rules (p)
Lines starting with p define whether a role (sub) may perform an action (act) on a menu item (obj). The effect (eft) is allow or deny.
أمثلة:
p, ROLE_ROOT, SystemMenu, read, allowgrantsROLE_ROOTread access toSystemMenu.p, ROLE_ROOT, UserMenu, read, denydeniesROLE_ROOTread access toUserMenu.
1.3 User–role assignments (g)
Lines starting with g assign users to roles and define role inheritance. Users get the union of permissions from all their roles.
أمثلة:
g, user, ROLE_USERassigns the user namedusertoROLE_USER.g, ROLE_ADMIN, ROLE_USERmakesROLE_ADMINinherit all permissions fromROLE_USER.
1.4 Menu hierarchy (g2)
Lines starting with g2 define parent–child menu structure.
أمثلة:
g2, UserSubMenu_allow, UserMenumakesUserSubMenu_allowa child ofUserMenu.g2, (NULL), SystemMenumarksSystemMenuas a top-level menu with no parent.
1.5 Menu permission inheritance
- If a parent menu has explicit
allow, its children inheritallowunless a child has explicitdeny. So allowing a parent allows its children. - If a parent has no explicit permission but at least one child has
allow, the parent is treated asallowso users can reach that child.