Skip to content

Instantly share code, notes, and snippets.

View tsatsujnr139's full-sized avatar

Tsatsu Adogla-Bessa Jnr tsatsujnr139

View GitHub Profile
@tsatsujnr139
tsatsujnr139 / users.py
Last active April 21, 2021 22:08
FastAPI RBAC
from typing import Any, List
from app import crud, models, schemas
from app.api import deps
from app.constants.role import Role
from app.core.config import settings
from fastapi import APIRouter, Body, Depends, HTTPException, Security
from fastapi.encoders import jsonable_encoder
from pydantic.networks import EmailStr
from pydantic.types import UUID4
from datetime import datetime
from typing import Optional
from app.schemas.user_role import UserRole
from pydantic import UUID4, BaseModel, EmailStr
# Shared properties
class UserBase(BaseModel):
email: Optional[EmailStr] = None
import datetime
from uuid import uuid4
from app.db.base_class import Base
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
class User(Base):
from datetime import timedelta
from typing import Any
from app import crud, models, schemas
from app.api import deps
from app.core import security
from app.core.config import settings
from fastapi import APIRouter, Body, Depends, HTTPException
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session
from datetime import datetime, timedelta
from typing import Any, Union
from app.core.config import settings
from jose import jwt
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
ALGORITHM = "HS256"
@router.get("", response_model=List[schemas.User])
def read_users(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
current_user: models.User = Security(
deps.get_current_active_user,
scopes=[Role.ADMIN["name"], Role.SUPER_ADMIN["name"]],
),
) -> Any:
import logging
from typing import Generator
from app import crud, models, schemas
from app.constants.role import Role
from app.core import security
from app.core.config import settings
from app.db.session import SessionLocal
from fastapi import Depends, HTTPException, Security, status
from fastapi.security import OAuth2PasswordBearer, SecurityScopes
class Role:
"""
Constants for the various roles scoped in the application ecosystem
"""
GUEST = {
"name": "GUEST",
"description": "A Guest Account",
}
ACCOUNT_ADMIN = {