2024-05-04 12:55:05 +00:00
|
|
|
from flask import abort, request
|
2019-07-09 13:41:16 +00:00
|
|
|
from flask_login import current_user
|
2020-03-27 11:06:11 +00:00
|
|
|
from functools import wraps
|
2024-09-25 15:46:53 +00:00
|
|
|
from typing import Optional
|
2023-03-06 14:02:46 +00:00
|
|
|
from werkzeug.exceptions import NotAcceptable
|
2022-09-02 11:02:04 +00:00
|
|
|
from app.models import Permission
|
2021-12-03 13:07:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def permission_required(permission):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if not current_user.can(permission):
|
|
|
|
abort(403)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
2019-07-09 13:41:16 +00:00
|
|
|
|
|
|
|
|
2020-03-26 15:14:09 +00:00
|
|
|
def admin_required(f):
|
2021-12-03 13:07:03 +00:00
|
|
|
return permission_required(Permission.ADMINISTRATE)(f)
|
|
|
|
|
|
|
|
|
2024-05-04 12:55:05 +00:00
|
|
|
def socketio_login_required(f):
|
2020-03-26 15:14:09 +00:00
|
|
|
@wraps(f)
|
2024-05-04 12:55:05 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
return f(*args, **kwargs)
|
2024-12-03 14:59:08 +00:00
|
|
|
return {'status': 401, 'statusText': 'Unauthorized'}
|
2024-05-04 12:55:05 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def socketio_permission_required(permission):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if not current_user.can(permission):
|
2024-12-03 14:59:08 +00:00
|
|
|
return {'status': 403, 'statusText': 'Forbidden'}
|
2024-05-04 12:55:05 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def socketio_admin_required(f):
|
|
|
|
return socketio_permission_required(Permission.ADMINISTRATE)(f)
|
2023-03-06 14:02:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def content_negotiation(
|
2024-09-25 15:46:53 +00:00
|
|
|
produces: Optional[str | list[str]] = None,
|
|
|
|
consumes: Optional[str | list[str]] = None
|
2023-03-06 14:02:46 +00:00
|
|
|
):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
provided = request.mimetype
|
2023-03-07 15:32:15 +00:00
|
|
|
if consumes is None:
|
|
|
|
consumeables = None
|
|
|
|
elif isinstance(consumes, str):
|
2023-03-06 14:02:46 +00:00
|
|
|
consumeables = {consumes}
|
2023-03-07 15:32:15 +00:00
|
|
|
elif isinstance(consumes, list) and all(isinstance(x, str) for x in consumes):
|
2023-03-06 14:02:46 +00:00
|
|
|
consumeables = {*consumes}
|
2023-03-07 15:32:15 +00:00
|
|
|
else:
|
|
|
|
raise TypeError()
|
2023-03-06 14:02:46 +00:00
|
|
|
accepted = {*request.accept_mimetypes.values()}
|
2023-03-07 15:32:15 +00:00
|
|
|
if produces is None:
|
|
|
|
produceables = None
|
|
|
|
elif isinstance(produces, str):
|
2023-03-06 14:02:46 +00:00
|
|
|
produceables = {produces}
|
2023-03-07 15:32:15 +00:00
|
|
|
elif isinstance(produces, list) and all(isinstance(x, str) for x in produces):
|
2023-03-06 14:02:46 +00:00
|
|
|
produceables = {*produces}
|
2023-03-07 15:32:15 +00:00
|
|
|
else:
|
|
|
|
raise TypeError()
|
|
|
|
if produceables is not None and len(produceables & accepted) == 0:
|
2023-03-06 14:02:46 +00:00
|
|
|
raise NotAcceptable()
|
2023-03-07 15:32:15 +00:00
|
|
|
if consumeables is not None and provided not in consumeables:
|
2023-03-06 14:02:46 +00:00
|
|
|
raise NotAcceptable()
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|