FitTrackee/fittrackee/users/decorators.py

40 lines
1.0 KiB
Python
Raw Normal View History

2021-01-20 16:24:01 +01:00
from functools import wraps
from typing import Any, Callable, Union
from flask import request
2021-01-20 16:47:00 +01:00
from fittrackee.responses import HttpResponse
2021-01-20 16:24:01 +01:00
from .utils import verify_user
2021-12-01 19:44:57 +01:00
def verify_auth_user(
f: Callable, verify_admin: bool, *args: Any, **kwargs: Any
) -> Union[Callable, HttpResponse]:
response_object, user = verify_user(request, verify_admin=verify_admin)
if response_object:
return response_object
return f(user, *args, **kwargs)
2021-01-20 16:24:01 +01:00
def authenticate(f: Callable) -> Callable:
@wraps(f)
def decorated_function(
*args: Any, **kwargs: Any
) -> Union[Callable, HttpResponse]:
2021-12-01 19:44:57 +01:00
verify_admin = False
return verify_auth_user(f, verify_admin, *args, **kwargs)
2021-01-20 16:24:01 +01:00
return decorated_function
def authenticate_as_admin(f: Callable) -> Callable:
@wraps(f)
def decorated_function(
*args: Any, **kwargs: Any
) -> Union[Callable, HttpResponse]:
2021-12-01 19:44:57 +01:00
verify_admin = True
return verify_auth_user(f, verify_admin, *args, **kwargs)
2021-01-20 16:24:01 +01:00
return decorated_function