Add to group

This commit is contained in:
uid 2020-10-11 14:53:32 +02:00
parent 8b901ca5d5
commit 69959d0b18

View File

@ -7,6 +7,7 @@ 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, add_user, delete_user
from phi.ldap.group import get_group_by_cn, get_all_groups, add_group_member
log = get_logger(__name__)
@ -14,6 +15,10 @@ log = get_logger(__name__)
@cli.register('dispaly user fields', ['user identifier'])
def showuser(uid):
user = get_user_by_uid(client, uid)
if user is None:
print('User {} not found'.format(uid))
return
print('\nuid: {}\n{}\n\n'.format(uid, pp(user)))
@ -55,6 +60,44 @@ def deluser(uid):
print('User {} not found'.format(uid))
@cli.register('show a group', ['group common name'])
def showgroup(cn):
group = get_group_by_cn(client, cn)
if group is None:
print('Group {} not found'.format(gcn))
return
print('\ncn: {}\n{}\n\n'.format(cn, pp(group)))
@cli.register('list all groups')
def listgroups():
groups = get_all_groups(client)
print()
for group in groups:
print(group['cn'])
print()
@cli.register('add an user to a group',
['user identifier', 'group common name'])
def addtogroup(uid, gcn):
user = get_user_by_uid(client, uid)
group = get_group_by_cn(client, gcn)
if user is None:
print('User {} not found'.format(uid))
return
if group is None:
print('Group {} not found'.format(gcn))
return
add_group_member(client, group, user)
if __name__ == '__main__':
cli.add_arg('--config', 'config.yml', 'custom configuration file')
args = cli.get_args()