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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for casbin_motor_adapter-0.3.0.tar.gz
Algorithm Hash digest
SHA256 605792bbfd57be6805250964b7aa4ee728ac06e4965eab4e2c7eb224486c8298
MD5 d944fa619a91ff55e0afb9091fb541a2
BLAKE2b-256 10a3f89adbfa1dfc2a8c3039af8980314d6bea540bb38b89057431fe83aa1cc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for casbin_motor_adapter-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3da9ad8f969d40d4611f2559e6e67ea9bb4a6bd4244850c3ccb54f3d81d7abd4
MD5 b7396299fcd78d75601ef09fa7cb4e24
BLAKE2b-256 e2fe0a5f69b7a43d9be94099c5f962c69d807f8407b2b9dd2ce0fbd165dfd93b

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