Skip to content

Commit c4ec72b

Browse files
committed
test: casbin api testing
1 parent e4f30c5 commit c4ec72b

5 files changed

Lines changed: 224 additions & 0 deletions

File tree

authz.policy

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
############################################
2+
# Open edX AuthZ — Casbin Policy Configuration
3+
#
4+
# This file defines policies that work with the model configuration.
5+
# Uses namespaced subjects, actions, and scopes for maximum flexibility.
6+
############################################
7+
8+
# Policy definitions - format: p = subject(role), action, scope, effect
9+
# For role definitions use: lib*, course:*, org:* to specify the scope of the role
10+
11+
# Library Admin Role Policies
12+
p, role:library_admin, lib:math_101, act:delete_library, allow
13+
p, role:library_admin, lib:math_101, act:publish_library, allow
14+
p, role:library_admin, act:manage_library_team, lib:*, allow
15+
p, role:library_admin, act:manage_library_tags, lib:*, allow
16+
p, role:library_admin, act:delete_library_content, lib:*, allow
17+
p, role:library_admin, act:publish_library_content, lib:*, allow
18+
p, role:library_admin, act:delete_library_collection, lib:*, allow
19+
p, role:library_admin, act:create_library, lib:*, allow
20+
p, role:library_admin, act:create_library_collection, lib:*, allow
21+
22+
# Library Author Role Policies
23+
p, role:library_author, act:delete_library_content, lib:*, allow
24+
p, role:library_author, act:publish_library_content, lib:*, allow
25+
p, role:library_author, act:edit_library, lib:*, allow
26+
p, role:library_author, act:manage_library_tags, lib:*, allow
27+
p, role:library_author, act:create_library_collection, lib:*, allow
28+
p, role:library_author, act:edit_library_collection, lib:*, allow
29+
p, role:library_author, act:delete_library_collection, lib:*, allow
30+
31+
# Library Collaborator Role Policies
32+
p, role:library_collaborator, act:edit_library, lib:*, allow
33+
p, role:library_collaborator, act:delete_library_content, lib:*, allow
34+
p, role:library_collaborator, act:manage_library_tags, lib:*, allow
35+
p, role:library_collaborator, act:create_library_collection, lib:*, allow
36+
p, role:library_collaborator, act:edit_library_collection, lib:*, allow
37+
p, role:library_collaborator, act:delete_library_collection, lib:*, allow
38+
39+
# Library User Role Policies
40+
p, role:library_user, act:view_library, lib:*, allow
41+
p, role:library_user, act:view_library_team, lib:*, allow
42+
p, role:library_user, act:reuse_library_content, lib:*, allow
43+
44+
# Example negative rule (deny overrides allow)
45+
# p, role:restricted_user, act:delete, course-v1:sensitive:*, deny
46+
47+
# User-to-Role assignments (g) - format: g = user, role, scope
48+
# These would be populated dynamically based on actual user assignments
49+
#
50+
# Examples using lib: namespace format:
51+
g, user:alice_admin, role:library_admin, lib:math_101
52+
g, user:bob_author, role:library_author, lib:history_201
53+
g, user:carol_collaborator, role:library_collaborator, lib:science_301
54+
g, user:dave_user, role:library_user, lib:english_101
55+
g, user:eve_multi, role:library_admin, lib:physics_401
56+
g, user:eve_multi, role:library_author, lib:chemistry_501
57+
g, user:frank_global, role:library_user, *
58+
59+
# Action Inheritance (g2) - format: g2 = parent_action, child_action
60+
# The logical inheritance hierarchy for actions
61+
g2, act:edit_library, act:delete_library
62+
g2, act:view_library, act:edit_library
63+
g2, act:edit_library, act:create_library
64+
g2, act:view_library, act:publish_library
65+
g2, act:view_library_team, act:manage_library_team
66+
g2, act:view_library_tags, act:manage_library_tags
67+
g2, act:edit_library_collection, act:delete_library_collection
68+
g2, act:view_library_collection, act:edit_library_collection
69+
g2, act:edit_library_collection, act:create_library_collection
70+
g2, act:view_library_content, act:edit_library_content
71+
g2, act:edit_library_content, act:delete_library_content
72+
g2, act:view_library_content, act:publish_library_content
73+
g2, act:view_library_content, act:reuse_library_content

