shepilov-vladislav / ormar-casbin-adapter

Ormar Casbin Adapter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ormar Adapter for PyCasbin

Repo

GitHub Workflow Status Codecov Code Climate maintainability Dependabot

GitHub

GitHub tag (latest SemVer) GitHub tag (latest by date) GitHub last commit

PyPI

PyPI - Version PyPI - Python Versions PyPI - Python Wheel PyPI - Format PyPI - Status PyPI - Downloads PyPI - License

ormar Adapter is the ormar adapter for PyCasbin. With this library, Casbin can load policy from ormar supported database or save policy to it.

Based on Officially Supported Databases, The current supported databases are:

  • PostgreSQL
  • MySQL
  • SQLite

Installation

pip install ormar_casbin_adapter

or

poetry add ormar-casbin-adapter

Simple Example

import casbin
import databases
import ormar
from ormar_casbin_adapter import DatabasesAdapter
import sqlalchemy

database = Database("sqlite://", force_rollback=True)
metadata = sqlalchemy.MetaData()


class CasbinRule(ormar.Model):
    class Meta:
        database = database
        metadata = metadata
        tablename = "casbin_rules"

    id: int = ormar.Integer(primary_key=True)
    ptype: str = ormar.String(max_length=255)
    v0: str = ormar.String(max_length=255)
    v1: str = ormar.String(max_length=255)
    v2: str = ormar.String(max_length=255, nullable=True)
    v3: str = ormar.String(max_length=255, nullable=True)
    v4: str = ormar.String(max_length=255, nullable=True)
    v5: str = ormar.String(max_length=255, nullable=True)


adapter = DatabasesAdapter(model=CasbinRule)

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

Getting Help

License

This project is licensed under the Apache 2.0 license.

About

Ormar Casbin Adapter

License:Apache License 2.0


Languages

Language:Python 100.0%