在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:pycasbin开源软件地址:https://gitee.com/casbin/pycasbin开源软件介绍:PyCasbinNews: Async version PyCasbin can be found at: https://pypi.org/project/asynccasbin/ News: still worry about how to write the correct Casbin policy? Casbin is a powerful and efficient open-source access control library for Python projects. It provides support for enforcing authorization based on various access control models. All the languages supported by Casbin:
Table of contents
Supported models
How it works?In Casbin, an access control model is abstracted into a CONF file based on the PERM metamodel (Policy, Effect, Request, Matchers). So switching or upgrading the authorization mechanism for a project is just as simple as modifying a configuration. You can customize your own access control model by combining the available models. For example, you can get RBAC roles and ABAC attributes together inside one model and share one set of policy rules. The most basic and simplest model in Casbin is ACL. ACL's model CONF is: # Request definition[request_definition]r = sub, obj, act# Policy definition[policy_definition]p = sub, obj, act# Policy effect[policy_effect]e = some(where (p.eft == allow))# Matchers[matchers]m = r.sub == p.sub && r.obj == p.obj && r.act == p.act An example policy for ACL model is like: p, alice, data1, readp, bob, data2, write It means:
We also support multi-line mode by appending '\' in the end: # Matchers[matchers]m = r.sub == p.sub && r.obj == p.obj \ && r.act == p.act Further more, if you are using ABAC, you can try operator # Matchers[matchers]m = r.obj == p.obj && r.act == p.act || r.obj in ('data2', 'data3') But you SHOULD make sure that the length of the array is MORE than 1, otherwise there will cause it to panic. For more operators, you may take a look at govaluate FeaturesWhat Casbin does:
What Casbin does NOT do:
Installationpip install casbin Documentationhttps://casbin.org/docs/en/overview Online editorYou can also use the online editor (http://casbin.org/editor/) to write your Casbin model and policy in your web browser. It provides functionality such as Tutorialshttps://casbin.org/docs/en/tutorials Get started
import casbine = casbin.Enforcer("path/to/model.conf", "path/to/policy.csv") Note: you can also initialize an enforcer with policy in DB instead of file, see Persistence section for details.
sub = "alice" # the user that wants to access a resource.obj = "data1" # the resource that is going to be accessed.act = "read" # the operation that the user performs on the resource.if e.enforce(sub, obj, act): # permit alice to read data1 passelse: # deny the request, show an error pass
roles = e.get_roles_for_user("alice") See Policy management APIs for more usage.
Policy managementCasbin provides two sets of APIs to manage permissions:
We also provide a web-based UI for model management and policy management: Policy persistencehttps://casbin.org/docs/en/adapters Role managerhttps://casbin.org/docs/en/role-managers Benchmarkshttps://casbin.org/docs/en/benchmark Examples
MiddlewaresAuthz middlewares for web frameworks: https://casbin.org/docs/en/middlewares Our adoptershttps://casbin.org/docs/en/adopters ContributorsThis project exists thanks to all the people who contribute. BackersThank you to all our backers! |
请发表评论