Skip to content

Commit

Permalink
Use affiliation attribute to automatically credential faculty users.
Browse files Browse the repository at this point in the history
[T-CAIREM 1136]
[sc-1136]
  • Loading branch information
matkaczmarek committed Dec 2, 2024
1 parent c4c9a31 commit 5c30af4
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion physionet-django/sso/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.contrib.auth import authenticate
from django.contrib.auth import login as auth_login
from django.contrib.auth.tokens import default_token_generator
from django.db import transaction
from django.db import transaction, DatabaseError
from django.shortcuts import redirect, render
from django.utils import timezone
from django.utils.encoding import force_bytes, force_str
Expand Down Expand Up @@ -35,6 +35,7 @@ def get(self, request, *args, **kwargs):
return redirect(self.get_success_url())

remote_sso_id = self.request.META.get(settings.SSO_REMOTE_USER_HEADER)
remote_user_affiliation = self.request.META.get("HTTP_REMOTE_USER_AFFILIATION")

# This should not happen as the SSO_REMOTE_USER_HEADER header should be always set by Nginx
if remote_sso_id is None:
Expand All @@ -47,6 +48,9 @@ def get(self, request, *args, **kwargs):
# Remote user seen for the first time, redirect to SSO registration form
return redirect('sso_register')

if _should_credential_user(user, remote_user_affiliation):
_mark_user_as_credentialed(user)

return redirect(self.get_success_url())


Expand All @@ -71,6 +75,7 @@ def sso_register(request):
return redirect('project_home')

remote_sso_id = request.META.get(settings.SSO_REMOTE_USER_HEADER)
remote_user_affiliation = request.META.get("REMOTE-USER-AFFILIATION")

# This should not happen as the SSO_REMOTE_USER_HEADER header should be always set by Nginx
if not remote_sso_id:
Expand All @@ -83,6 +88,9 @@ def sso_register(request):
user = form.save()
uidb64 = force_str(urlsafe_base64_encode(force_bytes(user.pk)))
token = default_token_generator.make_token(user)
if _should_credential_user(user, remote_user_affiliation):
_mark_user_as_credentialed(user)

notify_account_registration(request, user, uidb64, token, sso=True)
return render(request, 'user/register_done.html', {'email': user.email, 'sso': True})
else:
Expand Down Expand Up @@ -132,3 +140,21 @@ def sso_activate_user(request, uidb64, token):
return redirect('project_home')

return render(request, 'user/activate_user_complete.html', context)


def _should_credential_user(user, remote_user_affiliation):
if user.is_credentialed:
return False

if remote_user_affiliation is None:
return False

return "faculty" in remote_user_affiliation


def _mark_user_as_credentialed(user):
with transaction.atomic():
# update the user credentials
user.is_credentialed = True
user.credential_datetime = timezone.now()
user.save()

0 comments on commit 5c30af4

Please sign in to comment.