add some group management

This commit is contained in:
Franzi 2021-12-21 09:27:25 +01:00
parent 602127cbdc
commit 026fbf3c58
Signed by: kunsi
GPG key ID: 12E3D2136B818350
6 changed files with 157 additions and 21 deletions

View file

@ -0,0 +1,38 @@
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

View file

@ -2,9 +2,8 @@ from functools import wraps
from json import load
from os import environ
from flask import redirect, session, url_for, render_template
from ldap3 import ALL, Connection, Server
from ldap3 import ALL_ATTRIBUTES, MODIFY_REPLACE
from flask import redirect, session, url_for
from ldap3 import ALL, ALL_ATTRIBUTES, MODIFY_REPLACE, Connection, Server
from ldap3.core.exceptions import LDAPException
with open(environ["APP_CONFIG"]) as f:
@ -21,7 +20,7 @@ def login_required(func):
):
ldap = connect()
return func(ldap, *args, **kwargs)
return func(ldap, **kwargs)
else:
return redirect(url_for("login"))
else:
@ -41,7 +40,16 @@ def admin_required(func):
):
ldap = connect()
return func(ldap, *args, **kwargs)
ldap.search(
APP_CONFIG["ldap"]["user_base"],
APP_CONFIG["template"]["group_admin"].format(session["username"]),
attributes=["uid"],
)
if len(ldap.entries) == 1:
return func(ldap, **kwargs)
else:
return redirect(url_for("selfservice"))
else:
return redirect(url_for("login"))
else:
@ -89,6 +97,7 @@ def get_user(ldap, username):
else:
raise UserNotFoundException(username)
def update_user(ldap, username, settings):
attrs = {}
for attr, value in settings.items():
@ -100,18 +109,5 @@ def update_user(ldap, username, settings):
)
def template(ldap, name, **kwargs):
user = None
if ldap:
user = get_user(ldap, session["username"])
return render_template(
name,
APP_CONFIG=APP_CONFIG,
CURRENT_USER=user,
**kwargs,
)
class UserNotFoundException(Exception):
pass