Skip to main content

Motor Adapter for PyCasbin

Test Lint Release Coverage Status PyPI - Version PyPI - Python Version PyPI - License

Motor adapter for PyCasbin.
With this library, Casbin can load policy from MongoDB or save policy to it.

Example

Motor support a coroutine-based API for non-blocking access to MongoDB.
So that, this adapter allows you to use pycasbin's AsyncEnforcer.

load_policy()

load_policy() loads all policies from storage.

import casbin_motor_adapter
import casbin

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

e = casbin.AsyncEnforcer('path/to/model.conf', adapter, True)
await e.load_policy()

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

load_filtered_policy()

load_filtered_policy() loads filtered policies from storage. This is useful for performance optimization.

Policy Subset Loading, https://casbin.org/docs/policy-subset-loading

Additionally, load_filtered_policy() supports the MongoDB native queries for filtering conditions.

import casbin_motor_adapter
import casbin

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

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

# define filter conditions
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
await e.load_filtered_policy(filter)

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

Acknowledgments

This adapter was inspired by pymongo-adapter.

License

Apache 2.0 license.

Metadata

Release files for casbin_motor_adapter 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for casbin_motor_adapter 0.4.0
File Size Uploaded Provenance Actions
casbin_motor_adapter-0.4.0.tar.gz 8.1 kB Without provenance Details for casbin_motor_adapter-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for casbin_motor_adapter 0.4.0
File Interpreter ABI Platform Actions
casbin_motor_adapter-0.4.0-py3-none-any.whl Python 3 none any Details for casbin_motor_adapter-0.4.0-py3-none-any.whl

Total release size: 17.0 kB

Release files / casbin_motor_adapter-0.4.0.tar.gz

Download URL casbin_motor_adapter-0.4.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
197a76b43dd04015e8371690c633397585956196f83945ca1fd104669e81bfbf
BLAKE2b-256 checksum
How to use checksums
b18fb641528cfa1a620ae297e936a262860e91f413b51c859138b40fc028180f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

Release files / casbin_motor_adapter-0.4.0-py3-none-any.whl

Download URL casbin_motor_adapter-0.4.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70f6170766560ebeea75e2719ec77b373f34360364770d30fd89685a28821465
BLAKE2b-256 checksum
How to use checksums
3fcbdfdabc5b880aa55433432234133c453fb63f99f6e9a9f12438c474f7a202
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

Security for casbin_motor_adapter 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page