Example #1
0
def deactivate_user_account(user, notify_user=True):
    """
    Deactivate 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/', user.email, '/'])
    headers = {'Cache-Control': 'no-cache'}
    try:
        response = requests.delete(
            url,
            headers=headers,
            timeout=5,
        )
        response.raise_for_status()

        if notify_user:
            subject = _('{company_name} Account Deactivated'.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
Example #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
Example #3
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
Example #4
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