示例#1
0
def list_users():
    """
    List all users.
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/'])
    headers = {'Cache-Control': 'no-cache'}
    response = requests.get(
        url,
        headers=headers,
        timeout=5,
    )
    response.raise_for_status()
    response = decode_response(response)
    jsonschema.validate(response, schemas.list_users_schema)
    return response
示例#2
0
def create_project_membership(project_membership, notify_user=True):
    """
    Create an OpenLDAP project membership.

    Args:
        project_membership (str): Project Membership - required
        notify_user (bool): Issue a notification email to the user? - optional
    """
    url = ''.join([
        settings.OPENLDAP_HOST, 'project/member/',
        project_membership.project.code, '/'
    ])
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cache-Control': 'no-cache',
    }
    payload = {
        'email': project_membership.user.email,
    }
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, create_project_membership_json)
        raise_for_data_error(response.get('data'))

        if notify_user:
            subject = _('{company_name} Project Membership Created'.format(
                company_name=settings.COMPANY_NAME))
            context = {
                'first_name': project_membership.user.first_name,
                'to': project_membership.user.email,
                'status': project_membership.get_status_display(),
                'code': project_membership.project.code,
            }
            text_template_path = 'notifications/project_membership/update.txt'
            html_template_path = 'notifications/project_membership/update.html'
            email_user(subject, context, text_template_path,
                       html_template_path)
        return response
    except Exception as e:
        project_membership.reset_status()
        raise e
示例#3
0
def reset_user_password(email_address):
    """
    Reset a user's password.

    Args:
        email_address (str): Email address - required
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/resetPassword/', email_address, '/'])
    headers = {'Cache-Control': 'no-cache'}
    response = requests.post(
        url,
        headers=headers,
        timeout=5,
    )
    response.raise_for_status()
    response = decode_response(response)
示例#4
0
def test_get_user_by_email_address(email_address):
    """
    Get an existing user by email address.

    Args:
        email_address (str): Email address - required
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/', email_address, '/'])
    headers = {'Cache-Control': 'no-cache'}
    response = requests.get(
        url,
        headers=headers,
        timeout=5,
    )
    response.raise_for_status()
    response = decode_response(response)
    jsonschema.validate(response, schemas.get_user_schema)
    return response
示例#5
0
def get_user_by_id(user_id):
    """
    Get an existing user by id.

    Args:
        user_id (str): User id - required
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/', user_id, '/'])
    headers = {'Cache-Control': 'no-cache'}
    response = requests.get(
        url,
        headers=headers,
        timeout=5,
    )
    response.raise_for_status()
    response = decode_response(response)
    jsonschema.validate(response, schemas.get_user_schema)
    return response
示例#6
0
def activate_project(allocation, notify_user=True):
    """
    Activate an OpenLDAP project.

    Args:
        allocation (SystemAllocationRequest): Project's system allocation request - required
        notify_user (bool): Issue a notification email to the project technical lead? - optional
    """
    project = allocation.project
    url = ''.join(
        [settings.OPENLDAP_HOST, 'project/enable/', project.code, '/'])
    headers = {'Cache-Control': 'no-cache'}
    try:
        response = requests.put(
            url,
            headers=headers,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, activate_project_json)
        raise_for_data_error(response.get('data'))

        if notify_user:
            subject = _('{company_name} Project {code} Activated'.format(
                company_name=settings.COMPANY_NAME,
                code=project.code,
            ))
            context = {
                'first_name': project.tech_lead.first_name,
                'to': project.tech_lead.email,
                'code': project.code,
                'status': allocation.get_status_display().lower(),
            }
            text_template_path = 'notifications/project/update.txt'
            html_template_path = 'notifications/project/update.html'
            email_user(subject, context, text_template_path,
                       html_template_path)
        return response
    except Exception as e:
        print(e)
        allocation.reset_status()
        raise e
