phi/src/phicli

81 lines
2.1 KiB
Plaintext
Raw Normal View History

2020-10-10 18:45:16 +02:00
#!/usr/bin/env python3
from pprint import pformat as pp
2020-10-11 12:41:44 +02:00
from getpass import getpass
2020-10-10 18:45:16 +02:00
from phi.config import get_config
from phi.logging import setup_logging, get_logger
2020-10-11 11:55:36 +02:00
from phi import cli
import phi.ldap.client
2020-10-11 12:41:44 +02:00
from phi.ldap.user import get_user_by_uid, add_user, delete_user
2020-10-10 23:27:08 +02:00
2020-10-10 18:45:16 +02:00
log = get_logger(__name__)
2020-10-11 01:11:56 +02:00
2020-10-11 12:41:44 +02:00
@cli.register('dispaly user fields', ['user identifier'])
2020-10-11 01:11:56 +02:00
def showuser(uid):
user = get_user_by_uid(client, uid)
print('\nuid: {}\n{}\n\n'.format(uid, pp(user)))
2020-10-11 12:41:44 +02:00
@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))
2020-10-11 01:11:56 +02:00
if __name__ == '__main__':
2020-10-11 11:55:36 +02:00
cli.add_arg('--config', 'config.yml', 'custom configuration file')
args = cli.get_args()
2020-10-11 01:11:56 +02:00
config_file = args['config']
2020-10-10 18:45:16 +02:00
2020-10-10 21:14:21 +02:00
config_file, config = get_config(config_file)
setup_logging(config.get('logging', {}))
2020-10-10 23:27:08 +02:00
log.info("Using configuration at '{}':\n{}"
.format(config_file, pp(config)))
2020-10-10 18:45:16 +02:00
2020-10-10 23:27:08 +02:00
# TODO: check fields in config
2020-10-10 18:45:16 +02:00
client = phi.ldap.client.Client(**config['ldap'])
2020-10-10 23:27:08 +02:00
log.info('Opening LDAP client')
2020-10-10 18:45:16 +02:00
client.open()
2020-10-11 01:11:56 +02:00
log.info('Arguments: {}'.format(pp(args)))
2020-10-10 23:27:08 +02:00
2020-10-11 11:55:36 +02:00
cli.run(args)
2020-10-10 18:45:16 +02:00
log.info('Closing LDAP client')
client.close()