main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
This is a simple example of how to use Casbin to enforce policies.
3+
"""
4+
5+
from casbin import Enforcer
6+
7+
# policy_file = "simple-policy.csv"
8+
# model_file = "simple-model.conf"
9+
10+
policy_file = "authz.policy"
11+
model_file = "model.conf"
12+
13+
enforcer = Enforcer(model_file, policy_file)
14+
15+
enforcer.load_policy()
16+
17+
18+
# Complex Example
19+
20+
# Get permissions for a user
21+
print(enforcer.get_permissions_for_user("role:library_admin"))
22+
23+
# # Get permissions for a user in a specific domain
24+
# print(enforcer.get_permissions_for_user_in_domain("alice", "lib:math_101"))
25+
26+
# Get implicit permissions for a user
27+
print(enforcer.get_implicit_permissions_for_user("user:alice_admin", "lib:math_101"))
28+
print(enforcer.get_implicit_permissions_for_user("user:frank_global"))
29+
30+
# Get roles for a user in a specific domain
31+
print(enforcer.get_roles_for_user_in_domain("user:alice_admin", "lib:math_101"))
32+
print(enforcer.get_roles_for_user_in_domain("user:frank_global", "*"))
33+
34+
# Get all roles
35+
print(enforcer.get_role_manager().get_roles("user:alice_admin", "lib:math_101"))
36+
37+
# Print all roles
38+
print(enforcer.get_role_manager().print_roles())

model.conf

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
############################################
2+
# Open edX AuthZ — Casbin Model Configuration
3+
#
4+
# This model supports:
5+
# - Scoped role assignments (user roles tied to specific contexts)
6+
# - Action grouping (manage → read/write/edit/delete to reduce duplication)
7+
# - System-wide roles (global scope "*" applies everywhere)
8+
# - Negative rules (deny overrides allow for exceptions)
9+
# - Namespace support (course:*, lib:*, org:*, etc.)
10+
# - Extensibility (new resource types just need new namespaces)
11+
############################################
12+
13+
[request_definition]
14+
# Request format: subject (user), action, scope (specific resource being accessed)
15+
#
16+
# sub = subject/principal with namespace (e.g., "user:alice", "service:lms")
17+
# act = action with namespace (e.g., "act:read", "act:manage", "act:edit-courses")
18+
# scope = authorization scope context (e.g., "org:OpenedX", "course-v1:...", "*" for global)
19+
#
20+
# SCOPE SEMANTICS:
21+
# Scope determines the authorization context and which role assignments apply
22+
# - "*" = global scope (system-wide roles apply everywhere)
23+
# - "org:..." = organization-scoped roles (apply within specific organization)
24+
# - "course-v1:..." = course-scoped roles (apply within specific course)
25+
# - "lib:..." = library-scoped roles (apply within specific library)
26+
#
27+
# Application must provide appropriate scope based on business logic.
28+
r = sub, act, scope
29+
30+
[policy_definition]
31+
# Policy format: subject (role), action, scope (pattern), effect
32+
#
33+
# sub = role or user with namespace (e.g., "role:org_admin", "user:bob")
34+
# act = action identifier (e.g., "act:manage", "act:read", "act:edit-courses")
35+
# scope = scope where policy applies (e.g., "*", "org:*", "course-v1:*", "lib:*")
36+
# eft = "allow" or "deny" (deny overrides allow for exceptions)
37+
p = sub, act, scope, eft
38+
39+
[role_definition]
40+
# g: Role assignments with scope
41+
# Format: user/subject, role, scope
42+
#
43+
# Examples:
44+
# g, user:alice, role:org_admin, org:OpenedX # Alice is org admin for OpenedX
45+
# g, user:bob, role:course_instructor, course-v1:... # Bob is instructor for specific course
46+
# g, user:carol, role:library_admin, * # Carol is global library admin
47+
#
48+
# Role hierarchy (optional):
49+
# g, role:org_admin, role:org_editor, org:OpenedX # org_admin inherits org_editor permissions
50+
g = _, _, _
51+
52+
# g2: Action grouping and implications
53+
# Maps high-level actions to specific actions to reduce policy duplication
54+
#
55+
# Examples:
56+
# g2, act:manage, act:edit # manage implies edit
57+
# g2, act:manage, act:delete # manage implies delete
58+
# g2, act:edit-courses, act:read # edit-courses implies read (for resource access)
59+
# g2, act:edit-courses, act:write # edit-courses implies write (for resource modification)
60+
g2 = _, _
61+
62+
[policy_effect]
63+
# Deny-override policy: allow if any rule allows AND no rule denies
64+
# This enables negative rules/exceptions (e.g., "manage all courses except course Z")
65+
#
66+
# Evaluation order:
67+
# 1. Check if any policy grants allow
68+
# 2. Check if any policy specifies deny
69+
# 3. If deny found, result is deny (exceptions win)
70+
# 4. If allow found and no deny, result is allow
71+
# 5. If no matches, result is deny (default secure)
72+
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))
73+
74+
[matchers]
75+
# Authorization matching logic
76+
#
77+
# ROLE MATCHING:
78+
# - g(r.sub, p.sub, r.scope): check if subject has role in requested scope
79+
# - g(r.sub, p.sub, "*"): check if subject has role in all resources in the scope
80+
#
81+
# SCOPE MATCHING:
82+
# - keyMatch(r.scope, p.scope): scope matches pattern
83+
#
84+
# ACTION MATCHING:
85+
# - r.act == p.act: exact action match
86+
# - g2(p.act, r.act): policy action implies requested action via grouping
87+
#
88+
# All conditions must be true for a policy to match:
89+
# 1. Subject must have role in scope OR global role
90+
# 2. Scope must match pattern
91+
# 3. Action must match OR inherit via action grouping
92+
m = (g(r.sub, p.sub, r.scope) || g(r.sub, p.sub, "*")) && keyMatch(r.scope, p.scope) && (r.act == p.act || g2(p.act, r.act))

simple-model.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[request_definition]
2+
r = sub, dom, obj, act
3+
4+
[policy_definition]
5+
p = sub, dom, obj, act
6+
7+
[role_definition]
8+
g = _, _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act

simple-policy.csv

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
p, admin, domain1, data1, read
2+
p, admin, domain1, data1, write
3+
p, admin, domain2, data2, read
4+
p, admin, domain2, data2, write
5+
6+
g, alice, admin, domain1
7+
g, bob, admin, domain2

0 commit comments

Comments
 (0)