scout/commands/update/user.py
def user(context, user_id, update_role, add_institute, remove_admin, remove_institute):
    """
    Update a user in the database
    """
    adapter = context.obj['adapter']

    user_obj = adapter.user(user_id)

    if not user_obj:
        LOG.warning("User %s could not be found", user_id)
        context.abort()

    existing_roles = set(user_obj.get('roles',[]))
    if update_role:
        if not update_role in user_obj['roles']:
            existing_roles = set(user_obj['roles'])
            existing_roles.add(update_role)
            LOG.info("Adding role %s to user", update_role)
        else:
            LOG.warning("User already have role %s", update_role)

    if remove_admin:
        try:
            existing_roles.remove('admin')
            LOG.info("Removing admin rights from user %s", user_id)
        except KeyError as err:
            LOG.info("User %s does not have admin rights", user_id)

    user_obj['roles'] = list(existing_roles)

    existing_institutes = set(user_obj.get('institutes',[]))
    for institute_id in add_institute:
        institute_obj = adapter.institute(institute_id)
        if not institute_obj:
            LOG.warning("Institute %s could not be found", institute_id)
        else:
            existing_institutes.add(institute_id)
            LOG.info("Adding institute %s to user", institute_id)

    for institute_id in remove_institute:
        try:
            existing_institutes.remove(institute_id)
            LOG.info("Removing institute %s from user", institute_id)
        except KeyError as err:
            LOG.info("User does not have access to institute %s", institute_id)

    user_obj['institutes'] = list(existing_institutes)

    updated_user = adapter.update_user(user_obj)
def user(context, user_id, update_role, add_institute, remove_admin, remove_institute):
    adapter = context.obj['adapter']

    user_obj = adapter.user(user_id)

    if not user_obj:
        LOG.warning("User %s could not be found", user_id)
        context.abort()

    existing_roles = set(user_obj.get('roles',[]))
    if update_role:
        if not update_role in user_obj['roles']:
            existing_roles = set(user_obj['roles'])
            existing_roles.add(update_role)
            LOG.info("Adding role %s to user", update_role)
        else:
            LOG.warning("User already have role %s", update_role)

    if remove_admin:
        try:
            existing_roles.remove('admin')
            LOG.info("Removing admin rights from user %s", user_id)
        except KeyError as err:
            LOG.info("User %s does not have admin rights", user_id)

    user_obj['roles'] = list(existing_roles)

    existing_institutes = set(user_obj.get('institutes',[]))
    for institute_id in add_institute:
        institute_obj = adapter.institute(institute_id)
        if not institute_obj:
            LOG.warning("Institute %s could not be found", institute_id)
        else:
            existing_institutes.add(institute_id)
            LOG.info("Adding institute %s to user", institute_id)

    for institute_id in remove_institute:
        try:
            existing_institutes.remove(institute_id)
            LOG.info("Removing institute %s from user", institute_id)
        except KeyError as err:
            LOG.info("User does not have access to institute %s", institute_id)

    user_obj['institutes'] = list(existing_institutes)

    updated_user = adapter.update_user(user_obj)
def user ( context , user_id , update_role , add_institute , remove_admin , remove_institute ) : adapter = context . obj [ 'adapter' ] user_obj = adapter . user ( user_id ) if not user_obj : LOG . warning ( "User %s could not be found" , user_id ) context . abort ( ) existing_roles = set ( user_obj . get ( 'roles' , [ ] ) ) if update_role : if not update_role in user_obj [ 'roles' ] : existing_roles = set ( user_obj [ 'roles' ] ) existing_roles . add ( update_role ) LOG . info ( "Adding role %s to user" , update_role ) else : LOG . warning ( "User already have role %s" , update_role ) if remove_admin : try : existing_roles . remove ( 'admin' ) LOG . info ( "Removing admin rights from user %s" , user_id ) except KeyError as err : LOG . info ( "User %s does not have admin rights" , user_id ) user_obj [ 'roles' ] = list ( existing_roles ) existing_institutes = set ( user_obj . get ( 'institutes' , [ ] ) ) for institute_id in add_institute : institute_obj = adapter . institute ( institute_id ) if not institute_obj : LOG . warning ( "Institute %s could not be found" , institute_id ) else : existing_institutes . add ( institute_id ) LOG . info ( "Adding institute %s to user" , institute_id ) for institute_id in remove_institute : try : existing_institutes . remove ( institute_id ) LOG . info ( "Removing institute %s from user" , institute_id ) except KeyError as err : LOG . info ( "User does not have access to institute %s" , institute_id ) user_obj [ 'institutes' ] = list ( existing_institutes ) updated_user = adapter . update_user ( user_obj )
Update a user in the database
Update a user in the database