Skip to content

Commit

Permalink
Merge pull request #10 from Clubs-Council-IIITH/members_check_ldap
Browse files Browse the repository at this point in the history
Check whether member exists from LDAP before going ahead in create new member mutation
  • Loading branch information
abhiramtilakiiit authored Mar 31, 2024
2 parents 990c246 + 7424b50 commit e567d5f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 68 deletions.
74 changes: 6 additions & 68 deletions mutations_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,13 @@
from otypes import Info
from models import Member
from otypes import FullMemberInput, SimpleMemberInput, MemberType
from utils import non_deleted_members, unique_roles_id, getUser

"""
MEMBER MUTATIONS
"""


def non_deleted_members(member_input) -> MemberType:
"""
Function to return non-deleted members for a particular cid, uid
Only to be used in admin functions, as it returns both approved/non-approved members.
"""
updated_sample = membersdb.find_one(
{
"$and": [
{"cid": member_input["cid"]},
{"uid": member_input["uid"]},
]
},
{"_id": 0},
)
if updated_sample is None:
raise Exception("No such Record")

roles = []
for i in updated_sample["roles"]:
if i["deleted"] is True:
continue
roles.append(i)
updated_sample["roles"] = roles

return MemberType.from_pydantic(Member.parse_obj(updated_sample))


def unique_roles_id(uid, cid):
"""
Function to give unique ids for each of the role in roles list
"""
pipeline = [
{
"$set": {
"roles": {
"$map": {
"input": {"$range": [0, {"$size": "$roles"}]},
"in": {
"$mergeObjects": [
{"$arrayElemAt": ["$roles", "$$this"]},
{
"rid": {
"$toString": {
"$add": [
{"$toLong": datetime.now()},
"$$this",
]
}
}
},
]
},
}
}
}
}
]
membersdb.update_one(
{
"$and": [
{"cid": cid},
{"uid": uid},
]
},
pipeline,
)


@strawberry.mutation
def createMember(memberInput: FullMemberInput, info: Info) -> MemberType:
"""
Expand All @@ -109,6 +42,11 @@ def createMember(memberInput: FullMemberInput, info: Info) -> MemberType:
):
raise Exception("A record with same uid and cid already exists")

# Check whether this uid is valid or not
userMember = getUser(member_input["uid"], info.context.cookies)
if userMember is None:
raise Exception("Invalid User ID")

if len(member_input["roles"]) == 0:
raise Exception("Roles cannot be empty")

Expand Down
105 changes: 105 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from datetime import datetime
import requests

from db import membersdb

from otypes import MemberType
from models import Member

def non_deleted_members(member_input) -> MemberType:
"""
Function to return non-deleted members for a particular cid, uid
Only to be used in admin functions, as it returns both approved/non-approved members.
"""
updated_sample = membersdb.find_one(
{
"$and": [
{"cid": member_input["cid"]},
{"uid": member_input["uid"]},
]
},
{"_id": 0},
)
if updated_sample is None:
raise Exception("No such Record")

roles = []
for i in updated_sample["roles"]:
if i["deleted"] is True:
continue
roles.append(i)
updated_sample["roles"] = roles

return MemberType.from_pydantic(Member.parse_obj(updated_sample))


def unique_roles_id(uid, cid):
"""
Function to give unique ids for each of the role in roles list
"""
pipeline = [
{
"$set": {
"roles": {
"$map": {
"input": {"$range": [0, {"$size": "$roles"}]},
"in": {
"$mergeObjects": [
{"$arrayElemAt": ["$roles", "$$this"]},
{
"rid": {
"$toString": {
"$add": [
{"$toLong": datetime.now()},
"$$this",
]
}
}
},
]
},
}
}
}
}
]
membersdb.update_one(
{
"$and": [
{"cid": cid},
{"uid": uid},
]
},
pipeline,
)

def getUser(uid, cookies=None):
"""
Function to get a particular user details
"""
try:
query = """
query GetUserProfile($userInput: UserInput!) {
userProfile(userInput: $userInput) {
firstName
lastName
email
rollno
}
}
"""
variable = {"userInput": {"uid": uid}}
if cookies:
request = requests.post(
"http://gateway/graphql",
json={"query": query, "variables": variable},
cookies=cookies,
)
else:
request = requests.post(
"http://gateway/graphql", json={"query": query, "variables": variable}
)

return request.json()["data"]["userProfile"]
except Exception:
return None

0 comments on commit e567d5f

Please sign in to comment.