blob: ea8b1f3fb4e7be1223c801f6ca0abf944379625f (
plain)
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
|
from opendc.models.model import Model
from opendc.util.database import DB
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 validate(self, request_google_id=None):
super_validation = super().validate(request_google_id)
if super_validation is not None:
return super_validation
if request_google_id is not None and self.obj['googleId'] != request_google_id:
return Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.')
return None
def validate_insertion(self):
existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name)
if existing_user is not None:
return Response(409, f'User already exists.')
return None
|