メインコンテンツにスキップ

Menu Permissions

We begin by introducing a Spring Boot example featuring a menu system. This example leverages jCasbin to manage menu permissions. Ultimately, it aims to abstract a middleware, specifically for menu permissions, which could be extended to other languages supported by Casbin, such as Go and Python.

1. Configuration Files

You need to set up role and permission management in the policy.csv file, along with the parent-child relationships between menu items. For more details, please refer to this GitHub repo.

1.1 Overview

Using policy.csv, you can flexibly configure role permissions and menu structures for fine-grained access control. This configuration file defines access permissions for different roles on various menu items, associations between users and roles, and the hierarchical relationships between menu items.

1.2 Permission Definitions (Policies)

  • Policy Rules: Policies are defined with a p prefix, specifying roles (sub) and their permissions (act) on menu items (obj), along with the rule's effect (eft), where allow indicates permission is granted, and deny indicates it is denied.

Examples:

  • p, ROLE_ROOT, SystemMenu, read, allow means the ROLE_ROOT role has read access to the SystemMenu menu item.
  • p, ROLE_ROOT, UserMenu, read, deny means the ROLE_ROOT role is denied read access to the UserMenu menu item.

1.3 Roles and User Associations

  • Role Inheritance: User-role relationships and role hierarchies are defined with a g prefix. This allows users to inherit permissions from one or multiple roles.

Examples:

  • g, user, ROLE_USER means the user user is assigned the ROLE_USER role.
  • g, ROLE_ADMIN, ROLE_USER means ROLE_ADMIN inherits permissions from ROLE_USER.

1.4 Menu Item Hierarchy

  • Menu Relationships: Parent-child relationships between menu items are defined with a g2 prefix, aiding in the construction of a menu's structure.

Examples:

  • g2, UserSubMenu_allow, UserMenu indicates UserSubMenu_allow is a submenu of UserMenu.
  • g2, (NULL), SystemMenu indicates SystemMenu has no submenu item, meaning it is a top-level menu item.

1.5 Menu Permission Inheritance and Default Rules

When managing menu permissions with jCasbin, the permission relationship between parent and child menus follows specific inheritance rules, with two important default rules:

Inheritance of Parent Menu Permissions:

親メニューが明示的にallow権限を付与されている場合、特にdenyとマークされていない限り、そのすべてのサブメニューもデフォルトでallow権限になります。 これは、親メニューがアクセス可能になると、そのサブメニューもデフォルトでアクセス可能になることを意味します。

直接的な権限設定がない親メニューの取り扱い:

親メニューに直接的な権限設定(明示的に許可されても拒否されてもいない)がなく、少なくとも1つのサブメニューが明示的にallow権限を付与されている場合、その親メニューは暗黙的にallow権限を持つと考えられます。 これにより、ユーザーはこれらのサブメニューに移動できます。

1.6 特別な権限継承ルール

ロール間の権限の継承について、特にdeny権限が関与するシナリオでは、以下のルールを遵守することでシステムのセキュリティと権限の精密な制御を確保する必要があります:

明示的な拒否とデフォルトの拒否の区別:

ROLE_ADMINのようなロールが、AdminSubMenu_denyのようなメニューアイテムに対するアクセスを明示的に拒否(denyとマーク)している場合、そのロールが別のロール(例:ROLE_ROOT)に継承されても、継承するロールは拒否されたメニューアイテムにアクセスすることは許されません。 これにより、ロールの継承によって明示的なセキュリティポリシーが迂回されることはありません。

デフォルトの拒否権限の継承:

逆に、ロールがメニューアイテム(例:UserSubMenu_deny)へのアクセスを拒否することがデフォルト(明示的にdenyとマークされていないが、明示的にallowが付与されていないため)である場合、そのロールが別のロール(例:ROLE_ADMIN)に継承されると、継承するロールはデフォルトのdenyステータスを上書きし、これらのメニューアイテムへのアクセスを許可することができます。

1.7 例の説明

ポリシー:

p, ROLE_ROOT, SystemMenu, read, allow
p, ROLE_ROOT, AdminMenu, read, allow
p, ROLE_ROOT, UserMenu, read, deny
p, ROLE_ADMIN, UserMenu, read, allow
p, ROLE_ADMIN, AdminMenu, read, allow
p, ROLE_ADMIN, AdminSubMenu_deny, read, deny
p, ROLE_USER, UserSubMenu_allow, read, allow

g, user, ROLE_USER
g, admin, ROLE_ADMIN
g, root, ROLE_ROOT
g, ROLE_ADMIN, ROLE_USER

g2, UserSubMenu_allow, UserMenu
g2, UserSubMenu_deny, UserMenu
g2, UserSubSubMenu, UserSubMenu_allow
g2, AdminSubMenu_allow, AdminMenu
g2, AdminSubMenu_deny, AdminMenu
g2, (NULL), SystemMenu
MenuNameROLE_ROOTROLE_ADMINROLE_USER
SystemMenu
UserMenu
UserSubMenu_allow
UserSubSubMenu
UserSubMenu_deny
AdminMenu
AdminSubMenu_allow
AdminSubMenu_deny

2. メニュー権限制御

特定のユーザー名でアクセス可能なすべてのメニューアイテムのリストは、MenuServiceで利用可能なfindAccessibleMenus()関数を通じて特定できます。 特定のユーザーが指定されたメニューアイテムにアクセスする権限を持っているかどうかを確認するために、checkMenuAccess()メソッドを利用できます。 このアプローチにより、メニューの権限が効果的に制御され、jCasbinの機能を活用してアクセス権を効率的に管理します。