1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
from opendc.models_old.authorization import Authorization
from opendc.models_old.simulation import Simulation
from opendc.models_old.user import User
from opendc.util import exceptions
from opendc.util.rest import Response
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': 'string', 'userId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
# 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())
def GET(request):
"""Get this User's Authorization over this Simulation."""
# Make sure required parameters are there
try:
request.check_required_parameters(path={'simulationId': 'string', 'userId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
# 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())
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': 'string',
'simulationId': 'string'
},
body={'authorization': {
'authorizationLevel': 'string'
}})
except exceptions.ParameterError as e:
return Response(400, str(e))
# 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())
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': 'string',
'userId': 'string'
},
body={'authorization': {
'authorizationLevel': 'string'
}})
except exceptions.ParameterError as e:
return Response(400, str(e))
# 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())
|