Ejemplo n.º 1
0
def run_single_command(db_conn, email_address, optdict):
    log = logging.getLogger("run_single_command")

    if optdict['setpw']:
        set_user_password(db_conn, email_address, optdict['password'])

    elif optdict['create']:
        result = api_interface.create_user(
            {'firstname': optdict['given_name'],
             'lastname': optdict['surname'],
             'email': email_address,
             'group_id': optdict['group_id']})

    elif optdict['set_email']:
        result = api_interface.change_email(
            {'email': optdict['new-email'],
             'old_email': email_address,})
    elif optdict['set_group']:
        result = api_interface.set_user_group(
            {'email': email_address,
             'group_id': optdict['group_id'],})
    elif optdict['disable']:
        result = api_interface.deactivate_user(
            {'email': email_address,})
    elif optdict['enable']:
        result = api_interface.activate_user(
            {'email': email_address,})
    else:
        raise UsersActionError("Got an action that's not accounted for!")
Ejemplo n.º 2
0
def run_csv_file(db_conn, filename, optdict):
    """Runs the appropriate actions from a CSV file.

    :param db_conn: DB connection object
    :param filename: CSV filename
    :param optdict: Options dictionary.
    :returns int: number of successful user actions.
    """

    log = logging.getLogger("run_csv_file")
    dict_reader = csv.DictReader(filename)
    
    if 'setpw' in optdict:
        user_dicts = assure_keys(dict_reader, SETPW_REQUIRED_KEYS)
        emails = (email for email in user_dicts['email_addr'])
        pws = (pw for pw in user_dicts['password'])
        set_multi_passwords(db_conn, emails, pws)

        # All done, so leave the function here.
        return len(user_dicts)

    success_count = 0
    if 'create' in optdict:
        # Runs the creation routine for each user.
        user_dicts = assure_keys(dict_reader, CREATE_REQUIRED_KEYS)
        for user in user_dicts:
            result = api_interface.create_user(
                {'firstname': user['given_name'],
                 'lastname': user['surname'],
                 'email': user['email_addr'],})
            
            result['group_id'] = user['group_id']
            try:
                result2 = api_interface.set_user_group(result)
            except api_interface.ApiActionFailedError as e:
                log.error("User created with no group assignment: %s" % 
                          (user['email_addr'],))
                raise e
            success_count += 1

    elif 'set_email' in optdict:
        # Sets emails for each user.
        user_dicts = assure_keys(dict_reader, SET_EMAIL_REQUIRED_KEYS)
        for user in user_dicts:
            result = api_interface.change_email(
                {'email': user['new_email'],
                 'old_email': user['email_addr'],})
            success_count += 1
    elif 'set_group' in optdict:
        # Sets groups for each user.
        user_dicts = assure_keys(dict_reader, SET_GROUP_REQUIRED_KEYS)
        for user in user_dicts:
            result = api_interface.set_user_group(
                {'email': user['email_addr'],
                 'group_id': user['group_id'],})
            success_count += 1
    elif 'disable' in optdict:
        user_dicts = assure_keys(dict_reader, frozenset(['email_addr']))
        for user in user_dicts:
            result = api_interface.deactivate_user(
                {'email': user['email_addr'],})
            success_count += 1
    elif 'enable' in optdict:
        user_dicts = assure_keys(dict_reader, frozenset(['email_addr']))
        for user in user_dicts:
            result = api_interface.activate_user(
                {'email': user['email_addr'],})
            success_count += 1
    else:
        raise UsersActionError("Got an action that's not accounted for!")
        
    return success_count