-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.py
189 lines (154 loc) · 5.09 KB
/
queries.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
import os
import re
from typing import List, Optional
import ldap
import strawberry
from fastapi.encoders import jsonable_encoder
from db import db
# import all models and types
from models import User
from otypes import Info, ProfileType, UserInput, UserMetaType
inter_communication_secret_global = os.getenv("INTER_COMMUNICATION_SECRET")
# instantiate LDAP client
LDAP = ldap.initialize("ldap://ldap.iiit.ac.in")
# get user profile from LDAP
# if profileInput is passed, use the provided uid
# else return the profile of currently logged in user
@strawberry.field
def userProfile(
userInput: Optional[UserInput], info: Info
) -> ProfileType | None:
user = info.context.user
# if input uid is provided, use it
# else use current logged in user's uid (if logged in)
target = None
if userInput:
target = userInput.uid
if user and (target is None):
target = user.get("uid", None)
# error out if querying uid is null
if target is None:
return None
# raise Exception(
# "Can not query a null uid! Log in or provide an uid as input.")
# query LDAP for user profile
global LDAP
try:
result = LDAP.search_s(
"ou=Users,dc=iiit,dc=ac,dc=in",
ldap.SCOPE_SUBTREE,
filterstr=f"(uid={target})",
)
except ldap.SERVER_DOWN:
# Reconnect to LDAP server and retry the search
LDAP = ldap.initialize("ldap://ldap.iiit.ac.in")
result = LDAP.search_s(
"ou=Users,dc=iiit,dc=ac,dc=in",
ldap.SCOPE_SUBTREE,
filterstr=f"(uid={target})",
)
# error out if LDAP query fails
if not result:
print(f"Could not find user profile for {target} in LDAP!")
raise Exception("Could not find user profile in LDAP!")
# extract profile attributes
dn = result[-1][0]
ous = re.findall(
r"ou=\w.*?,", dn
) # get list of OUs the current DN belongs to
result = result[-1][1]
if "cn" in result.keys():
fullNameList = result["cn"][0].decode().split()
firstName = fullNameList[0]
lastName = " ".join(fullNameList[1:])
else:
firstName = result["givenName"][0].decode()
lastName = result["sn"][0].decode()
email = result["mail"][0].decode()
# extract optional attributes
gender = None
if "gender" in result:
gender = result["gender"][0].decode()
rollno = None
if "uidNumber" in result:
rollno = result["uidNumber"][0].decode()
elif "sambaSID" in result:
rollno = result["sambaSID"][0].decode()
batch = None
if len(ous) > 1:
# extract batch code from OUs
batch = re.sub(r"ou=(.*)?,", r"\1", ous[1])
# remove the 'dual' suffix if it exists
batch = re.sub(r"dual$", "", batch, flags=re.IGNORECASE)
stream = None
if len(ous) > 0:
# extract stream code from OUs
stream = re.sub(r"ou=(.*)?,", r"\1", ous[0])
profile = ProfileType(
firstName=firstName,
lastName=lastName,
email=email,
gender=gender,
batch=batch,
stream=stream,
rollno=rollno,
)
return profile
# get user metadata (uid, role, etc.) from local database
@strawberry.field
def userMeta(
userInput: Optional[UserInput], info: Info
) -> UserMetaType | None:
user = info.context.user
# if input uid is provided, use it
# else use current logged in user's uid (if logged in)
target = None
if userInput:
target = userInput.uid
if user and (target is None):
target = user.get("uid", None)
# error out if querying uid is null
if target is None:
return None
# raise Exception(
# "Can not query a null uid! Log in or provide an uid as input.")
target = target.lower()
# query database for user
found_user = db.users.find_one({"uid": target})
# if user doesn't exist, add to database
if found_user:
found_user = User.model_validate(found_user)
else:
found_user = User(uid=target)
db.users.insert_one(jsonable_encoder(found_user))
found_user.uid = target
if not user or (
user["role"] not in ["cc", "slo", "slc", "club"]
and user["uid"] != target
):
# if user is not authorized to see phone number, hide the phone number
found_user.phone = None
return UserMetaType.from_pydantic(found_user)
# get all users belonging to the input role
@strawberry.field
def usersByRole(
info: Info, role: str, inter_communication_secret: str | None = None
) -> List[UserMetaType]:
user = info.context.user
if user:
if user["role"] in [
"cc",
]:
inter_communication_secret = inter_communication_secret_global
if inter_communication_secret != inter_communication_secret_global:
raise Exception("Authentication Error! Invalid secret!")
users = db.users.find({"role": role})
return [
UserMetaType.from_pydantic(User.model_validate(user)) for user in users
]
# register all queries
queries = [
userProfile,
userMeta,
usersByRole,
]