mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Pass argument namespace & refactor user function
This commit is contained in:
parent
77d3e24ddc
commit
2ae505b69d
3 changed files with 97 additions and 77 deletions
|
@ -92,8 +92,11 @@ user:
|
||||||
delete:
|
delete:
|
||||||
action_help: Delete user
|
action_help: Delete user
|
||||||
arguments:
|
arguments:
|
||||||
users:
|
-u:
|
||||||
|
full: --users
|
||||||
help: Username of users to delete
|
help: Username of users to delete
|
||||||
|
ask: "Users to delete"
|
||||||
|
pattern: '^[a-z0-9_]+$'
|
||||||
nargs: "*"
|
nargs: "*"
|
||||||
--purge:
|
--purge:
|
||||||
action: store_true
|
action: store_true
|
||||||
|
@ -102,7 +105,7 @@ user:
|
||||||
update:
|
update:
|
||||||
action_help: Update user informations
|
action_help: Update user informations
|
||||||
arguments:
|
arguments:
|
||||||
user:
|
username:
|
||||||
help: Username of user to update
|
help: Username of user to update
|
||||||
-f:
|
-f:
|
||||||
full: --firstname
|
full: --firstname
|
||||||
|
@ -136,7 +139,9 @@ user:
|
||||||
action_help: Get user informations
|
action_help: Get user informations
|
||||||
arguments:
|
arguments:
|
||||||
-u:
|
-u:
|
||||||
full: --user
|
full: --username
|
||||||
|
ask: "Username"
|
||||||
|
pattern: '^[a-z0-9_]+$'
|
||||||
-m:
|
-m:
|
||||||
full: --mail
|
full: --mail
|
||||||
|
|
||||||
|
|
|
@ -166,7 +166,7 @@ def main():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
args = parse_dict(action_map)
|
args = parse_dict(action_map)
|
||||||
result = args.func(vars(args))
|
result = args.func(args)
|
||||||
except TypeError, error:
|
except TypeError, error:
|
||||||
if not __debug__ :
|
if not __debug__ :
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
161
yunohost_user.py
161
yunohost_user.py
|
@ -9,12 +9,15 @@ import string
|
||||||
import getpass
|
import getpass
|
||||||
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
|
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
|
||||||
|
|
||||||
def user_list(args):
|
def user_list(fields=None, filter=None, limit=None, offset=None):
|
||||||
"""
|
"""
|
||||||
List YunoHost users from LDAP
|
List YunoHost users from LDAP
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
args -- Dictionnary of values (can be empty)
|
fields -- Fields to fetch
|
||||||
|
filter -- LDAP filter to use
|
||||||
|
limit -- Number of user to fetch
|
||||||
|
offset -- User number to begin with
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict
|
Dict
|
||||||
|
@ -23,14 +26,13 @@ def user_list(args):
|
||||||
user_attrs = ['uid', 'mail', 'cn', 'mailalias']
|
user_attrs = ['uid', 'mail', 'cn', 'mailalias']
|
||||||
attrs = []
|
attrs = []
|
||||||
result_dict = {}
|
result_dict = {}
|
||||||
if args['offset']: offset = int(args['offset'])
|
if offset: offset = int(offset)
|
||||||
else: offset = 0
|
else: offset = 0
|
||||||
if args['limit']: limit = int(args['limit'])
|
if limit: limit = int(limit)
|
||||||
else: limit = 1000
|
else: limit = 1000
|
||||||
if args['filter']: filter = args['filter']
|
if not filter: filter = 'uid=*'
|
||||||
else: filter = 'uid=*'
|
if fields:
|
||||||
if args['fields']:
|
for attr in fields:
|
||||||
for attr in args['fields']:
|
|
||||||
if attr in user_attrs:
|
if attr in user_attrs:
|
||||||
attrs.append(attr)
|
attrs.append(attr)
|
||||||
continue
|
continue
|
||||||
|
@ -63,29 +65,32 @@ def user_list(args):
|
||||||
return result_dict
|
return result_dict
|
||||||
|
|
||||||
|
|
||||||
def user_create(args):
|
def user_create(username=None, firstname=None, lastname=None, mail=None, password=None):
|
||||||
"""
|
"""
|
||||||
Add user to LDAP
|
Add user to LDAP
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
args -- Dictionnary of values (can be empty)
|
username
|
||||||
|
firstname
|
||||||
|
lastname
|
||||||
|
password
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict
|
Dict
|
||||||
"""
|
"""
|
||||||
with YunoHostLDAP() as yldap:
|
with YunoHostLDAP() as yldap:
|
||||||
# Validate password length
|
# Validate password length
|
||||||
if len(args['password']) < 4:
|
if len(password) < 4:
|
||||||
raise YunoHostError(22, _("Password is too short"))
|
raise YunoHostError(22, _("Password is too short"))
|
||||||
|
|
||||||
yldap.validate_uniqueness({
|
yldap.validate_uniqueness({
|
||||||
'uid' : args['username'],
|
'uid' : username,
|
||||||
'mail' : args['mail'],
|
'mail' : mail,
|
||||||
'mailalias' : args['mail']
|
'mailalias' : mail
|
||||||
})
|
})
|
||||||
|
|
||||||
# Check if unix user already exists (doesn't work)
|
# Check if unix user already exists (doesn't work)
|
||||||
#if not os.system("getent passwd " + args['username']):
|
#if not os.system("getent passwd " + username):
|
||||||
# raise YunoHostError(17, _("Username not available"))
|
# raise YunoHostError(17, _("Username not available"))
|
||||||
|
|
||||||
#TODO: check if mail belongs to a domain
|
#TODO: check if mail belongs to a domain
|
||||||
|
@ -98,43 +103,44 @@ def user_create(args):
|
||||||
gid_check = os.system("getent group " + uid)
|
gid_check = os.system("getent group " + uid)
|
||||||
|
|
||||||
# Adapt values for LDAP
|
# Adapt values for LDAP
|
||||||
fullname = args['firstname'] + ' ' + args['lastname']
|
fullname = firstname + ' ' + lastname
|
||||||
rdn = 'uid=' + args['username'] + ',ou=users'
|
rdn = 'uid=' + username + ',ou=users'
|
||||||
char_set = string.ascii_uppercase + string.digits
|
char_set = string.ascii_uppercase + string.digits
|
||||||
salt = ''.join(random.sample(char_set,8))
|
salt = ''.join(random.sample(char_set,8))
|
||||||
salt = '$1$' + salt + '$'
|
salt = '$1$' + salt + '$'
|
||||||
pwd = '{CRYPT}' + crypt.crypt(str(args['password']), salt)
|
pwd = '{CRYPT}' + crypt.crypt(str(password), salt)
|
||||||
attr_dict = {
|
attr_dict = {
|
||||||
'objectClass' : ['mailAccount', 'inetOrgPerson', 'posixAccount'],
|
'objectClass' : ['mailAccount', 'inetOrgPerson', 'posixAccount'],
|
||||||
'givenName' : args['firstname'],
|
'givenName' : firstname,
|
||||||
'sn' : args['lastname'],
|
'sn' : lastname,
|
||||||
'displayName' : fullname,
|
'displayName' : fullname,
|
||||||
'cn' : fullname,
|
'cn' : fullname,
|
||||||
'uid' : args['username'],
|
'uid' : username,
|
||||||
'mail' : args['mail'],
|
'mail' : mail,
|
||||||
'userPassword' : pwd,
|
'userPassword' : pwd,
|
||||||
'gidNumber' : uid,
|
'gidNumber' : uid,
|
||||||
'uidNumber' : uid,
|
'uidNumber' : uid,
|
||||||
'homeDirectory' : '/home/' + args['username'],
|
'homeDirectory' : '/home/' + username,
|
||||||
'loginShell' : '/bin/false'
|
'loginShell' : '/bin/false'
|
||||||
}
|
}
|
||||||
|
|
||||||
if yldap.add(rdn, attr_dict):
|
if yldap.add(rdn, attr_dict):
|
||||||
# Create user /home directory by switching user
|
# Create user /home directory by switching user
|
||||||
os.system("su - " + args['username'] + " -c ''")
|
os.system("su - " + username + " -c ''")
|
||||||
#TODO: Send a welcome mail to user
|
#TODO: Send a welcome mail to user
|
||||||
win_msg(_("User successfully created"))
|
win_msg(_("User successfully created"))
|
||||||
return { _("Fullname") : fullname, _("Username") : args['username'], _("Mail") : args['mail'] }
|
return { _("Fullname") : fullname, _("Username") : username, _("Mail") : mail }
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(169, _("An error occured during user creation"))
|
raise YunoHostError(169, _("An error occured during user creation"))
|
||||||
|
|
||||||
|
|
||||||
def user_delete(args):
|
def user_delete(users=None, purge=None):
|
||||||
"""
|
"""
|
||||||
Remove user from LDAP
|
Remove user from LDAP
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
args -- Dictionnary of values (can be empty)
|
users -- List of users to delete or single user
|
||||||
|
purge -- Whether or not purge /home/user directory
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict
|
Dict
|
||||||
|
@ -142,13 +148,12 @@ def user_delete(args):
|
||||||
with YunoHostLDAP() as yldap:
|
with YunoHostLDAP() as yldap:
|
||||||
result = { 'Users' : [] }
|
result = { 'Users' : [] }
|
||||||
|
|
||||||
args = get_required_args(args, { 'users' : _('User to delete') })
|
if not isinstance(users, list):
|
||||||
if not isinstance(args['users'], list):
|
users = [ users ]
|
||||||
args['users'] = [ args['users'] ]
|
|
||||||
|
|
||||||
for user in args['users']:
|
for user in users:
|
||||||
if yldap.remove('uid=' + user+ ',ou=users'):
|
if yldap.remove('uid=' + user+ ',ou=users'):
|
||||||
if args['purge']:
|
if purge:
|
||||||
os.system('rm -rf /home/' + user)
|
os.system('rm -rf /home/' + user)
|
||||||
result['Users'].append(user)
|
result['Users'].append(user)
|
||||||
continue
|
continue
|
||||||
|
@ -159,12 +164,22 @@ def user_delete(args):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def user_update(args):
|
def user_update(username, firstname=None, lastname=None, mail=None, change_password=None,
|
||||||
|
add_mailforward=None, remove_mailforward=None,
|
||||||
|
add_mailalias=None, remove_mailalias=None):
|
||||||
"""
|
"""
|
||||||
Update user informations
|
Update user informations
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
args -- Dictionnary of values
|
username -- Username to update
|
||||||
|
firstname
|
||||||
|
lastname
|
||||||
|
mail
|
||||||
|
change_password -- New password
|
||||||
|
add_mailforward
|
||||||
|
remove_mailforward
|
||||||
|
add_mailalias
|
||||||
|
remove_mailalias
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict
|
Dict
|
||||||
|
@ -174,41 +189,41 @@ def user_update(args):
|
||||||
new_attr_dict = {}
|
new_attr_dict = {}
|
||||||
|
|
||||||
# Populate user informations
|
# Populate user informations
|
||||||
result = yldap.search(base='ou=users,dc=yunohost,dc=org', filter='uid=' + args['user'], attrs=attrs_to_fetch)
|
result = yldap.search(base='ou=users,dc=yunohost,dc=org', filter='uid=' + username, attrs=attrs_to_fetch)
|
||||||
if not result:
|
if not result:
|
||||||
raise YunoHostError(167, _("No user found"))
|
raise YunoHostError(167, _("No user found"))
|
||||||
user = result[0]
|
user = result[0]
|
||||||
|
|
||||||
# Get modifications from arguments
|
# Get modifications from arguments
|
||||||
if args['firstname']:
|
if firstname:
|
||||||
new_attr_dict['givenName'] = args['firstname'] # TODO: Validate
|
new_attr_dict['givenName'] = firstname # TODO: Validate
|
||||||
new_attr_dict['cn'] = new_attr_dict['displayName'] = args['firstname'] + ' ' + user['sn'][0]
|
new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + user['sn'][0]
|
||||||
|
|
||||||
if args['lastname']:
|
if lastname:
|
||||||
new_attr_dict['sn'] = args['lastname'] # TODO: Validate
|
new_attr_dict['sn'] = lastname # TODO: Validate
|
||||||
new_attr_dict['cn'] = new_attr_dict['displayName'] = user['givenName'][0] + ' ' + args['lastname']
|
new_attr_dict['cn'] = new_attr_dict['displayName'] = user['givenName'][0] + ' ' + lastname
|
||||||
|
|
||||||
if args['lastname'] and args['firstname']:
|
if lastname and firstname:
|
||||||
new_attr_dict['cn'] = new_attr_dict['displayName'] = args['firstname'] + ' ' + args['lastname']
|
new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + lastname
|
||||||
|
|
||||||
if args['change_password']:
|
if change_password:
|
||||||
char_set = string.ascii_uppercase + string.digits
|
char_set = string.ascii_uppercase + string.digits
|
||||||
salt = ''.join(random.sample(char_set,8))
|
salt = ''.join(random.sample(char_set,8))
|
||||||
salt = '$1$' + salt + '$'
|
salt = '$1$' + salt + '$'
|
||||||
new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(args['change_password']), salt)
|
new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(change_password), salt)
|
||||||
|
|
||||||
if args['mail']:
|
if mail:
|
||||||
yldap.validate_uniqueness({
|
yldap.validate_uniqueness({
|
||||||
'mail' : args['mail'],
|
'mail' : mail,
|
||||||
'mailalias' : args['mail']
|
'mailalias' : mail
|
||||||
})
|
})
|
||||||
del user['mail'][0]
|
del user['mail'][0]
|
||||||
new_attr_dict['mail'] = [args['mail']] + user['mail']
|
new_attr_dict['mail'] = [mail] + user['mail']
|
||||||
|
|
||||||
if args['add_mailforward']:
|
if add_mailforward:
|
||||||
if not isinstance(args['add_mailforward'], list):
|
if not isinstance(add_mailforward, list):
|
||||||
args['add_mailforward'] = [ args['add_mailforward'] ]
|
add_mailforward = [ add_mailforward ]
|
||||||
for mail in args['add_mailforward']:
|
for mail in add_mailforward:
|
||||||
yldap.validate_uniqueness({
|
yldap.validate_uniqueness({
|
||||||
'mail' : mail,
|
'mail' : mail,
|
||||||
'mailalias' : mail
|
'mailalias' : mail
|
||||||
|
@ -216,20 +231,20 @@ def user_update(args):
|
||||||
user['mail'].append(mail)
|
user['mail'].append(mail)
|
||||||
new_attr_dict['mail'] = user['mail']
|
new_attr_dict['mail'] = user['mail']
|
||||||
|
|
||||||
if args['remove_mailforward']:
|
if remove_mailforward:
|
||||||
if not isinstance(args['remove_mailforward'], list):
|
if not isinstance(remove_mailforward, list):
|
||||||
args['remove_mailforward'] = [ args['remove_mailforward'] ]
|
remove_mailforward = [ remove_mailforward ]
|
||||||
for mail in args['remove_mailforward']:
|
for mail in remove_mailforward:
|
||||||
if len(user['mail']) > 1 and mail in user['mail'][1:]:
|
if len(user['mail']) > 1 and mail in user['mail'][1:]:
|
||||||
user['mail'].remove(mail)
|
user['mail'].remove(mail)
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(22, _("Invalid mail forward : ") + mail)
|
raise YunoHostError(22, _("Invalid mail forward : ") + mail)
|
||||||
new_attr_dict['mail'] = user['mail']
|
new_attr_dict['mail'] = user['mail']
|
||||||
|
|
||||||
if args['add_mailalias']:
|
if add_mailalias:
|
||||||
if not isinstance(args['add_mailalias'], list):
|
if not isinstance(add_mailalias, list):
|
||||||
args['add_mailalias'] = [ args['add_mailalias'] ]
|
add_mailalias = [ add_mailalias ]
|
||||||
for mail in args['add_mailalias']:
|
for mail in add_mailalias:
|
||||||
yldap.validate_uniqueness({
|
yldap.validate_uniqueness({
|
||||||
'mail' : mail,
|
'mail' : mail,
|
||||||
'mailalias' : mail
|
'mailalias' : mail
|
||||||
|
@ -240,30 +255,31 @@ def user_update(args):
|
||||||
user['mailalias'] = [ mail ]
|
user['mailalias'] = [ mail ]
|
||||||
new_attr_dict['mailalias'] = user['mailalias']
|
new_attr_dict['mailalias'] = user['mailalias']
|
||||||
|
|
||||||
if args['remove_mailalias']:
|
if remove_mailalias:
|
||||||
if not isinstance(args['remove_mailalias'], list):
|
if not isinstance(remove_mailalias, list):
|
||||||
args['remove_mailalias'] = [ args['remove_mailalias'] ]
|
remove_mailalias = [ remove_mailalias ]
|
||||||
for mail in args['remove_mailalias']:
|
for mail in remove_mailalias:
|
||||||
if 'mailalias' in user and mail in user['mailalias']:
|
if 'mailalias' in user and mail in user['mailalias']:
|
||||||
user['mailalias'].remove(mail)
|
user['mailalias'].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['mailalias'] = user['mailalias']
|
||||||
|
|
||||||
if yldap.update('uid=' + args['user'] + ',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"))
|
||||||
return user_info({ 'user' : args['user'], 'mail' : None }, connections)
|
return user_info(username=username)
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(169, _("An error occured during user update"))
|
raise YunoHostError(169, _("An error occured during user update"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def user_info(args):
|
def user_info(username=None, mail=None):
|
||||||
"""
|
"""
|
||||||
Fetch user informations from LDAP
|
Fetch user informations from LDAP
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
args -- Dictionnary of values (can be empty)
|
username
|
||||||
|
mail
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict
|
Dict
|
||||||
|
@ -271,11 +287,10 @@ def user_info(args):
|
||||||
with YunoHostLDAP() as yldap:
|
with YunoHostLDAP() as yldap:
|
||||||
user_attrs = ['cn', 'mail', 'uid', 'mailAlias']
|
user_attrs = ['cn', 'mail', 'uid', 'mailAlias']
|
||||||
|
|
||||||
if args['mail']:
|
if mail:
|
||||||
filter = 'mail=' + args['mail']
|
filter = 'mail=' + mail
|
||||||
else:
|
else:
|
||||||
args = get_required_args(args, { 'user' : _("Username") })
|
filter = 'uid=' + username
|
||||||
filter = 'uid=' + args['user']
|
|
||||||
|
|
||||||
result = yldap.search('ou=users,dc=yunohost,dc=org', filter, user_attrs)
|
result = yldap.search('ou=users,dc=yunohost,dc=org', filter, user_attrs)
|
||||||
user = result[0]
|
user = result[0]
|
||||||
|
|
Loading…
Add table
Reference in a new issue