示例#1
0
def create_ona_user(  # pylint: disable=too-many-arguments
        api_root: str,
        username: str,
        first_name: str,
        last_name: str,
        email: str,
        password: str
):
    """
    Custom Method that creates an Ona User
    and returns Errors That Occur.
    """
    response = request_session(
        urljoin(api_root, 'api/v1/profiles'),
        'POST',
        payload={
            'username': username,
            'email': email,
            'password': password,
            'first_name': first_name,
            'last_name': last_name
        }
    )

    if response.status_code != 201:
        created = False
    else:
        created = True

    try:
        data = response.json()
    except JSONDecodeError:
        data = None

    return (created, data)
示例#2
0
def update_details(  # pylint: disable=too-many-arguments
        api_root: str,
        username: str,
        payload: dict = None
):
    """
    Custom Method that Updates User Details
    """
    data = None

    response = request_session(
        urljoin(api_root, f'api/v1/profiles/{username}'),
        'PATCH',
        payload=payload
    )

    if response.status_code != 200:
        updated = False
    else:
        updated = True

    try:
        data = response.json()
    except JSONDecodeError:
        data = None

    return (updated, data)
示例#3
0
def add_team_member(
        api_root: str,
        username: str,
        team_id: int
):
    """
    Custom Method that adds a User to the Projects
    Org Members List
    """
    data = None
    response = request_session(
        urljoin(api_root, f'api/v1/teams/{team_id}/members'),
        'POST',
        payload={
            'username': username
        }
    )

    if response.status_code != 201:
        added = False
    else:
        added = True

    try:
        data = response.json()
    except JSONDecodeError:
        data = None

    return (added, data)
示例#4
0
def change_user_role(
        api_root: str,
        organisation: str,
        username: str,
        role: str
):
    """
    Custom method that makes a user an admin
    """
    response = request_session(
        urljoin(
            api_root,
            f'api/v1/orgs/{organisation}/members'),
        'PUT',
        payload={
            'username': username,
            'role': role
        }
    )

    if response.status_code != 200:
        updated = False
    else:
        updated = True

    return updated
示例#5
0
def change_password(
        api_root: str,
        username: str,
        old_password: str,
        password: str
):
    """
    Custom Method that Changes Password
    """
    response = request_session(
        urljoin(
            api_root,
            f'api/v1/profiles/{username}/change_password'),
        'POST',
        payload={
            'current_password': old_password,
            'new_password': password
        }
    )

    if response.status_code != 204:
        updated = False
    else:
        updated = True

    return updated
示例#6
0
    def authenticate_credentials(self, key: str, request: object = None):
        username = cache.get(key)
        cached = False

        if username is None:
            try:
                # Request is usually encoded in utf-8
                auth_key = key.decode('utf-8')
            except AttributeError:
                auth_key = key

            response = request_session(
                settings.ONA_CROSS_AUTHENTICATION_URL,
                'GET',
                username=None,
                password=None,
                headers={'Authorization': f'TempToken {auth_key}'})

            if response.status_code != 200:
                error = response.json()
                raise exceptions.AuthenticationFailed(error['detail'])

            username = response.json().get('username')
        else:
            cached = True

        try:
            # Returns a username from the user_list
            profile = UserProfile.objects.get(ona_username=username)
        except UserProfile.DoesNotExist:  # pylint: disable=no-member
            raise exceptions.AuthenticationFailed(AUTH_USER_DOESNT_EXIST)
        else:
            # Cache the key and username for a set amount of time
            # Only if the user is not already cached
            if not cached:
                # We cache twice. Creating one cache with a Key that is
                # the token key in order to use it for future validation
                # We cache it with the username as the password too, In order
                # to be able to retrieve the key within updation functions
                cache.set(key, username, settings.TEMP_TOKEN_TIMEOUT)
                cache.set(username, key, settings.TEMP_TOKEN_TIMEOUT)
                # send the logged in signal
                if request is not None:
                    user_logged_in.send(sender=profile.user.__class__,
                                        request=request,
                                        user=profile.user)

            return (profile.user, None)