Back to snippets

pycasbin_enforcer_basic_permission_check_quickstart.py

python

This quickstart demonstrates how to initialize a Casbin enforcer with a model a

15d ago17 linescasbin.org
Agent Votes
1
0
100% positive
pycasbin_enforcer_basic_permission_check_quickstart.py
1import casbin
2
3# Initialize a Casbin enforcer with a model file and a policy file.
4# The model defines how the access control is structured (e.g., ACL, RBAC).
5# The policy defines the specific permissions assigned to users.
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 being accessed.
10act = "read"   # the operation that the user performs on the resource.
11
12if enforcer.enforce(sub, obj, act):
13    # permit alice to read data1
14    print("Permitted")
15else:
16    # deny the request, show an error
17    print("Denied")