-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoutnetuser.py
86 lines (69 loc) · 2.86 KB
/
scoutnetuser.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
from data import Semester
import logging
import json
class ScoutnetUser():
def __init__(self, display_name: str, email: str, uid: str, group_roles: dict[str, str]):
self.display_name = display_name
self.email = email
self.uid = uid
self.group_roles = group_roles
self.active_semester = None # TODO: set active semester
def getname(self) -> str:
return self.display_name
def hasAccess(self, group_id) -> bool:
return self.hasOneOfRoles(group_id, ["leader", "member_registrar"])
def isGroupAdmin(self, group_id: str) -> bool:
return self.hasOneOfRoles(group_id, ["member_registrar"])
def canImport(self, group_id) -> bool:
return self.hasOneOfRoles(group_id, ["member_registrar"])
def getActiveSemester(self) -> Semester:
return self.active_semester
def isSuperUser(self) -> bool:
# TODO: fetch scoutnet id (uid) from a table with super users.
return False
def getAllGroupIds(self) -> list[str]:
return [group_id for group_id in self.group_roles]
def hasOneOfRoles(self, group_id: str, desired_roles: list[str]):
if group_id not in self.group_roles:
return False
roles = self.group_roles[group_id]
for role_id in roles:
if roles[role_id] in desired_roles:
return True
return False
@staticmethod
def loads_json_if_string(data):
# if this is a string we need to parse the json, else assume it's already parsed
if isinstance(data, str):
return json.loads(data)
return data
@staticmethod
def parse_user(user_data) -> "ScoutnetUser":
print(user_data)
email = user_data['email']
# these can be an array of groups if you are member of multiple groups
#group_no = user_data['group_no']
#group_id = user_data['group_id']
uid = user_data['uid']
display_name = user_data['displayName']
group_roles = []
if 'roles' in user_data:
roles = ScoutnetUser.loads_json_if_string(user_data['roles']) # this is a json string (must be a bug in scoutid)
if 'group' in roles:
# Assuming here that if you are leader in multiple groups you will get a role on each group having an array of groups with roles
# yet to be verified.
group_roles = roles['group']
user = ScoutnetUser(display_name, email, uid, group_roles)
logging.info(f"User: {user.getname()}, {user.email}")
return user
@property
def activeSemester(self):
return self.active_semester
@activeSemester.setter
def activeSemester(self, value):
self.active_semester = value
@property
def key(self) -> str:
return self.uid
def put(self):
pass # TODO: store active_semester in db