Skip to content

Commit

Permalink
Improve the NGO Hub data retrieval (#320)
Browse files Browse the repository at this point in the history
* Improve the NGO Hub data retrieval
* Create the organization depending on the user type
* Prevent crashes on deleting own admin account
  • Loading branch information
tudoramariei authored Jul 11, 2024
1 parent ae5971a commit 5d7a709
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions backend/hub/social_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def save_user(self, request: HttpRequest, sociallogin: SocialLogin, form=None) -
user.is_ngohub_user = True
user.save()

# Add the user to the NGO group
ngo_group: Group = Group.objects.get(name=NGO_GROUP)
user.groups.add(ngo_group)
if user.is_superuser:
return user

# Create a blank Organization for the newly registered user
org = create_blank_org(user)
org = update_user_information(user, sociallogin.token)

# Start the import of initial data from NGO Hub
update_user_org(org, sociallogin.token, in_auth_flow=True)
if org:
update_user_org(org, sociallogin.token, in_auth_flow=True)

return user

Expand Down Expand Up @@ -101,23 +101,6 @@ def update_user_org(org: Organization, token: str, *, in_auth_flow: bool = False
auth_headers = {"Authorization": f"Bearer {token}"}
response = requests.get(settings.NGOHUB_API_BASE + "organization-profile/", headers=auth_headers)

user_profile: Dict = _get_ngo_hub_user_profile(auth_headers)
user_role: str = user_profile.get("role", "")

if user_role == "super-admin":
user = org.user
org.delete()

user.first_name = user_profile.get("name", "")
user.is_superuser = True
user.is_staff = True
user.save()

user.groups.add(Group.objects.get(name=STAFF_GROUP))
user.groups.remove(Group.objects.get(name=NGO_GROUP))

return

if response.status_code != requests.codes.ok:
raise OrganizationRetrievalHTTPException

Expand Down Expand Up @@ -146,6 +129,35 @@ def update_user_org(org: Organization, token: str, *, in_auth_flow: bool = False
update_organization(org.id, token)


def update_user_information(user, token):
auth_headers = {"Authorization": f"Bearer {token}"}

user_profile: Dict = _get_ngo_hub_user_profile(auth_headers)
user_role: str = user_profile.get("role", "")

if user_role == "super-admin":
if user.orgs.exists():
user.orgs.all().delete()

user.first_name = user_profile.get("name", "")
user.is_superuser = True
user.is_staff = True
user.save()

user.groups.add(Group.objects.get(name=STAFF_GROUP))
user.groups.remove(Group.objects.get(name=NGO_GROUP))

return None
elif user_role == "admin":
# Add the user to the NGO group
ngo_group: Group = Group.objects.get(name=NGO_GROUP)
user.groups.add(ngo_group)

org = Organization.objects.filter(user=user).first()
if not org:
return create_blank_org(user)


@receiver(social_account_updated)
def handle_existing_login(sender: SocialLogin, **kwargs) -> None:
"""
Expand All @@ -157,11 +169,15 @@ def handle_existing_login(sender: SocialLogin, **kwargs) -> None:
"""

social = kwargs.get("sociallogin")
org = Organization.objects.filter(user=social.user).first()
if not org:
org = create_blank_org(social.user)

update_user_org(org, social.token, in_auth_flow=True)
user = social.user
if user.is_superuser:
return

org = update_user_information(user, social.token)

if org:
update_user_org(org, social.token, in_auth_flow=True)


@receiver(pre_social_login)
Expand Down

0 comments on commit 5d7a709

Please sign in to comment.