Exemplo n.º 1
0
def GET(request):
    """Get this User's Authorization over this Simulation."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(path={
            'simulationId': 'int',
            'userId': 'int'
        })

    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate an Authorization

    authorization = Authorization.from_primary_key(
        (request.params_path['userId'], request.params_path['simulationId']))

    # Make sure this Authorization exists in the database

    if not authorization.exists():
        return Response(404, '{} not found.'.format(authorization))

    # Read this Authorization from the database

    authorization.read()

    # Return this Authorization

    return Response(200, 'Successfully retrieved {}'.format(authorization),
                    authorization.to_JSON())
Exemplo n.º 2
0
    def google_id_has_at_least(self, google_id, authorization_level):
        """Return True if the user has at least the given auth level over this Path."""

        # Get the User id

        try:
            user_id = User.from_google_id(google_id).read().id
        except exceptions.RowNotFoundError:
            return False

        # Check the Authorization

        authorization = Authorization.from_primary_key(
            (user_id, self.simulation_id))

        return authorization.has_at_least(authorization_level)
Exemplo n.º 3
0
def DELETE(request):
    """Delete a user's authorization level over a simulation."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(path={
            'simulationId': 'int',
            'userId': 'int'
        })

    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate an Authorization

    authorization = Authorization.from_primary_key(
        (request.params_path['userId'], request.params_path['simulationId']))

    # Make sure this Authorization exists in the database

    if not authorization.exists():
        return Response(404, '{} not found.'.format(authorization))

    # Make sure this User is allowed to delete this Authorization

    if not authorization.google_id_has_at_least(request.google_id, 'OWN'):
        return Response(403,
                        'Forbidden from deleting {}.'.format(authorization))

    # Delete this Authorization

    authorization.delete()

    return Response(200, 'Successfully deleted {}.'.format(authorization),
                    authorization.to_JSON())