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 PUT(request):
    """Change a user's authorization level over a simulation."""

    # Make sure required parameters are there

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

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

    # Instantiate and Authorization

    authorization = Authorization.from_JSON({
        'userId':
        request.params_path['userId'],
        'simulationId':
        request.params_path['simulationId'],
        'authorizationLevel':
        request.params_body['authorization']['authorizationLevel']
    })

    # Make sure this Authorization exists

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

    # Make sure this User is allowed to edit this Authorization

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

    # Try to update this Authorization

    try:
        authorization.update()

    except exceptions.ForeignKeyError as e:
        return Response(400, 'Invalid authorization level.')

    # Return this Authorization

    return Response(200, 'Successfully updated {}.'.format(authorization),
                    authorization.to_JSON())
Exemplo n.º 3
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.º 4
0
def GET(request):
    """Find all authorizations for a Simulation."""

    # Make sure required parameters are there

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

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

    # Instantiate a Simulation and make sure it exists

    simulation = Simulation.from_primary_key((request.params_path['simulationId'],))

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

    # Make sure this User is allowed to view this Simulation's Authorizations

    if not simulation.google_id_has_at_least(request.google_id, 'VIEW'):
        return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(simulation))

    # Get the Authorizations

    authorizations = Authorization.query('simulation_id', request.params_path['simulationId'])

    # Return the Authorizations

    return Response(
        200,
        'Successfully retrieved Authorizations for {}.'.format(simulation),
        [x.to_JSON() for x in authorizations]
    )
Exemplo n.º 5
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())
Exemplo n.º 6
0
def GET(request):
    """Get this User's Authorizations."""

    # Make sure required parameters are there

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

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

    # Instantiate a User and make sure they exist

    user = User.from_primary_key((request.params_path['userId'],))

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

    # Make sure this requester is allowed to retrieve this User's Authorizations

    if not user.google_id_has_at_least(request.google_id, 'OWN'):
        return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(user))

    # Return this User's Authorizations

    authorizations = Authorization.query('user_id', request.params_path['userId'])

    return Response(
        200,
        'Successfully retrieved Authorizations for {}.'.format(user),
        [x.to_JSON() for x in authorizations]
    )
Exemplo n.º 7
0
def POST(request):
    """Create a new simulation, and return that new simulation."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(
            body={'simulation': {
                'name': 'string'
            }})

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

    # Instantiate a Simulation

    simulation_data = request.params_body['simulation']

    simulation_data['datetimeCreated'] = database.datetime_to_string(
        datetime.now())
    simulation_data['datetimeLastEdited'] = database.datetime_to_string(
        datetime.now())

    simulation = Simulation.from_JSON(simulation_data)

    # Insert this Simulation into the database

    simulation.insert()

    # Instantiate an Authorization and insert it into the database

    authorization = Authorization(user_id=User.from_google_id(
        request.google_id).id,
                                  simulation_id=simulation.id,
                                  authorization_level='OWN')

    authorization.insert()

    # Instantiate a Path and insert it into the database

    path = Path(simulation_id=simulation.id,
                datetime_created=database.datetime_to_string(datetime.now()))

    path.insert()

    # Instantiate a Datacenter and insert it into the database

    datacenter = Datacenter(starred=0, simulation_id=simulation.id)

    datacenter.insert()

    # Instantiate a Section and insert it into the database

    section = Section(path_id=path.id,
                      datacenter_id=datacenter.id,
                      start_tick=0)

    section.insert()

    # Return this Simulation

    return Response(200, 'Successfully created {}.'.format(simulation),
                    simulation.to_JSON())
Exemplo n.º 8
0
def POST(request):
    """Add an authorization for a user's access to a simulation."""

    # Make sure required parameters are there

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

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

    # Instantiate an Authorization

    authorization = Authorization.from_JSON({
        'userId':
        request.params_path['userId'],
        'simulationId':
        request.params_path['simulationId'],
        'authorizationLevel':
        request.params_body['authorization']['authorizationLevel']
    })

    # Make sure the Simulation and User exist

    user = User.from_primary_key((authorization.user_id, ))
    if not user.exists():
        return Response(404, '{} not found.'.format(user))

    simulation = Simulation.from_primary_key((authorization.simulation_id, ))
    if not simulation.exists():
        return Response(404, '{} not found.'.format(simulation))

    # Make sure this User is allowed to add this Authorization

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

    # Make sure this Authorization does not already exist

    if authorization.exists():
        return Response(409, '{} already exists.'.format(authorization))

    # Try to insert this Authorization into the database

    try:
        authorization.insert()

    except exceptions.ForeignKeyError:
        return Response(400, 'Invalid authorizationLevel')

    # Return this Authorization

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