1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2025-04-04 00:17:06 +00:00

Add a display name for users, saved as 'cn' in LDAP

This commit is contained in:
downtownallday 2020-08-25 16:33:06 -04:00
parent 05f4164793
commit 191b575ab2
3 changed files with 89 additions and 11 deletions

View File

@ -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():

View File

@ -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.

View File

@ -15,20 +15,30 @@
<form class="form-inline" role="form" onsubmit="return do_add_user(); return false;">
<div class="form-group">
<div>User (email address)</div>
<label class="sr-only" for="adduserEmail">Email address</label>
<input type="email" class="form-control" id="adduserEmail" placeholder="Email Address">
</div>
<div class="form-group">
<div>Password</div>
<label class="sr-only" for="adduserPassword">Password</label>
<input type="password" class="form-control" id="adduserPassword" placeholder="Password">
</div>
<div class="form-group">
<div>Privilege</div>
<select class="form-control" id="adduserPrivs">
<option value="">Normal User</option>
<option value="admin">Administrator</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Add User</button>
<div class="form-group">
<div>Display Name</div>
<input id="adduserDisplayName" class="form-control" type="text" placeholder="eg: John Smith">
</div>
<div class="text-center">
<div>&nbsp;</div>
<button type="submit" class="btn btn-primary">Add User</button>
</div>
</form>
<ul style="margin-top: 1em; padding-left: 1.5em; font-size: 90%;">
<li>Passwords must be at least eight characters consisting of English lettters and numbers only. For best results, <a href="#" onclick="return generate_random_password()">generate a random password</a>.</li>
@ -52,7 +62,8 @@
<div style="display: none">
<table>
<tr id="user-template">
<td class='address'>
<td>
<span class="address"></span> <span class="display_name_wrapper">(<a class="display_name" href="#" onclick="users_set_displayname(this); return false;" title="Change display name"></a>)</span>
</td>
<td class='actions'>
<span class='privs'>
@ -152,7 +163,13 @@ function show_users() {
n2.addClass("account_" + user.status);
n.attr('data-email', user.email);
n.find('.address').text(user.email)
n.find('.address').text(user.email);
if (user.status == "inactive") {
n.find('.display_name_wrapper').text('[archived]');
}
else {
n.find('.display_name').text(user.display_name);
}
n2.find('.restore_info tt').text(user.mailbox);
if (user.status == 'inactive') continue;
@ -181,13 +198,15 @@ function do_add_user() {
var email = $("#adduserEmail").val();
var pw = $("#adduserPassword").val();
var privs = $("#adduserPrivs").val();
var display_name = $("#adduserDisplayName").val();
api(
"/mail/users/add",
"POST",
{
email: email,
password: pw,
privileges: privs
privileges: privs,
display_name: display_name
},
function(r) {
// Responses are multiple lines of pre-formatted text.
@ -229,6 +248,31 @@ function users_set_password(elem) {
});
}
function users_set_displayname(elem) {
var email = $(elem).parents('tr').attr('data-email');
var display_name = $(elem).text();
show_modal_confirm(
"Change Display Name",
$('<p>Change display name of ' + email + ' (' + display_name + ')?</p> <p><label for="users_set_displayname_name">New display name:</label><input type="text" id="users_set_displayname_name" value="' + display_name + '" placeholder="eg: Mary Jay" onfocus="this.select();"></p>'),
"Change Display Name",
function() {
api("/mail/users/display_name",
"POST",
{
email: email,
display_name: $('#users_set_displayname_name').val()
},
function(r) {
// Responses are multiple lines of pre-formatted text.
show_modal_error("Change Display Name", $("<pre/>").text(r));
show_users();
},
function(r) {
show_modal_error("Change Display Name", r);
});
});
}
function users_remove(elem) {
var email = $(elem).parents('tr').attr('data-email');