2019-05-06 16:58:36 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2019-06-11 17:37:20 +02:00
|
|
|
import os
|
2019-11-05 15:18:03 +01:00
|
|
|
import sys
|
|
|
|
|
2019-05-06 16:58:36 +02:00
|
|
|
from functools import wraps
|
2024-08-13 14:59:30 +02:00
|
|
|
from flask_login import LoginManager, current_user, logout_user, login_required
|
2019-05-06 16:58:36 +02:00
|
|
|
|
2024-08-13 14:59:30 +02:00
|
|
|
from flask import make_response, current_app
|
2019-05-06 16:58:36 +02:00
|
|
|
|
|
|
|
login_manager = LoginManager()
|
2019-11-20 16:15:08 +01:00
|
|
|
login_manager.login_view = 'root.role'
|
2019-05-06 16:58:36 +02:00
|
|
|
|
2019-09-03 11:58:34 +02:00
|
|
|
###############################################################
|
2019-09-03 12:00:24 +02:00
|
|
|
############### FLASK CACHE ##################
|
2019-09-03 11:58:34 +02:00
|
|
|
###############################################################
|
|
|
|
def no_cache(func):
|
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
resp = make_response(func(*args, **kwargs))
|
|
|
|
resp.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
|
|
|
|
resp.headers['Pragma'] = 'no-cache'
|
|
|
|
return resp
|
|
|
|
return decorated_view
|
|
|
|
###############################################################
|
|
|
|
###############################################################
|
|
|
|
###############################################################
|
|
|
|
|
2019-06-19 15:00:25 +02:00
|
|
|
###############################################################
|
|
|
|
############### CHECK ROLE ACCESS ##################
|
|
|
|
###############################################################
|
|
|
|
|
2019-05-06 16:58:36 +02:00
|
|
|
def login_admin(func):
|
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return login_manager.unauthorized()
|
2024-05-06 16:21:00 +02:00
|
|
|
elif not current_user.is_in_role('admin'):
|
2019-05-06 16:58:36 +02:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
|
|
|
|
2024-09-05 16:40:24 +02:00
|
|
|
def login_coordinator(func):
|
2019-05-06 16:58:36 +02:00
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return login_manager.unauthorized()
|
2024-09-05 16:40:24 +02:00
|
|
|
elif not current_user.is_in_role('coordinator'):
|
2019-05-06 16:58:36 +02:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
2019-06-11 17:37:20 +02:00
|
|
|
|
2019-11-20 16:15:08 +01:00
|
|
|
def login_user(func):
|
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return login_manager.unauthorized()
|
2024-05-06 16:21:00 +02:00
|
|
|
elif not current_user.is_in_role('user'):
|
2019-11-20 16:15:08 +01:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
2019-06-11 17:37:20 +02:00
|
|
|
|
2019-11-20 16:15:08 +01:00
|
|
|
def login_user_no_api(func):
|
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return login_manager.unauthorized()
|
2024-05-06 16:21:00 +02:00
|
|
|
elif not current_user.is_in_role('user_no_api'):
|
2019-11-20 16:15:08 +01:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
|
|
|
|
|
|
|
def login_read_only(func):
|
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return login_manager.unauthorized()
|
2024-05-06 16:21:00 +02:00
|
|
|
elif not current_user.is_in_role('read_only'):
|
2019-11-20 16:15:08 +01:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
2019-06-11 17:37:20 +02:00
|
|
|
|
|
|
|
###############################################################
|
|
|
|
###############################################################
|