{"id":3429,"library":"casbin","title":"Casbin","description":"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 like ACL, RBAC, and ABAC. Authorization models are defined using `.conf` files, and policies are stored in `.csv` files or various database backends via adapters. The library is actively maintained with frequent updates.","status":"active","version":"1.43.0","language":"python","source_language":"en","source_url":"https://github.com/apache/casbin-pycasbin","tags":["authorization","access control","ACL","RBAC","ABAC","permission","security"],"install":[{"cmd":"pip install casbin","lang":"bash","label":"Install core library"}],"dependencies":[],"imports":[{"symbol":"Enforcer","correct":"from casbin import Enforcer"}],"quickstart":{"code":"import casbin\nimport os\n\n# Create a simple model.conf file\nmodel_conf_content = \"\"\"\n[request_definition]\nr = sub, obj, act\n\n[policy_definition]\np = sub, obj, act\n\n[policy_effect]\ne = some(where (p.eft == allow))\n\n[matchers]\nm = r.sub == p.sub && r.obj == p.obj && r.act == p.act\n\"\"\"\n\n# Create a simple policy.csv file\npolicy_csv_content = \"\"\"\np, alice, data1, read\np, bob, data2, write\n\"\"\"\n\n# Save model and policy to temporary files\nwith open(\"model.conf\", \"w\") as f:\n    f.write(model_conf_content)\nwith open(\"policy.csv\", \"w\") as f:\n    f.write(policy_csv_content)\n\ntry:\n    # Initialize the enforcer\n    e = casbin.Enforcer(\"model.conf\", \"policy.csv\")\n\n    # Test enforcement\n    print(f\"Alice can read data1: {e.enforce('alice', 'data1', 'read')}\") # True\n    print(f\"Alice can write data1: {e.enforce('alice', 'data1', 'write')}\") # False\n    print(f\"Bob can read data2: {e.enforce('bob', 'data2', 'read')}\") # False\n    print(f\"Bob can write data2: {e.enforce('bob', 'data2', 'write')}\") # True\n    print(f\"Charlie can read data1: {e.enforce('charlie', 'data1', 'read')}\") # False\nfinally:\n    # Clean up temporary files\n    os.remove(\"model.conf\")\n    os.remove(\"policy.csv\")","lang":"python","description":"This quickstart demonstrates how to initialize a Casbin Enforcer with a basic model and policy, and then use it to check authorization requests. It creates temporary `model.conf` and `policy.csv` files, which define the access control structure and rules respectively. The `enforce` method is then called to determine if a subject (user), object (resource), and action combination is allowed."},"warnings":[{"fix":"Review the Casbin documentation and changelog for PyCasbin v2 regarding custom effector implementations and update your code accordingly.","message":"When upgrading to PyCasbin v2 (which was released with version 0.20.0), custom effectors require a rewrite due to API changes.","severity":"breaking","affected_versions":"0.x.x to 2.x.x (specifically 0.20.0 and above)"},{"fix":"Install the appropriate adapter package, e.g., `pip install casbin-sqlalchemy-adapter` or `pip install casbin-pymongo-adapter`, and configure the `Enforcer` to use it.","message":"The core `casbin` library only includes a default file adapter. For policy persistence in databases (e.g., MySQL, PostgreSQL, MongoDB), you must install a separate, corresponding adapter library.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement a separate authentication mechanism (e.g., OAuth, JWT, session management) in your application to verify user identities before passing them to Casbin for authorization checks.","message":"Casbin handles *authorization* (who can do what on which resource) but explicitly *does not* handle authentication (verifying user identity/passwords).","severity":"gotcha","affected_versions":"All versions"},{"fix":"For large policy sets or distributed environments, explore Casbin's filtered policy loading feature or implement a separate caching layer with explicit invalidation to reduce database pressure and ensure policy freshness.","message":"In distributed systems, the `SyncEnforcer`'s periodic policy reloading might lead to temporary inconsistencies or frequent database hits. Consider using filtered policy loading or a robust caching strategy.","severity":"gotcha","affected_versions":"All versions, especially in high-load or distributed environments"}],"env_vars":null,"search_vec":"'abac':33,67 'access':11,26,63 'acl':30,65 'activ':57 'adapt':53 'author':22,34,62 'backend':51 'base':23 'casbin':1,2 'conf':39 'control':12,27,64 'csv':46 'databas':50 'defin':37 'effici':7 'enforc':21 'file':40,47 'frequent':60 'librari':13,55 'like':29 'maintain':58 'model':28,35 'open':9 'open-sourc':8 'permiss':68 'polici':42 'power':5 'project':16 'provid':18 'python':15 'rbac':31,66 'secur':69 'sourc':10 'store':44 'support':19 'updat':61 'use':38 'various':25,49 'via':52","created_at":"2026-04-11T17:28:44.260116+00:00","updated_at":"2026-04-16T01:27:27.548593+00:00","problems":[{"fix":"Install the 'casbin' package using pip: `pip install casbin`","cause":"The 'casbin' library is not installed in the current Python environment or the environment where the code is being executed.","error":"ModuleNotFoundError: No module named 'casbin'"},{"fix":"Ensure you are calling policy management methods on an instance of `casbin.Enforcer` after it has been created: `enforcer.add_policy('alice', 'data1', 'read')`","cause":"Policy management methods like `add_policy`, `remove_policy`, etc., are intended to be called on an initialized `casbin.Enforcer` object, not directly on the `Model` object.","error":"AttributeError: 'Model' object has no attribute 'add_policy'"},{"fix":"Verify that the paths provided to the `casbin.Enforcer` constructor for the model and policy files are correct and that the files exist and are accessible in the specified location.","cause":"The `casbin.Enforcer` constructor or its internal methods (`LoadModel`, `LoadPolicy`) cannot find the specified model (`.conf`) or policy (`.csv`) file at the given path.","error":"FileNotFoundError: [Errno 2] No such file or directory: '<filepath>'"},{"fix":"Ensure that the Python object (e.g., `sub`, `obj`, `act`) passed to `enforcer.enforce()` is an instance of a class or has attributes that exactly match those referenced in your ABAC model's matcher definition.","cause":"When using Attribute-Based Access Control (ABAC), your Casbin model's matcher (e.g., `m = r.obj.Owner == p.obj.Owner`) references an attribute (e.g., 'Owner') that is not present in the Python object passed as an argument (e.g., `obj`) to the `enforce()` method.","error":"AttributeError: '<class_instance>' object has no attribute '<attribute_name>'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.43.0","cli_name":"","cli_version":null,"type":"library","homepage":"https://casbin.org/","github":"https://github.com/casbin/pycasbin","docs":null,"changelog":null,"pypi":"https://pypi.org/project/casbin/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["auth-security","database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}