From 191b575ab28cf1fa4753211a7a95437be9618aeb Mon Sep 17 00:00:00 2001 From: downtownallday Date: Tue, 25 Aug 2020 16:33:06 -0400 Subject: [PATCH 1/4] Add a display name for users, saved as 'cn' in LDAP --- management/daemon.py | 12 ++++++-- management/mailconfig.py | 36 +++++++++++++++++++---- management/templates/users.html | 52 ++++++++++++++++++++++++++++++--- 3 files changed, 89 insertions(+), 11 deletions(-) diff --git a/management/daemon.py b/management/daemon.py index 56888fa2..46c84778 100755 --- a/management/daemon.py +++ b/management/daemon.py @@ -6,7 +6,7 @@ from functools import wraps from flask import Flask, request, render_template, abort, Response, send_from_directory, make_response import auth, utils, multiprocessing.pool -from mailconfig import get_mail_users, get_mail_users_ex, get_admins, add_mail_user, set_mail_password, remove_mail_user +from mailconfig import get_mail_users, get_mail_users_ex, get_admins, add_mail_user, set_mail_password, set_mail_display_name, remove_mail_user from mailconfig import get_mail_user_privileges, add_remove_mail_user_privilege from mailconfig import get_mail_aliases, get_mail_aliases_ex, get_mail_domains, add_mail_alias, remove_mail_alias @@ -154,7 +154,7 @@ def mail_users(): @authorized_personnel_only def mail_users_add(): try: - return add_mail_user(request.form.get('email', ''), request.form.get('password', ''), request.form.get('privileges', ''), env) + return add_mail_user(request.form.get('email', ''), request.form.get('password', ''), request.form.get('privileges', ''), request.form.get('display_name', ''), env) except ValueError as e: return (str(e), 400) @@ -166,6 +166,14 @@ def mail_users_password(): except ValueError as e: return (str(e), 400) +@app.route('/mail/users/display_name', methods=['POST']) +@authorized_personnel_only +def mail_users_display_name(): + try: + return set_mail_display_name(request.form.get('email', ''), request.form.get('display_name', ''), env) + except ValueError as e: + return (str(e), 400) + @app.route('/mail/users/remove', methods=['POST']) @authorized_personnel_only def mail_users_remove(): diff --git a/management/mailconfig.py b/management/mailconfig.py index 1e6cd79f..078ec809 100755 --- a/management/mailconfig.py +++ b/management/mailconfig.py @@ -201,14 +201,15 @@ def get_mail_users(env, as_map=False): # is the user and value is a dict having, dn, maildrop and # mail addresses c = open_database(env) - pager = c.paged_search(env.LDAP_USERS_BASE, "(objectClass=mailUser)", attributes=['maildrop','mail']) + pager = c.paged_search(env.LDAP_USERS_BASE, "(objectClass=mailUser)", attributes=['maildrop','mail','cn']) if as_map: users = {} for rec in pager: users[rec['maildrop'][0]] = { "dn": rec['dn'], "mail": rec['mail'], - "maildrop": rec['maildrop'][0] + "maildrop": rec['maildrop'][0], + "display_name": rec['cn'][0] } return users else: @@ -228,6 +229,7 @@ def get_mail_users_ex(env, with_archived=False): # email: "name@domain.tld", # privileges: [ "priv1", "priv2", ... ], # status: "active" | "inactive", + # display_name: "" # }, # ... # ] @@ -239,16 +241,18 @@ def get_mail_users_ex(env, with_archived=False): users = [] active_accounts = set() c = open_database(env) - response = c.wait( c.search(env.LDAP_USERS_BASE, "(objectClass=mailUser)", attributes=['maildrop','mailaccess']) ) + response = c.wait( c.search(env.LDAP_USERS_BASE, "(objectClass=mailUser)", attributes=['maildrop','mailaccess','cn']) ) for rec in response: email = rec['maildrop'][0] privileges = rec['mailaccess'] + display_name = rec['cn'][0] active_accounts.add(email) user = { "email": email, "privileges": privileges, "status": "active", + "display_name": display_name } users.append(user) @@ -266,6 +270,7 @@ def get_mail_users_ex(env, with_archived=False): "privileges": [], "status": "inactive", "mailbox": mbox, + "display_name": "" } users.append(user) @@ -615,13 +620,14 @@ def remove_mail_domain(env, domain, validate=True): return True -def add_mail_user(email, pw, privs, env): +def add_mail_user(email, pw, privs, display_name, env): # Add a new mail user. # # email: the new user's email address # pw: the new user's password # privs: either an array of privilege strings, or a newline # separated string of privilege names + # display_name: a string with users givenname and surname (eg "Al Woods") # # If an error occurs, the function returns a tuple of (message, # http-status). @@ -673,7 +679,10 @@ def add_mail_user(email, pw, privs, env): uid = m.hexdigest() # choose a common name and surname (required attributes) - cn = email.split("@")[0].replace('.',' ').replace('_',' ') + if display_name: + cn = display_name + else: + cn = email.split("@")[0].replace('.',' ').replace('_',' ') sn = cn[cn.find(' ')+1:] # compile user's attributes @@ -723,6 +732,23 @@ def set_mail_password(email, pw, env): return "OK" +def set_mail_display_name(email, display_name, env): + # validate arguments + if not display_name or display_name.strip() == "": + return ("Display name may not be empty!", 400) + + # find the user + conn = open_database(env) + user = find_mail_user(env, email, ['cn', 'sn'], conn) + if user is None: + return ("That's not a user (%s)." % email, 400) + + # update cn and sn + sn = display_name[display_name.strip().find(' ')+1:] + conn.modify_record(user, {'cn': display_name.strip(), 'sn': sn}) + + return "OK" + def validate_login(email, pw, env): # Validate that `email` exists and has password `pw`. # Returns True if valid, or False if invalid. diff --git a/management/templates/users.html b/management/templates/users.html index 78fef61a..f23adad2 100644 --- a/management/templates/users.html +++ b/management/templates/users.html @@ -15,20 +15,30 @@
+
User (email address)
+
Password
+
Privilege
- +
+
Display Name
+ +
+
+
 
+ +