Add and delete users

This commit is contained in:
uid 2020-10-11 12:41:44 +02:00
parent ae81ec3885
commit 8b901ca5d5

View File

@ -1,21 +1,60 @@
#!/usr/bin/env python3
from pprint import pformat as pp
from getpass import getpass
from phi.config import get_config
from phi.logging import setup_logging, get_logger
from phi import cli
import phi.ldap.client
from phi.ldap.user import get_user_by_uid
from phi.ldap.user import get_user_by_uid, add_user, delete_user
log = get_logger(__name__)
@cli.register('dispaly user field', ['user identifier'])
@cli.register('dispaly user fields', ['user identifier'])
def showuser(uid):
user = get_user_by_uid(client, uid)
print('\nuid: {}\n{}\n\n'.format(uid, pp(user)))
@cli.register('add a new user', ['user identifier'])
def adduser(uid):
def ask(prompt, default):
full_prompt = '{} [{}] '.format(prompt, default)
return input(full_prompt) or default
cn = ask('Common name:', uid)
sn = ask('Last name:', uid)
mail = ask('Mail:', '{}@localhost'.format(uid))
password = getpass()
pass_check = getpass('Retype password: ')
if password != pass_check:
print('Password not matching')
return
add_user(client, uid, cn, sn, mail, password)
# Check
user = get_user_by_uid(client, uid)
print('\nuid: {}\n{}\n\n'.format(uid, pp(user)))
@cli.register('delete an unser', ['user identifier'])
def deluser(uid):
check = input('Are you sure? [y/N] ') or 'N'
if check.lower() != 'y':
print('Ok then')
return
user = get_user_by_uid(client, uid)
if user is not None:
print('Deleting')
delete_user(client, user)
else:
print('User {} not found'.format(uid))
if __name__ == '__main__':
cli.add_arg('--config', 'config.yml', 'custom configuration file')
args = cli.get_args()