2022-06-28 12:30:02 +02:00
|
|
|
from app.models import Permission
|
2021-09-22 13:50:43 +02:00
|
|
|
from flask import abort, current_app
|
2019-07-09 15:41:16 +02:00
|
|
|
from flask_login import current_user
|
2020-03-27 12:06:11 +01:00
|
|
|
from functools import wraps
|
2021-09-22 13:50:26 +02:00
|
|
|
from threading import Thread
|
2021-12-03 14:07:03 +01: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 15:41:16 +02:00
|
|
|
|
|
|
|
|
2020-03-26 16:14:09 +01:00
|
|
|
def admin_required(f):
|
2021-12-03 14:07:03 +01:00
|
|
|
return permission_required(Permission.ADMINISTRATE)(f)
|
|
|
|
|
|
|
|
|
|
|
|
def socketio_login_required(f):
|
2020-03-26 16:14:09 +01:00
|
|
|
@wraps(f)
|
2021-12-03 14:07:03 +01:00
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if current_user.is_authenticated:
|
2020-04-21 18:34:21 +02:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
else:
|
2021-12-03 14:07:03 +01:00
|
|
|
return {'code': 401, 'msg': 'Unauthorized'}
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
def socketio_permission_required(permission):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if not current_user.can(permission):
|
|
|
|
return {'code': 403, 'msg': 'Forbidden'}
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def socketio_admin_required(f):
|
|
|
|
return socketio_permission_required(Permission.ADMINISTRATE)(f)
|
2020-03-26 16:14:09 +01:00
|
|
|
|
|
|
|
|
2020-04-21 18:34:21 +02:00
|
|
|
def background(f):
|
2020-04-23 09:40:43 +02:00
|
|
|
'''
|
|
|
|
' This decorator executes a function in a Thread.
|
|
|
|
' Decorated functions need to be executed within a code block where an
|
|
|
|
' app context exists.
|
|
|
|
'
|
|
|
|
' NOTE: An app object is passed as a keyword argument to the decorated
|
|
|
|
' function.
|
|
|
|
'''
|
2020-03-26 16:14:09 +01:00
|
|
|
@wraps(f)
|
|
|
|
def wrapped(*args, **kwargs):
|
2020-04-23 08:35:18 +02:00
|
|
|
kwargs['app'] = current_app._get_current_object()
|
2021-09-22 13:50:26 +02:00
|
|
|
thread = Thread(target=f, args=args, kwargs=kwargs)
|
|
|
|
thread.start()
|
2020-04-21 18:34:21 +02:00
|
|
|
return thread
|
2020-03-26 16:14:09 +01:00
|
|
|
return wrapped
|