-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.py
More file actions
289 lines (228 loc) · 7.99 KB
/
queries.py
File metadata and controls
289 lines (228 loc) · 7.99 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Query Resolvers
"""
import os
from typing import List, Optional
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
from utils import get_profile, ldap_search
inter_communication_secret_global = os.getenv("INTER_COMMUNICATION_SECRET")
@strawberry.field
async def userProfile(
userInput: Optional[UserInput], info: Info
) -> ProfileType | None:
"""
User Information from LDAP
This method is used to get the profile of a user from IIITH server
directory using LDAP.
Searched on the basis of the uid given as input or the currently logged
user's uid.
Accessible to all users.
Args:
userInput (Optional[otypes.UserInput]): Contains uid of a user.
Optional as if not passed uses the logged
in user's info.
info (otypes.Info): Contains the user details.
Returns:
(otypes.ProfileType | None): Contains the profile of the user.
Raises:
Exception: Could not find user profile in LDAP.
"""
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
result = await ldap_search(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!")
return get_profile(result[-1]) # single profile
# get user metadata (uid, role, etc.) from local database
@strawberry.field
async def userMeta(
userInput: Optional[UserInput], info: Info
) -> UserMetaType | None:
"""
User information from database
This method is used to get the metadata of a user from database.
Searched on the basis of the uid given as input or the currently logged
user's uid.
It hides the phone number only for public users.
Args:
userInput (Optional[otypes.UserInput]): Contains uid of a user.
Optional as if not passed uses the current
logged in user's info.
info (otypes.Info): Contains the user details.
Returns:
(otypes.UserMetaType | None): Contains the metadata of the user.
"""
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 = await 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)
await 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
async def usersByRole(
info: Info, role: str, inter_communication_secret: str | None = None
) -> List[UserMetaType]:
"""
This method is used to get the metadata of all users belonging to the
given input role.
Args:
info (otypes.Info): Contains the user details.
role (str): The role of the user.
inter_communication_secret (str | None): secret used to authenticate
the request. Defaults to None.
Returns:
(List[otypes.UserMetaType]): Contains the metadata of the users.
Raises:
Exception: Authentication Error! Invalid secret!
"""
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 = await db.users.find({"role": role}).to_list(length=None)
return [
UserMetaType.from_pydantic(User.model_validate(user)) for user in users
]
@strawberry.field
async def usersByBatch(
batch_year: int, ug: bool = True, pg: bool = True
) -> List[ProfileType]:
"""
This method is used to get the profiles
of all users belonging to the
given input batch year, from UG, MS,
MTECH, PG and PHD batches.
Args:
batch_year (int): The batch year of the user.
ug (bool): Whether to include UG students. Defaults to True.
pg (bool): Whether to include PG students. Defaults to True.
Returns:
(List[otypes.ProfileType]): Contains the profiles of the users.
Raises:
Exception: Could not find user profiles
for given batch year in LDAP!
"""
if batch_year < 18 or batch_year > 100:
return []
if not (ug or pg):
return []
full_ous = []
if ug:
full_ous.extend(
[
f"ug2k{batch_year}",
f"ug2k{batch_year}dual",
f"le2k{batch_year + 1}",
]
)
if pg:
full_ous.extend(
[
f"ms2k{batch_year}",
f"mtech2k{batch_year}",
f"pgssp2k{batch_year}",
f"phd2k{batch_year}",
]
)
filterstr = f"(&(|{''.join(f'(ou:dn:={ou})' for ou in full_ous)})(uid=*))"
result = await ldap_search(filterstr)
# error out if LDAP query fails
if not result:
print(
f"Could not find user profiles for batch 2k{batch_year} in LDAP!"
)
raise Exception(
f"Could not find user profiles for batch 2k{batch_year} in LDAP!"
)
# use filter() to get non None values
return [
get_profile(user_result) for user_result in result
] # single profile
# get all users in the given list of uids
@strawberry.field
async def usersByList(
info: Info, userInputs: List[UserInput]
) -> List[Optional[ProfileType]]:
"""
This method is used to get the profiles of all
users belonging to the input array of uids.
Args:
userInputs (List[otypes.UserInput]): The list of
uids of the users.
info (otypes.Info): Contains the user details.
Returns:
(List[Optional[otypes.ProfileType]]): Contains profiles of the users.
"""
profiles = []
filterstr = (
f"(|{''.join(f'(uid={userInput.uid})' for userInput in userInputs)})"
)
results: List = await ldap_search(filterstr)
# Make a list of successful profiles
resultUids = [result[1]["uid"][0].decode() for result in results]
for userInput in userInputs:
uid = userInput.uid
if uid in resultUids: # If we get a result for this uid
index = resultUids.index(uid)
profiles.append(get_profile(results[index]))
results.pop(index) # Remove uid to speed up search time
resultUids.pop(index)
else:
profiles.append(None)
return profiles
# register all queries
queries = [
userProfile,
userMeta,
usersByRole,
usersByBatch,
usersByList,
]