Setting up RBAC
RBAC is a method of regulating access to resources based on the roles of individual users within an organization. In this recipe, we’ll implement RBAC in a FastAPI application to manage user permissions effectively.
Getting ready
Since we will expand our database to accommodate role definitions, make sure you have completed the Setting up user registration recipe before diving into this.
To set up access control, we first need to define a variety of roles that we can allocate to. Let’s follow these steps to do it.
- In the
module.py
module, we can define a new class calledRole
and add it as a new field of theUser
model that will be stored in the users table:from enum import Enum class Role(str, Enum): basic = "basic" premium = "premium" class User(Base): __tablename__ = "users" # existing fields role: Mapped[Role...