-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrollers.py
273 lines (251 loc) · 9.62 KB
/
controllers.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from flask import jsonify
from helpers import load_body, Response
from models import db
from models import User, Group, Resource
from sqlalchemy.exc import IntegrityError
import traceback
class GroupController(object):
def create(self, request):
data = load_body(request)
if data is None:
return Response(status=400, response={"Error": "Bad Request, request has to be a valid application-json."})
try:
group = Group(data["name"], data.get("description"))
db.session.add(group)
db.session.commit()
except KeyError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Missing key in body"
})
except IntegrityError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Resource already exists"
})
except Exception as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=500, response={
"status": 500,
"message": "Internal Server Error"
})
else:
result = {
"id": group.id,
"name": group.name,
"description": group.description,
}
return jsonify(result)
def read(self, request, id):
group = Group.query.get_or_404(id)
result = {
"id": group.id,
"name": group.name,
"description": group.description,
}
return jsonify(result)
def list(self, request):
query = Group.query
count = query.count()
items = map(lambda group: {
"id": group.id,
"name": group.name,
"description": group.description,
}, query.all())
result = {
"count": count,
"items": list(items)
}
return jsonify(result)
class UserController(object):
def add(self, request, group_id):
try:
group_id = int(group_id)
data = load_body(request)
users_ids = map(lambda item: item['userId'], data)
users = list()
for user_id in users_ids:
user = User.query.get(user_id) # XXX: if single query is executed each loop then ehance to execute one query using filters
if not user:
user = User(id=user_id, groups=[group_id])
db.session.add(user)
else:
users.append(user)
for user in users:
if group_id not in user.groups:
user.groups = user.groups + [group_id]
except KeyError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Missing key in body"
})
except IntegrityError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Resource already exists"
})
except Exception as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=500, response={
"status": 500,
"message": "Internal Server Error"
})
else:
db.session.commit()
return Response(status=204)
def list(self, request, group_id):
q = User.query.filter(User.groups.contains(group_id))
count = q.count()
items = map(lambda u: {
"userId": u.id
}, q.all())
result = {
"count": count,
"items": list(items)
}
return jsonify(result)
class ResourceController(object):
def create(self, request):
data = load_body(request)
if data is None:
return Response(status=400, response={
"status": 400,
"message": "Bad Request, request has to be a valid application-json."
})
try:
resource = Resource(name=data["name"], groups=data.get("groups", []))
db.session.add(resource)
db.session.commit()
except KeyError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Missing key in body"
})
except IntegrityError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Resource already exists"
})
except Exception as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=500, response={
"status": 500,
"message": "Internal Server Error"
})
else:
result = {
"id": resource.id,
"name": resource.name,
}
return jsonify(result)
def read(self, request, id):
resource = Resource.query.get_or_404(id)
result = {
"id": resource.id,
"name": resource.name,
}
return jsonify(result)
def list(self, request):
query = Resource.query
count = query.count()
items = map(lambda resource: {
"id": resource.id,
"name": resource.name,
}, query.all())
result = {
"count": count,
"items": list(items)
}
return jsonify(result)
def add(self, request, group_id):
Group.query.get_or_404(group_id)
try:
group_id = int(group_id)
data = load_body(request)
resources_ids = map(lambda item: item['resourceId'], data)
resources = list()
for resource_id in resources_ids:
resource = Resource.query.get(resource_id) # XXX: if single query is executed each loop then ehance to execute one query using filters
if not resource:
Resource.query.session.rollback()
return Response(status=404, response={
"status": 404,
"message": "Resource Not Found, resourceId: %s" % resource_id
})
else:
resources.append(resource)
for resource in resources:
if group_id not in resource.groups:
resource.groups = resource.groups + [group_id]
except KeyError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Missing key in body"
})
except IntegrityError as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=400, response={
"status": 400,
"message": "Resource already exists"
})
except Exception as err:
print("Error: %s\nStacktrace: %s" % (err, traceback.format_exc()))
db.session.rollback()
return Response(status=500, response={
"status": 500,
"message": "Internal Server Error"
})
else:
db.session.commit()
return Response(status=204)
def list_by_group(self, request, group_id):
Group.query.get_or_404(group_id)
q = Resource.query.filter(Resource.groups.contains(group_id))
count = q.count()
items = map(lambda resource: {
"id": resource.id,
"name": resource.name,
}, q.all())
result = {
"count": count,
"items": list(items)
}
return jsonify(result)
class AuthController(object):
def is_authorized(self, request):
try:
user_id = request.args['userId']
resource_name = request.args['resourceName']
except KeyError:
return Response(status=400, response={
"status": 400,
"message": "Missing query param 'userId' or 'resourceName'"
})
user = User.query.get(user_id)
resource = Resource.query.filter(Resource.name == resource_name).first()
if user is None or resource is None:
return Response(status=400, response={"error": "Invalid 'userId' or 'resourceName'"})
is_authorized = not set(user.groups).isdisjoint(resource.groups) # check if both user and resource has relation with the same group
return Response(response={"authorized": is_authorized}, status=200 if is_authorized else 403)
group_controller = GroupController()
user_controller = UserController()
resource_controller = ResourceController()
auth_controller = AuthController()