Mail aliases/forward fix

This commit is contained in:
Kload 2013-06-08 09:50:43 +02:00
parent 2de2a2433e
commit f278b3b6a0
2 changed files with 42 additions and 53 deletions

View file

@ -121,19 +121,19 @@ user:
metavar: PASSWORD metavar: PASSWORD
--add-mailforward: --add-mailforward:
help: Mailforward addresses to add help: Mailforward addresses to add
nargs: "+" nargs: "*"
metavar: MAIL metavar: MAIL
--remove-mailforward: --remove-mailforward:
help: Mailforward addresses to remove help: Mailforward addresses to remove
nargs: "+" nargs: "*"
metavar: MAIL metavar: MAIL
--add-mailalias: --add-mailalias:
help: Mail aliases to add help: Mail aliases to add
nargs: "+" nargs: "*"
metavar: MAIL metavar: MAIL
--remove-mailalias: --remove-mailalias:
help: Mail aliases to remove help: Mail aliases to remove
nargs: "+" nargs: "*"
metavar: MAIL metavar: MAIL
### user_info() ### user_info()

View file

@ -23,7 +23,7 @@ def user_list(fields=None, filter=None, limit=None, offset=None):
Dict Dict
""" """
with YunoHostLDAP() as yldap: with YunoHostLDAP() as yldap:
user_attrs = ['uid', 'mail', 'cn', 'mailalias'] user_attrs = ['uid', 'mail', 'cn', 'maildrop']
attrs = [] attrs = []
result_list = [] result_list = []
if offset: offset = int(offset) if offset: offset = int(offset)
@ -53,9 +53,9 @@ def user_list(fields=None, filter=None, limit=None, offset=None):
'Mail': user['mail'][0] 'Mail': user['mail'][0]
} }
if len(user['mail']) > 1: if len(user['mail']) > 1:
entry['Mail Forward'] = user['mail'][1:] entry['Mail Aliases'] = user['mail'][1:]
if 'mailalias' in user: if 'maildrop' in user:
entry['Mail Aliases'] = user['mailalias'] entry['Mail Forward'] = user['maildrop']
result_list.append(entry) result_list.append(entry)
i += 1 i += 1
@ -85,8 +85,7 @@ def user_create(username, firstname, lastname, mail, password):
yldap.validate_uniqueness({ yldap.validate_uniqueness({
'uid' : username, 'uid' : username,
'mail' : mail, 'mail' : mail
'mailalias' : mail
}) })
# Check if unix user already exists (doesn't work) # Check if unix user already exists (doesn't work)
@ -117,6 +116,7 @@ def user_create(username, firstname, lastname, mail, password):
'cn' : fullname, 'cn' : fullname,
'uid' : username, 'uid' : username,
'mail' : mail, 'mail' : mail,
'maildrop' : username,
'userPassword' : pwd, 'userPassword' : pwd,
'gidNumber' : uid, 'gidNumber' : uid,
'uidNumber' : uid, 'uidNumber' : uid,
@ -185,7 +185,7 @@ def user_update(username, firstname=None, lastname=None, mail=None, change_passw
Dict Dict
""" """
with YunoHostLDAP() as yldap: with YunoHostLDAP() as yldap:
attrs_to_fetch = ['givenName', 'sn', 'mail', 'mailAlias'] attrs_to_fetch = ['givenName', 'sn', 'mail', 'maildrop']
new_attr_dict = {} new_attr_dict = {}
# Populate user informations # Populate user informations
@ -213,57 +213,46 @@ def user_update(username, firstname=None, lastname=None, mail=None, change_passw
new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(change_password), salt) new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(change_password), salt)
if mail: if mail:
yldap.validate_uniqueness({ yldap.validate_uniqueness({ 'mail': mail })
'mail' : mail,
'mailalias' : mail
})
del user['mail'][0] del user['mail'][0]
new_attr_dict['mail'] = [mail] + user['mail'] new_attr_dict['mail'] = [mail] + user['mail']
if add_mailforward:
if not isinstance(add_mailforward, list):
add_mailforward = [ add_mailforward ]
for mail in add_mailforward:
yldap.validate_uniqueness({
'mail' : mail,
'mailalias' : mail
})
user['mail'].append(mail)
new_attr_dict['mail'] = user['mail']
if remove_mailforward:
if not isinstance(remove_mailforward, list):
remove_mailforward = [ remove_mailforward ]
for mail in remove_mailforward:
if len(user['mail']) > 1 and mail in user['mail'][1:]:
user['mail'].remove(mail)
else:
raise YunoHostError(22, _("Invalid mail forward : ") + mail)
new_attr_dict['mail'] = user['mail']
if add_mailalias: if add_mailalias:
if not isinstance(add_mailalias, list): if not isinstance(add_mailalias, list):
add_mailalias = [ add_mailalias ] add_mailalias = [ add_mailalias ]
for mail in add_mailalias: for mail in add_mailalias:
yldap.validate_uniqueness({ yldap.validate_uniqueness({ 'mail': mail })
'mail' : mail, user['mail'].append(mail)
'mailalias' : mail new_attr_dict['mail'] = user['mail']
})
if 'mailalias' in user:
user['mailalias'].append(mail)
else:
user['mailalias'] = [ mail ]
new_attr_dict['mailalias'] = user['mailalias']
if remove_mailalias: if remove_mailalias:
if not isinstance(remove_mailalias, list): if not isinstance(remove_mailalias, list):
remove_mailalias = [ remove_mailalias ] remove_mailalias = [ remove_mailalias ]
for mail in remove_mailalias: for mail in remove_mailalias:
if 'mailalias' in user and mail in user['mailalias']: if len(user['mail']) > 1 and mail in user['mail'][1:]:
user['mailalias'].remove(mail) user['mail'].remove(mail)
else: else:
raise YunoHostError(22, _("Invalid mail alias : ") + mail) raise YunoHostError(22, _("Invalid mail alias : ") + mail)
new_attr_dict['mailalias'] = user['mailalias'] new_attr_dict['mail'] = user['mail']
if add_mailforward:
if not isinstance(add_mailforward, list):
add_mailforward = [ add_mailforward ]
for mail in add_mailforward:
if mail in user['maildrop'][1:]:
continue
user['maildrop'].append(mail)
new_attr_dict['maildrop'] = user['maildrop']
if remove_mailforward:
if not isinstance(remove_mailforward, list):
remove_mailforward = [ remove_mailforward ]
for mail in remove_mailforward:
if len(user['maildrop']) > 1 and mail in user['maildrop'][1:]:
user['maildrop'].remove(mail)
else:
raise YunoHostError(22, _("Invalid mail forward : ") + mail)
new_attr_dict['maildrop'] = user['maildrop']
if yldap.update('uid=' + username + ',ou=users', new_attr_dict): if yldap.update('uid=' + username + ',ou=users', new_attr_dict):
win_msg(_("User successfully updated")) win_msg(_("User successfully updated"))
@ -285,10 +274,10 @@ def user_info(user_or_mail):
Dict Dict
""" """
with YunoHostLDAP() as yldap: with YunoHostLDAP() as yldap:
user_attrs = ['cn', 'mail', 'uid', 'mailAlias'] user_attrs = ['cn', 'mail', 'uid', 'maildrop']
if len(user_or_mail.split('@')) is 2: if len(user_or_mail.split('@')) is 2:
filter = '(|(mail='+ user_or_mail +')(mailalias='+ user_or_mail +'))' filter = 'mail='+ user_or_mail
else: else:
filter = 'uid='+ user_or_mail filter = 'uid='+ user_or_mail
@ -306,10 +295,10 @@ def user_info(user_or_mail):
} }
if len(user['mail']) > 1: if len(user['mail']) > 1:
result_dict['Mail Forward'] = user['mail'][1:] result_dict['Mail Aliases'] = user['mail'][1:]
if 'mailalias' in user: if len(user['maildrop']) > 1:
result_dict['Mail Aliases'] = user['mailalias'] result_dict['Mail Forward'] = user['maildrop'][1:]
if result: if result:
return result_dict return result_dict