aboutsummaryrefslogtreecommitdiff
path: root/gn3/auth/authorisation/users/admin/ui.py
blob: 242c7a6a4a7e2ccb89c3f44021808de55ed4b3f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""UI utilities for the auth system."""
from functools import wraps
from flask import flash, url_for, redirect

from gn3.auth.authentication.users import User
from gn3.auth.db_utils import with_db_connection
from gn3.auth.authorisation.roles.models import user_roles

from gn3.session import logged_in, session_user, clear_session_info

def is_admin(func):
    """Verify user is a system admin."""
    @wraps(func)
    @logged_in
    def __admin__(*args, **kwargs):
        admin_roles = [
            role for role in with_db_connection(
                lambda conn: user_roles(
                    conn, User(**session_user())))
            if role.role_name == "system-administrator"]
        if len(admin_roles) > 0:
            return func(*args, **kwargs)
        flash("Expected a system administrator.", "alert-danger")
        flash("You have been logged out of the system.", "alert-info")
        clear_session_info()
        return redirect(url_for("oauth2.admin.login"))
    return __admin__