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.4.0.tar.gz (8.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_motor_adapter-0.4.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: casbin_motor_adapter-0.4.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for casbin_motor_adapter-0.4.0.tar.gz
Algorithm Hash digest
SHA256 197a76b43dd04015e8371690c633397585956196f83945ca1fd104669e81bfbf
MD5 90c848465b91420250f669549a56d761
BLAKE2b-256 b18fb641528cfa1a620ae297e936a262860e91f413b51c859138b40fc028180f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: casbin_motor_adapter-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for casbin_motor_adapter-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70f6170766560ebeea75e2719ec77b373f34360364770d30fd89685a28821465
MD5 4b035c0a8b01306fac4b4938b258fe44
BLAKE2b-256 3fcbdfdabc5b880aa55433432234133c453fb63f99f6e9a9f12438c474f7a202

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