2021-06-09 13:19:05 +01:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
from flask import abort, current_app
|
|
|
|
|
from flask_login import current_user, login_required
|
|
|
|
|
|
2023-05-19 13:54:27 -07:00
|
|
|
from app import config
|
2021-06-09 13:19:05 +01:00
|
|
|
|
|
|
|
|
user_is_logged_in = login_required
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def user_has_permissions(*permissions, **permission_kwargs):
|
|
|
|
|
def wrap(func):
|
|
|
|
|
@wraps(func)
|
|
|
|
|
def wrap_func(*args, **kwargs):
|
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
if not current_user.has_permissions(*permissions, **permission_kwargs):
|
|
|
|
|
abort(403)
|
|
|
|
|
return func(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
2021-06-09 13:19:05 +01:00
|
|
|
return wrap_func
|
2023-08-25 09:12:23 -07:00
|
|
|
|
2021-06-09 13:19:05 +01:00
|
|
|
return wrap
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def user_is_gov_user(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
if not current_user.is_gov_user:
|
|
|
|
|
abort(403)
|
|
|
|
|
return f(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
2021-06-09 13:19:05 +01:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def user_is_platform_admin(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
|
if not current_user.is_authenticated:
|
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
if not current_user.platform_admin:
|
|
|
|
|
abort(403)
|
|
|
|
|
return f(*args, **kwargs)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
2021-06-09 13:19:05 +01:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_gov_user(email_address):
|
|
|
|
|
return _email_address_ends_with(
|
2023-05-19 13:54:27 -07:00
|
|
|
email_address, config.Config.GOVERNMENT_EMAIL_DOMAIN_NAMES
|
2024-05-20 12:09:49 -07:00
|
|
|
) # or _email_address_ends_with(email_address, organizations_client.get_domains())
|
2021-06-09 13:19:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _email_address_ends_with(email_address, known_domains):
|
|
|
|
|
return any(
|
2023-08-25 09:12:23 -07:00
|
|
|
email_address.lower().endswith(
|
|
|
|
|
(
|
|
|
|
|
"@{}".format(known),
|
|
|
|
|
".{}".format(known),
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-06-09 13:19:05 +01:00
|
|
|
for known in known_domains
|
|
|
|
|
)
|
2021-07-13 15:26:19 +01:00
|
|
|
|
|
|
|
|
|
2022-10-04 03:04:13 +00:00
|
|
|
# def normalise_email_address_aliases(email_address):
|
|
|
|
|
# local_part, domain = email_address.split('@')
|
|
|
|
|
# local_part = local_part.split('+')[0].replace('.', '')
|
2021-07-13 15:26:19 +01:00
|
|
|
|
2022-10-04 03:04:13 +00:00
|
|
|
# return f'{local_part}@{domain}'.lower()
|
2021-07-13 15:26:19 +01:00
|
|
|
|
|
|
|
|
|
2022-10-04 03:04:13 +00:00
|
|
|
# def distinct_email_addresses(*args):
|
|
|
|
|
# return len(args) == len(set(map(normalise_email_address_aliases, args)))
|