Ideas, observations, craftsmanship, and frustrations of a software engineer.

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

def require_role(required_role: str = "user"):
    def wrapper(func):

        @functools.wraps(func)
        async def wrap_func(*args, current_user: Annotated[UserModel, Depends(AuthService.get_current_user)], **kwargs):
            user_role = current_user['role'] if 'role' in current_user else 'user'
            if user_roles_order.index(user_role) < user_roles_order.index(required_role):
                raise HTTPException(status_code=403, detail="Forbidden")

            try:
                if inspect.iscoroutinefunction(func):
                    result = await func(*args, current_user=current_user, **kwargs)
                else:
                    result = func(*args, current_user=current_user, **kwargs)
            except HTTPException as e:
                raise e

            return result

        return wrap_func

    return wrapper