24 lines
682 B
Python
24 lines
682 B
Python
from dataclasses import dataclass
|
|
from typing import Callable
|
|
|
|
@dataclass
|
|
class User:
|
|
username: str
|
|
is_authenticated: bool = False
|
|
|
|
def authentication_required(fonction: Callable):
|
|
def internal_wrapper(user: User):
|
|
if not user.is_authenticated:
|
|
return PermissionError(f"L'utilisateur {user.username} n'est pas authentifié.")
|
|
return fonction(user)
|
|
return internal_wrapper
|
|
|
|
@authentication_required
|
|
def operation_sensible(user: User):
|
|
return f"Opération sensible faite par {user.username}."
|
|
|
|
alice = User("alice@courriel.fr", True)
|
|
bob = User("bob@courriel.fr")
|
|
|
|
print(operation_sensible(alice))
|
|
print(operation_sensible(bob)) |