2019-05-06 14:58:36 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2019-06-11 15:37:20 +00:00
|
|
|
import os
|
2019-11-05 14:18:03 +00:00
|
|
|
import sys
|
|
|
|
|
2019-05-06 14:58:36 +00:00
|
|
|
from functools import wraps
|
2024-08-13 12:59:30 +00:00
|
|
|
from flask_login import LoginManager, current_user, logout_user, login_required
|
2019-05-06 14:58:36 +00:00
|
|
|
|
2024-08-13 12:59:30 +00:00
|
|
|
from flask import make_response, current_app
|
2019-05-06 14:58:36 +00:00
|
|
|
|
|
|
|
login_manager = LoginManager()
|
2019-11-20 15:15:08 +00:00
|
|
|
login_manager.login_view = 'root.role'
|
2019-05-06 14:58:36 +00:00
|
|
|
|
2019-09-03 09:58:34 +00:00
|
|
|
###############################################################
|
2019-09-03 10:00:24 +00:00
|
|
|
############### FLASK CACHE ##################
|
2019-09-03 09:58:34 +00: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 13:00:25 +00:00
|
|
|
###############################################################
|
|
|
|
############### CHECK ROLE ACCESS ##################
|
|
|
|
###############################################################
|
|
|
|
|
2019-05-06 14:58:36 +00: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 14:21:00 +00:00
|
|
|
elif not current_user.is_in_role('admin'):
|
2019-05-06 14:58:36 +00:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
|
|
|
|
2024-09-05 14:40:24 +00:00
|
|
|
def login_coordinator(func):
|
2019-05-06 14:58:36 +00:00
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return login_manager.unauthorized()
|
2024-09-05 14:40:24 +00:00
|
|
|
elif not current_user.is_in_role('coordinator'):
|
2019-05-06 14:58:36 +00:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
2019-06-11 15:37:20 +00:00
|
|
|
|
2019-11-20 15:15:08 +00: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 14:21:00 +00:00
|
|
|
elif not current_user.is_in_role('user'):
|
2019-11-20 15:15:08 +00:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
2019-06-11 15:37:20 +00:00
|
|
|
|
2019-11-20 15:15:08 +00: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 14:21:00 +00:00
|
|
|
elif not current_user.is_in_role('user_no_api'):
|
2019-11-20 15:15:08 +00: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 14:21:00 +00:00
|
|
|
elif not current_user.is_in_role('read_only'):
|
2019-11-20 15:15:08 +00:00
|
|
|
return login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorated_view
|
2019-06-11 15:37:20 +00:00
|
|
|
|
|
|
|
###############################################################
|
|
|
|
###############################################################
|