ldap-frontend/ldap_frontend/helpers/flask.py

39 lines
820 B
Python
Raw Normal View History

2021-12-21 08:27:25 +00:00
from functools import wraps
from json import load
from os import environ
from flask import redirect, render_template, session, url_for
from .ldap import get_user
with open(environ["APP_CONFIG"]) as f:
APP_CONFIG = load(f)
def template(ldap, name, **kwargs):
user = None
is_admin = False
if ldap:
user = get_user(ldap, session["username"])
ldap.search(
APP_CONFIG["ldap"]["user_base"],
APP_CONFIG["template"]["group_admin"].format(user["uid"]),
attributes=["uid"],
)
if len(ldap.entries) == 1:
is_admin = True
return render_template(
name,
APP_CONFIG=APP_CONFIG,
CURRENT_USER=user,
USER_IS_ADMIN=is_admin,
**kwargs,
)
class UserNotFoundException(Exception):
pass