mirror of
https://github.com/ail-project/ail-framework.git
synced 2024-11-27 08:17:17 +00:00
88 lines
3 KiB
Python
88 lines
3 KiB
Python
#!/usr/bin/env python3
|
|
# -*-coding:UTF-8 -*
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
import redis
|
|
import bcrypt
|
|
|
|
from functools import wraps
|
|
from flask_login import LoginManager, current_user, login_user, logout_user, login_required
|
|
|
|
from flask import request, make_response, current_app
|
|
|
|
login_manager = LoginManager()
|
|
login_manager.login_view = 'root.role'
|
|
|
|
###############################################################
|
|
############### FLASK CACHE ##################
|
|
###############################################################
|
|
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
|
|
###############################################################
|
|
###############################################################
|
|
###############################################################
|
|
|
|
###############################################################
|
|
############### CHECK ROLE ACCESS ##################
|
|
###############################################################
|
|
|
|
def login_admin(func):
|
|
@wraps(func)
|
|
def decorated_view(*args, **kwargs):
|
|
if not current_user.is_authenticated:
|
|
return login_manager.unauthorized()
|
|
elif not current_user.is_in_role('admin'):
|
|
return login_manager.unauthorized()
|
|
return func(*args, **kwargs)
|
|
return decorated_view
|
|
|
|
def login_analyst(func):
|
|
@wraps(func)
|
|
def decorated_view(*args, **kwargs):
|
|
if not current_user.is_authenticated:
|
|
return login_manager.unauthorized()
|
|
elif not current_user.is_in_role('analyst'):
|
|
return login_manager.unauthorized()
|
|
return func(*args, **kwargs)
|
|
return decorated_view
|
|
|
|
def login_user(func):
|
|
@wraps(func)
|
|
def decorated_view(*args, **kwargs):
|
|
if not current_user.is_authenticated:
|
|
return login_manager.unauthorized()
|
|
elif not current_user.is_in_role('user'):
|
|
return login_manager.unauthorized()
|
|
return func(*args, **kwargs)
|
|
return decorated_view
|
|
|
|
def login_user_no_api(func):
|
|
@wraps(func)
|
|
def decorated_view(*args, **kwargs):
|
|
if not current_user.is_authenticated:
|
|
return login_manager.unauthorized()
|
|
elif not current_user.is_in_role('user_no_api'):
|
|
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()
|
|
elif not current_user.is_in_role('read_only'):
|
|
return login_manager.unauthorized()
|
|
return func(*args, **kwargs)
|
|
return decorated_view
|
|
|
|
###############################################################
|
|
###############################################################
|