From 4f9a40abdc7836345113c047f27fcc96800cb3f5 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Mon, 29 Jun 2020 16:05:23 +0200 Subject: Prepare web-server repository for monorepo This change prepares the web-server Git repository for the monorepo residing at https://github.com/atlarge-research.com/opendc. To accomodate for this, we move all files into a web-server subdirectory. --- web-server/opendc/models/user.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 web-server/opendc/models/user.py (limited to 'web-server/opendc/models/user.py') diff --git a/web-server/opendc/models/user.py b/web-server/opendc/models/user.py new file mode 100644 index 00000000..cd314457 --- /dev/null +++ b/web-server/opendc/models/user.py @@ -0,0 +1,26 @@ +from opendc.models.model import Model +from opendc.util.database import DB +from opendc.util.exceptions import ClientError +from opendc.util.rest import Response + + +class User(Model): + collection_name = 'users' + + @classmethod + def from_email(cls, email): + return User(DB.fetch_one({'email': email}, User.collection_name)) + + @classmethod + def from_google_id(cls, google_id): + return User(DB.fetch_one({'googleId': google_id}, User.collection_name)) + + def check_correct_user(self, request_google_id): + if request_google_id is not None and self.obj['googleId'] != request_google_id: + raise ClientError(Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.')) + + def check_already_exists(self): + existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name) + + if existing_user is not None: + raise ClientError(Response(409, 'User already exists.')) -- cgit v1.2.3