PyMongo Adapter for PyCasbin

Clone this repo:
  1. 96ac5ef feat: Support reusing existing MongoDB client instances (#10) by Yang Luo · 6 weeks ago master v1.7.0
  2. 6dbea4f feat: remove changelog commits from semantic-release (#12) by Yang Luo · 6 weeks ago
  3. 18c6779 chore(release): 1.6.0 [skip ci] by semantic-release-bot · 7 months ago v1.6.0
  4. 24b7c92 feat: switch from casbin to pycasbin (#8) by Theo · 7 months ago
  5. 3a8fd5c chore(release): 1.5.0 [skip ci] by semantic-release-bot · 7 months ago v1.5.0

PyMongo Adapter for PyCasbin

build Status Coverage Status Version PyPI - Wheel Pyversions Download License

PyMongo Adapter is the PyMongo adapter for PyCasbin. With this library, Casbin can load policy from MongoDB or save policy to it.

This adapter supports both synchronous and asynchronous PyMongo APIs.

Installation

pip install casbin_pymongo_adapter

Simple Example

import casbin_pymongo_adapter
import casbin

adapter = casbin_pymongo_adapter.Adapter('mongodb://localhost:27017/', "dbname")

e = casbin.Enforcer('path/to/model.conf', adapter, True)

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 data1casbin_sqlalchemy_adapter
    pass
else:
    # deny the request, show an error
    pass

# define filter conditions
from casbin_pymongo_adapter import Filter

filter = Filter()
filter.ptype = ["p"]
filter.v0 = ["alice"]

# support MongoDB native query
filter.raw_query = {
    "ptype": "p",
    "v0": {
        "$in": ["alice"]
    }
}

# In this case, load only policies with sub value alice
e.load_filtered_policy(filter)

Using an Existing MongoDB Client

If you already have a MongoDB client instance in your application, you can reuse it:

from pymongo import MongoClient
import casbin_pymongo_adapter
import casbin

# Create or use your existing MongoDB client
mongo_client = MongoClient('mongodb://localhost:27017/')

# Pass the client to the adapter
adapter = casbin_pymongo_adapter.Adapter(client=mongo_client, db_name="casbin")

e = casbin.Enforcer('path/to/model.conf', adapter, True)

Async Example

from casbin_pymongo_adapter.asynchronous import Adapter
import casbin

adapter = Adapter('mongodb://localhost:27017/', "dbname")
e = casbin.AsyncEnforcer('path/to/model.conf', adapter)

# Note: AsyncEnforcer does not automatically load policies.
# You need to call load_policy() manually.
await e.load_policy()

Using an Existing AsyncMongoClient

from pymongo import AsyncMongoClient
from casbin_pymongo_adapter.asynchronous import Adapter
import casbin

# Create or use your existing AsyncMongoClient
mongo_client = AsyncMongoClient('mongodb://localhost:27017/')

# Pass the client to the adapter
adapter = Adapter(client=mongo_client, db_name="casbin")
e = casbin.AsyncEnforcer('path/to/model.conf', adapter)

await e.load_policy()

Getting Help

License

This project is licensed under the Apache 2.0 license.