示例#7
0
def list_projects():
    """
    List all OpenLDAP projects.
    """
    url = ''.join([settings.OPENLDAP_HOST, 'project/'])
    headers = {'Cache-Control': 'no-cache'}
    try:
        response = requests.get(
            url,
            headers=headers,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, list_projects_json)
        raise_for_data_error(response.get('data'))
        return response
    except Exception as e:
        raise e
示例#8
0
def delete_user(email_address):
    """
    Delete (deactivate) an existing user.

    Args:
        email_address (str): Email address - required
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/', email_address, '/'])
    headers = {'Cache-Control': 'no-cache'}
    response = requests.delete(
        url,
        headers=headers,
        timeout=5,
    )
    response.raise_for_status()
    response = decode_response(response)
    # Pending implementation
    #jsonschema.validate(response, schemas.get_user_schema)
    return response
示例#9
0
def enable_user_account(email_address):
    """
    Enable a user's account.

    Args:
        email_address (str): Email address - required
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/enable/', email_address, '/'])
    headers = {'Cache-Control': 'no-cache'}
    response = requests.put(
        url,
        headers=headers,
        timeout=5,
    )
    response.raise_for_status()
    response = decode_response(response)
    # Pending implementation
    #jsonschema.validate(response, schemas.get_user_schema)
    return response
示例#10
0
def list_project_memberships(project_code):
    """
    List all OpenLDAP project memberships for a given project.
    """
    url = ''.join(
        [settings.OPENLDAP_HOST, 'project/member/', project_code, '/'])
    try:
        response = requests.get(
            url,
            headers=headers,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, list_project_memberships_json)
        raise_for_data_error(response.get('data'))
        return response
    except Exception as e:
        raise e
示例#11
0
def reset_user_password(user, password, notify_user=True):
    """
    Reset a user's OpenLDAP account password.

    Args:
        user (CustomUser): User instance - required
        password (str): New password - required
        notify_user (bool): Issue a notification email to the user? - optional
    """
    url = ''.join(
        [settings.OPENLDAP_HOST, 'user/resetPassword/', user.email, '/'])
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cache-Control': 'no-cache',
    }
    payload = {'password': password}
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, reset_user_password_json)
        raise_for_data_error(response.get('data'))

        if notify_user:
            subject = _('{company_name} Password Reset'.format(
                company_name=settings.COMPANY_NAME))
            context = {
                'first_name': user.first_name,
                'to': user.email,
            }
            text_template_path = 'notifications/user/password_reset.txt'
            html_template_path = 'notifications/user/password_reset.html'
            email_user(subject, context, text_template_path,
                       html_template_path)
        return response
    except Exception as e:
        raise e
示例#12
0
def get_project(project_code):
    """
    Get an existing OpenLDAP project.

    Args:
        project_code (str): Project code - required
    """
    url = ''.join([settings.OPENLDAP_HOST, 'project/', project_code, '/'])
    headers = {'Cache-Control': 'no-cache'}
    try:
        response = requests.get(
            url,
            headers=headers,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, get_project_json)
        raise_for_data_error(response.get('data'))
        return response
    except Exception as e:
        raise e
示例#13
0
def get_user_by_email_address(email_address):
    """
    Get an existing user's OpenLDAP account details by email address.

    Args:
        email_address (str): Email address - required
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/', email_address, '/'])
    headers = {'Cache-Control': 'no-cache'}
    try:
        response = requests.get(
            url,
            headers=headers,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, get_user_json)
        raise_for_data_error(response.get('data'))
        return response
    except Exception as e:
        raise e
示例#14
0
def activate_user_account(user, notify_user=True):
    """
    Activate an existing user's OpenLDAP account.

    Args:
        user (CustomUser): User instance - required
        notify_user (bool): Issue a notification email to the user? - optional
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/enable/', user.email, '/'])
    headers = {'Cache-Control': 'no-cache'}
    try:
        response = requests.put(
            url,
            headers=headers,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, activate_user_json)
        raise_for_data_error(response.get('data'))

        if notify_user:
            subject = _('{company_name} Account Activated'.format(
                company_name=settings.COMPANY_NAME))
            context = {
                'first_name': user.first_name,
                'to': user.email,
                'status': user.profile.get_account_status_display(),
            }
            text_template_path = 'notifications/user/update.txt'
            html_template_path = 'notifications/user/update.html'
            email_user(subject, context, text_template_path,
                       html_template_path)
        return response
    except Exception as e:
        user.profile.reset_account_status()
        raise e
