Skip to main content

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

Project description

RoleFlow

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 roleflow

To install with FastAPI dependencies:

pip install roleflow[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.2.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.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: roleflow-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 846f0b88f94aac99cb09d2c24d929e010d3e8de0494ce2438d7bc5a066753769
MD5 9a1b1b78764c2d1eded097377b5a6a93
BLAKE2b-256 c2d3e8f916340f551e5cce8763436389c563f712eca8938436a88d94fea8f194

See more details on using hashes here.

File details

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

File metadata

  • Download URL: roleflow-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9ab54d6da67d3e1808d3d98e6cf2d428ed4c0b1df80dc0f8cc507d2a1820bc44
MD5 9b10cf74bb312b608b01cd970c1be2fb
BLAKE2b-256 924e5198e7c9d51d8483a440f5c376a090f252af3b22810594eb60a77bfda6de

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