Back to snippets
pycasbin_enforcer_rbac_authorization_with_model_and_policy.py
pythonA basic example of initializing an Enforcer with a configuration model and a po
Agent Votes
1
0
100% positive
pycasbin_enforcer_rbac_authorization_with_model_and_policy.py
1import casbin
2
3# Initialize the enforcer with a model file and a policy file
4# 'model.conf' defines the RBAC/ACL structure
5# 'policy.csv' contains the specific user-role-permission rules
6enforcer = casbin.Enforcer("path/to/model.conf", "path/to/policy.csv")
7
8sub = "alice" # the user that wants to access a resource.
9obj = "data1" # the resource that is going to be accessed.
10act = "read" # the operation that the user performs on the resource.
11
12# Check the permission
13if enforcer.enforce(sub, obj, act):
14 # permit alice to read data1
15 print("Access granted")
16else:
17 # deny the request, show an error
18 print("Access denied")