From 690818051d0c9768cdaf735acf77ea9e98f00b38 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 10:31:27 +0200 Subject: Implement authorizations endpoint --- web-server/opendc/models/simulation.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'web-server/opendc/models/simulation.py') diff --git a/web-server/opendc/models/simulation.py b/web-server/opendc/models/simulation.py index 5cd3d49e..a77697ab 100644 --- a/web-server/opendc/models/simulation.py +++ b/web-server/opendc/models/simulation.py @@ -1,5 +1,6 @@ from opendc.models.model import Model from opendc.models.user import User +from opendc.util.database import DB from opendc.util.exceptions import ClientError from opendc.util.rest import Response @@ -13,3 +14,10 @@ class Simulation(Model): filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): raise ClientError(Response(403, "Forbidden from retrieving simulation.")) + + def get_all_authorizations(self): + return [ + user['_id'] for user in DB.fetch_all({'authorizations': { + 'simulationId': self.obj['_id'] + }}, User.collection_name) + ] -- cgit v1.2.3 From 9f87ab4bbab048b527585929135cab80fafd9ef9 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 13:28:18 +0200 Subject: Address a number of pylint issues --- web-server/opendc/models/simulation.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'web-server/opendc/models/simulation.py') diff --git a/web-server/opendc/models/simulation.py b/web-server/opendc/models/simulation.py index a77697ab..bf19368c 100644 --- a/web-server/opendc/models/simulation.py +++ b/web-server/opendc/models/simulation.py @@ -6,9 +6,16 @@ from opendc.util.rest import Response class Simulation(Model): + """Model representing a Simulation.""" + collection_name = 'simulations' def check_user_access(self, google_id, edit_access): + """Raises an error if the user with given [google_id] has insufficient access. + + :param google_id: The Google ID of the user. + :param edit_access: True when edit access should be checked, otherwise view access. + """ user = User.from_google_id(google_id) authorizations = list( filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations'])) @@ -16,6 +23,7 @@ class Simulation(Model): raise ClientError(Response(403, "Forbidden from retrieving simulation.")) def get_all_authorizations(self): + """Get all user IDs having access to this simulation.""" return [ user['_id'] for user in DB.fetch_all({'authorizations': { 'simulationId': self.obj['_id'] -- cgit v1.2.3 From a27598ee4755423ebd2f0ad8c505644d644cf2c8 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 14:07:51 +0200 Subject: Make accessing the ID easer --- web-server/opendc/models/simulation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'web-server/opendc/models/simulation.py') diff --git a/web-server/opendc/models/simulation.py b/web-server/opendc/models/simulation.py index bf19368c..dbe1e800 100644 --- a/web-server/opendc/models/simulation.py +++ b/web-server/opendc/models/simulation.py @@ -18,7 +18,7 @@ class Simulation(Model): """ user = User.from_google_id(google_id) authorizations = list( - filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations'])) + filter(lambda x: str(x['simulationId']) == str(self.get_id()), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): raise ClientError(Response(403, "Forbidden from retrieving simulation.")) @@ -26,6 +26,6 @@ class Simulation(Model): """Get all user IDs having access to this simulation.""" return [ user['_id'] for user in DB.fetch_all({'authorizations': { - 'simulationId': self.obj['_id'] + 'simulationId': self.get_id() }}, User.collection_name) ] -- cgit v1.2.3