Skip to main content

PyMongo Adapter for PyCasbin

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

casbin_pymongo_adapter-1.7.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

casbin_pymongo_adapter-1.7.0-py2.py3-none-any.whl (14.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file casbin_pymongo_adapter-1.7.0.tar.gz.

File metadata

  • Download URL: casbin_pymongo_adapter-1.7.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for casbin_pymongo_adapter-1.7.0.tar.gz
Algorithm Hash digest
SHA256 1326ea4b9a882d935fb792cbcb84dc62a4c45d34995012ad0754bcee248c2e47
MD5 653224f1badd5ce3e46588f26c5130f1
BLAKE2b-256 9a3a5b2f8119da0d506d4d8abdb2bd2c12512d339419327c51d364ddcb9084b3

See more details on using hashes here.

File details

Details for the file casbin_pymongo_adapter-1.7.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for casbin_pymongo_adapter-1.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a4c7c55d284d7e46bde2e44436994461fe3152063c260d8d4a85338c0b619ab8
MD5 715a8272597a309f9cc8be91a88792b6
BLAKE2b-256 6c408dc1c92ffdea42251bf87703bd249a0a4dc5ed35354ed14f5ac6ef74d07d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page