Skip to main content

Motor Adapter for PyCasbin

Project description

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.

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_motor_adapter-0.2.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

casbin_motor_adapter-0.2.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file casbin_motor_adapter-0.2.0.tar.gz.

File metadata

  • Download URL: casbin_motor_adapter-0.2.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-1012-azure

File hashes

Hashes for casbin_motor_adapter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c4b90e487de472f9324ace34b5cd5a59733165894ad72cf1e750d061b795b568
MD5 f44749e1bc56ba1dae4b5119bbd06418
BLAKE2b-256 7f22cb392c6e4be2f4ca093a165b4d99c68f65b41273fd5cc5f058029ce63dd1

See more details on using hashes here.

File details

Details for the file casbin_motor_adapter-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for casbin_motor_adapter-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6cdac33db16b771bdf61a0af9cafb12b3173e40f69fd26aa63210ffbad1c9ebb
MD5 fbd0ebde6479a1eb6055781a5a2515f9
BLAKE2b-256 7b87330e6cf09b6e38b0dc01501e72458e3f5e0f31bd723d6b2ecd6daae664d4

See more details on using hashes here.

Supported by

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