Skip to main content

A lightweight, hassle-free and production-ready RBAC (Role-Based Access Control) library.

Project description

Easy RBAC

A lightweight, production-ready Role-Based Access Control (RBAC) package for Python, designed to be simple, fast, and framework-agnostic, while featuring seamless integration out-of-the-box for FastAPI.

Features

  • Generic RBAC Engine: Easily verify permissions using wildcards (*, table.*) or exact matches.
  • Pydantic Validation: Strong typing and validation for your Role and Permission schemas.
  • FastAPI Integration: Native RBACGuard dependency injection for secure and hassle-free route protection.

Installation

pip install easy-rbac

To install with FastAPI dependencies:

pip install easy-rbac[fastapi]

Quick Start

1. Define your Roles

from easy_rbac import Role, RBACEngine

roles = [
    Role(id=1, name="ROLE_ADMIN", permissions=["*"]),
    Role(id=2, name="ROLE_STUDENT", permissions=["profile.read", "profile.edit", "course.read"]),
    Role(id=3, name="ROLE_HOD", permissions=["course.*", "leave.approve"])
]

engine = RBACEngine(roles=roles)

2. Fetch Roles from a Database (Dynamic Loading)

You don't have to provide all roles upfront. You can hook into your Database ORM by passing a role_loader callback function to the engine:

from easy_rbac import Role, RBACEngine

# Simulated database fetch function (e.g. using SQLAlchemy)
def db_role_loader(role_name: str) -> Role:
    # 1. Query your database here using SQLAlchemy
    # db_record = session.query(DbRole).filter(DbRole.name == role_name).first()
    # 2. Convert database result into the generic easy_rbac.Role schema
    # return Role(id=db_record.id, name=db_record.name, permissions=db_record.permissions)
    pass

# Initialize engine without static roles
engine = RBACEngine(role_loader=db_role_loader)

# The engine will automatically call db_role_loader("ROLE_ADMIN") and cache it!
engine.is_granted("ROLE_ADMIN", "table1.read")

3. Check Permissions

# Returns True
engine.is_granted("ROLE_ADMIN", "anything.you.want") 
engine.is_granted("ROLE_STUDENT", "profile.read")
engine.is_granted("ROLE_HOD", "course.create")

# Returns False
engine.is_granted("ROLE_STUDENT", "course.create")

4. FastAPI Integration

from fastapi import FastAPI, Depends
from easy_rbac.fastapi import RBACGuard

app = FastAPI()

# A mock function to get the current user's role
def get_current_user_role() -> str:
    return "ROLE_STUDENT"

guard = RBACGuard(engine=engine, role_provider=get_current_user_role)

@app.get("/courses", dependencies=[Depends(guard("course.read"))])
def list_courses():
    return {"message": "You can read courses!"}

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

roleflow-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

roleflow-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file roleflow-0.1.1.tar.gz.

File metadata

  • Download URL: roleflow-0.1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for roleflow-0.1.1.tar.gz
Algorithm Hash digest
SHA256 98e3a2dae5ce8b6d519f11c9af5619389bf6da9cf4bb882b2f78eb73e58d311d
MD5 6979b13606865fee74599e5527bddc49
BLAKE2b-256 d751e6220de012409310799a9f4baa6565bc7236833458d5efb9ab8e7eb335f7

See more details on using hashes here.

File details

Details for the file roleflow-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: roleflow-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for roleflow-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7f114d2d738356c29bf2f76acfa6eb85b4b4ef279e64b2f24e055cc257239c4
MD5 a8da991f7e47a1ce1c579cee86e5a5d5
BLAKE2b-256 a0482bf7812abf32e348181442b6d891e676f926ec403216892d0eb7cc608e8a

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