示例#15
0
def create_project(allocation, notify_user=True):
    """
    Create an OpenLDAP project.

    Args:
        allocation (SystemAllocationRequest): Project's system allocation request - required
        notify_user (bool): Issue a notification email to the project technical lead? - optional
    """
    project = allocation.project
    url = ''.join([settings.OPENLDAP_HOST, 'project/'])
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cache-Control': 'no-cache',
    }
    title = '{title} (Project Leader = {supervisor}, Technical Lead = {tech_lead})'.format(
        supervisor=project.supervisor_name,
        tech_lead=project.tech_lead.email,
        title=project.title,
    )
    payload = {
        'code': project.code,
        'category': project.category.id,
        'title': title,
        'technical_lead': project.tech_lead.profile.scw_username,
    }
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, create_project_json)
        data = response.get('data')
        raise_for_data_error(data)
        mapping = {
            'code': 'cn',
            'technical_lead': 'memberUid',
        }
        verify_payload_data(payload, data, mapping)

        # Update project details.
        project.gid_number = data.get('gidNumber', '')
        project.save()

        if notify_user:
            subject = _('{company_name} Project {code} Created'.format(
                company_name=settings.COMPANY_NAME,
                code=project.code,
            ))
            context = {
                'first_name': project.tech_lead.first_name,
                'to': project.tech_lead.email,
                'code': project.code,
                'status': allocation.get_status_display().lower()
            }
            text_template_path = 'notifications/project/update.txt'
            html_template_path = 'notifications/project/update.html'
            email_user(subject, context, text_template_path,
                       html_template_path)
        return response
    except Exception as e:
        print(e)
        if 'Existing Project' not in str(e):
            allocation.reset_status()
        raise e
示例#16
0
def create_user(user, notify_user=True):
    """
    Create an OpenLDAP user account.

    Args:
        user (CustomUser): User instance - required
        notify_user (bool): Issue a notification email to the user? - optional
    """
    url = ''.join([settings.OPENLDAP_HOST, 'user/'])
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cache-Control': 'no-cache',
    }
    payload = {
        'email': user.email,
        'firstName': user.first_name,
        'surname': user.last_name,
    }
    if user.profile.phone:
        payload.update({'telephone': user.profile.phone})
    if user.profile.uid_number:
        payload.update({'uidNumber': user.profile.uid_number})
    if hasattr(user.profile, 'department'):
        payload.update({'department': user.profile.department})
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload,
            timeout=5,
        )
        response.raise_for_status()
        response = decode_response(response)
        jsonschema.validate(response, create_user_json)
        data = response.get('data')
        raise_for_data_error(data)
        mapping = {
            'email': 'mail',
            'firstName': 'givenname',
        }
        verify_payload_data(payload, data, mapping)

        # Update user profile.
        user.profile.scw_username = data.get('uid', '')
        user.profile.uid_number = data.get('uidnumber', '')
        user.save()

        if notify_user:
            subject = _('{company_name} Account Created'.format(
                company_name=settings.COMPANY_NAME))
            context = {
                'first_name': user.first_name,
                'to': user.email,
                'status': user.profile.get_account_status_display(),
            }
            text_template_path = 'notifications/user/update.txt'
            html_template_path = 'notifications/user/update.html'
            email_user(subject, context, text_template_path,
                       html_template_path)
        return response
    except Exception as e:
        if 'Existing user' not in str(e):
            user.profile.reset_account_status()
        raise e