mirror of
https://github.com/mail-in-a-box/mailinabox.git
synced 2025-04-05 00:27:25 +00:00
Add a display name for users, saved as 'cn' in LDAP
This commit is contained in:
parent
05f4164793
commit
191b575ab2
@ -6,7 +6,7 @@ from functools import wraps
|
|||||||
from flask import Flask, request, render_template, abort, Response, send_from_directory, make_response
|
from flask import Flask, request, render_template, abort, Response, send_from_directory, make_response
|
||||||
|
|
||||||
import auth, utils, multiprocessing.pool
|
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_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
|
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
|
@authorized_personnel_only
|
||||||
def mail_users_add():
|
def mail_users_add():
|
||||||
try:
|
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:
|
except ValueError as e:
|
||||||
return (str(e), 400)
|
return (str(e), 400)
|
||||||
|
|
||||||
@ -166,6 +166,14 @@ def mail_users_password():
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return (str(e), 400)
|
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'])
|
@app.route('/mail/users/remove', methods=['POST'])
|
||||||
@authorized_personnel_only
|
@authorized_personnel_only
|
||||||
def mail_users_remove():
|
def mail_users_remove():
|
||||||
|
@ -201,14 +201,15 @@ def get_mail_users(env, as_map=False):
|
|||||||
# is the user and value is a dict having, dn, maildrop and
|
# is the user and value is a dict having, dn, maildrop and
|
||||||
# mail addresses
|
# mail addresses
|
||||||
c = open_database(env)
|
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:
|
if as_map:
|
||||||
users = {}
|
users = {}
|
||||||
for rec in pager:
|
for rec in pager:
|
||||||
users[rec['maildrop'][0]] = {
|
users[rec['maildrop'][0]] = {
|
||||||
"dn": rec['dn'],
|
"dn": rec['dn'],
|
||||||
"mail": rec['mail'],
|
"mail": rec['mail'],
|
||||||
"maildrop": rec['maildrop'][0]
|
"maildrop": rec['maildrop'][0],
|
||||||
|
"display_name": rec['cn'][0]
|
||||||
}
|
}
|
||||||
return users
|
return users
|
||||||
else:
|
else:
|
||||||
@ -228,6 +229,7 @@ def get_mail_users_ex(env, with_archived=False):
|
|||||||
# email: "name@domain.tld",
|
# email: "name@domain.tld",
|
||||||
# privileges: [ "priv1", "priv2", ... ],
|
# privileges: [ "priv1", "priv2", ... ],
|
||||||
# status: "active" | "inactive",
|
# status: "active" | "inactive",
|
||||||
|
# display_name: ""
|
||||||
# },
|
# },
|
||||||
# ...
|
# ...
|
||||||
# ]
|
# ]
|
||||||
@ -239,16 +241,18 @@ def get_mail_users_ex(env, with_archived=False):
|
|||||||
users = []
|
users = []
|
||||||
active_accounts = set()
|
active_accounts = set()
|
||||||
c = open_database(env)
|
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:
|
for rec in response:
|
||||||
email = rec['maildrop'][0]
|
email = rec['maildrop'][0]
|
||||||
privileges = rec['mailaccess']
|
privileges = rec['mailaccess']
|
||||||
|
display_name = rec['cn'][0]
|
||||||
active_accounts.add(email)
|
active_accounts.add(email)
|
||||||
user = {
|
user = {
|
||||||
"email": email,
|
"email": email,
|
||||||
"privileges": privileges,
|
"privileges": privileges,
|
||||||
"status": "active",
|
"status": "active",
|
||||||
|
"display_name": display_name
|
||||||
}
|
}
|
||||||
users.append(user)
|
users.append(user)
|
||||||
|
|
||||||
@ -266,6 +270,7 @@ def get_mail_users_ex(env, with_archived=False):
|
|||||||
"privileges": [],
|
"privileges": [],
|
||||||
"status": "inactive",
|
"status": "inactive",
|
||||||
"mailbox": mbox,
|
"mailbox": mbox,
|
||||||
|
"display_name": ""
|
||||||
}
|
}
|
||||||
users.append(user)
|
users.append(user)
|
||||||
|
|
||||||
@ -615,13 +620,14 @@ def remove_mail_domain(env, domain, validate=True):
|
|||||||
return 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.
|
# Add a new mail user.
|
||||||
#
|
#
|
||||||
# email: the new user's email address
|
# email: the new user's email address
|
||||||
# pw: the new user's password
|
# pw: the new user's password
|
||||||
# privs: either an array of privilege strings, or a newline
|
# privs: either an array of privilege strings, or a newline
|
||||||
# separated string of privilege names
|
# 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,
|
# If an error occurs, the function returns a tuple of (message,
|
||||||
# http-status).
|
# http-status).
|
||||||
@ -673,6 +679,9 @@ def add_mail_user(email, pw, privs, env):
|
|||||||
uid = m.hexdigest()
|
uid = m.hexdigest()
|
||||||
|
|
||||||
# choose a common name and surname (required attributes)
|
# choose a common name and surname (required attributes)
|
||||||
|
if display_name:
|
||||||
|
cn = display_name
|
||||||
|
else:
|
||||||
cn = email.split("@")[0].replace('.',' ').replace('_',' ')
|
cn = email.split("@")[0].replace('.',' ').replace('_',' ')
|
||||||
sn = cn[cn.find(' ')+1:]
|
sn = cn[cn.find(' ')+1:]
|
||||||
|
|
||||||
@ -723,6 +732,23 @@ def set_mail_password(email, pw, env):
|
|||||||
|
|
||||||
return "OK"
|
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):
|
def validate_login(email, pw, env):
|
||||||
# Validate that `email` exists and has password `pw`.
|
# Validate that `email` exists and has password `pw`.
|
||||||
# Returns True if valid, or False if invalid.
|
# Returns True if valid, or False if invalid.
|
||||||
|
@ -15,20 +15,30 @@
|
|||||||
|
|
||||||
<form class="form-inline" role="form" onsubmit="return do_add_user(); return false;">
|
<form class="form-inline" role="form" onsubmit="return do_add_user(); return false;">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<div>User (email address)</div>
|
||||||
<label class="sr-only" for="adduserEmail">Email address</label>
|
<label class="sr-only" for="adduserEmail">Email address</label>
|
||||||
<input type="email" class="form-control" id="adduserEmail" placeholder="Email Address">
|
<input type="email" class="form-control" id="adduserEmail" placeholder="Email Address">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<div>Password</div>
|
||||||
<label class="sr-only" for="adduserPassword">Password</label>
|
<label class="sr-only" for="adduserPassword">Password</label>
|
||||||
<input type="password" class="form-control" id="adduserPassword" placeholder="Password">
|
<input type="password" class="form-control" id="adduserPassword" placeholder="Password">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<div>Privilege</div>
|
||||||
<select class="form-control" id="adduserPrivs">
|
<select class="form-control" id="adduserPrivs">
|
||||||
<option value="">Normal User</option>
|
<option value="">Normal User</option>
|
||||||
<option value="admin">Administrator</option>
|
<option value="admin">Administrator</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
<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> </div>
|
||||||
<button type="submit" class="btn btn-primary">Add User</button>
|
<button type="submit" class="btn btn-primary">Add User</button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<ul style="margin-top: 1em; padding-left: 1.5em; font-size: 90%;">
|
<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>
|
<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">
|
<div style="display: none">
|
||||||
<table>
|
<table>
|
||||||
<tr id="user-template">
|
<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>
|
||||||
<td class='actions'>
|
<td class='actions'>
|
||||||
<span class='privs'>
|
<span class='privs'>
|
||||||
@ -152,7 +163,13 @@ function show_users() {
|
|||||||
n2.addClass("account_" + user.status);
|
n2.addClass("account_" + user.status);
|
||||||
|
|
||||||
n.attr('data-email', user.email);
|
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);
|
n2.find('.restore_info tt').text(user.mailbox);
|
||||||
|
|
||||||
if (user.status == 'inactive') continue;
|
if (user.status == 'inactive') continue;
|
||||||
@ -181,13 +198,15 @@ function do_add_user() {
|
|||||||
var email = $("#adduserEmail").val();
|
var email = $("#adduserEmail").val();
|
||||||
var pw = $("#adduserPassword").val();
|
var pw = $("#adduserPassword").val();
|
||||||
var privs = $("#adduserPrivs").val();
|
var privs = $("#adduserPrivs").val();
|
||||||
|
var display_name = $("#adduserDisplayName").val();
|
||||||
api(
|
api(
|
||||||
"/mail/users/add",
|
"/mail/users/add",
|
||||||
"POST",
|
"POST",
|
||||||
{
|
{
|
||||||
email: email,
|
email: email,
|
||||||
password: pw,
|
password: pw,
|
||||||
privileges: privs
|
privileges: privs,
|
||||||
|
display_name: display_name
|
||||||
},
|
},
|
||||||
function(r) {
|
function(r) {
|
||||||
// Responses are multiple lines of pre-formatted text.
|
// 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) {
|
function users_remove(elem) {
|
||||||
var email = $(elem).parents('tr').attr('data-email');
|
var email = $(elem).parents('tr').attr('data-email');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user