Skip to main content

casbin-fastapi-decorator-file

File-based enforcer provider with hot-reload for casbin-fastapi-decorator.

Provides CachedFileEnforcerProvider — loads the casbin Enforcer once from model.conf + policy.csv and automatically reloads when either file changes on disk (via watchdog).

Install

pip install casbin-fastapi-decorator-file

Usage

from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from casbin_fastapi_decorator import PermissionGuard
from casbin_fastapi_decorator_file import CachedFileEnforcerProvider

provider = CachedFileEnforcerProvider(
    model_path="casbin/model.conf",
    policy_path="casbin/policy.csv",
)

@asynccontextmanager
async def lifespan(app: FastAPI):
    async with provider:   # starts file watcher
        yield              # stops file watcher on shutdown

guard = PermissionGuard(
    user_provider=get_current_user,
    enforcer_provider=provider,
    error_factory=lambda *_: HTTPException(403, "Forbidden"),
)

app = FastAPI(lifespan=lifespan)

@app.get("/articles")
@guard.require_permission("articles", "read")
async def list_articles(): ...

Any change to model.conf or policy.csv is detected automatically — the enforcer reloads on the next request with zero downtime.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

casbin_fastapi_decorator_file-1.2.2.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file casbin_fastapi_decorator_file-1.2.2.tar.gz.

File metadata

  • Download URL: casbin_fastapi_decorator_file-1.2.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for casbin_fastapi_decorator_file-1.2.2.tar.gz
Algorithm Hash digest
SHA256 5cce1aba561f9deff4f74e2031bcd3b81725f48727c3553b68d1dffcfe8f539e
MD5 a2732259fc9b3f61ac59f2d909daa9e3
BLAKE2b-256 5a867ef400f9233852c4c8209bba2717fcfbc227234de1e619a07ce731e2bae6

See more details on using hashes here.

File details

Details for the file casbin_fastapi_decorator_file-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: casbin_fastapi_decorator_file-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for casbin_fastapi_decorator_file-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 608bdf1055c6f5a0f893fb0cdfdbc9f88af3a755e11f4dda49f6ba04bc79908b
MD5 d47bd29c6c0118c4c3ab5295e922a943
BLAKE2b-256 af567c4e2cef3e7a784f23392387c6c88747241bcb588278691913cecbf4359d

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