Skip to content
Snippets Groups Projects
Commit 8ac03362 authored by Eric's avatar Eric
Browse files

Merge branch '152-lab-association-not-done-when-logging-in-with-token' into 'master'

Resolve "lab association not done when logging in with token"

Closes #152

See merge request !80
parents c3d798f1 141c5bc9
No related branches found
No related tags found
1 merge request!80Resolve "lab association not done when logging in with token"
......@@ -7,5 +7,5 @@ class PortalConfig(AppConfig):
def ready(self):
from .listeners import (
create_external_user_profile, create_user_profile,
create_lab_group, delete_lab_group
create_lab_group, delete_lab_group, user_login
)
from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save, post_delete
from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
from django_auth_ldap.backend import LDAPBackend, populate_user
from .models import Profile, Lab
@receiver(user_logged_in)
def user_login(sender, user, **kwargs):
""" User logged in (any method)
Update user association with IRIC labs. External lab associations are to be managed manually.
"""
azuread_data = {}
try:
azuread_data = user.social_auth.get(provider='azuread-tenant-oauth2').extra_data
except:
pass
azuread_groups = azuread_data['groups'] if 'groups' in azuread_data else None
azuread_samaccountname = azuread_data['onPremisesSamAccountName'] if 'onPremisesSamAccountName' in azuread_data else None
if azuread_samaccountname and not user.profile.accountname:
user.profile.accountname = azuread_samaccountname
if hasattr(user, 'ldapuser') or azuread_groups:
user.profile.labs.remove(*Lab.objects.filter(ldap__startswith='iric-'))
if hasattr(user, 'ldapuser'):
user.profile.labs.add(*Lab.objects.filter(ldap__in=user.ldap_user.group_names))
elif azuread_groups:
user.profile.labs.add(*Lab.objects.filter(ldap__in=azuread_groups))
user.profile.save()
@receiver(populate_user, sender=LDAPBackend)
def create_user_profile(sender, user, ldap_user=None, **kwargs):
......
......@@ -8,34 +8,8 @@ from ...models import Lab
class LoginSuccess(View):
def get(self, request, *args, **kwargs):
"""
Update user association with IRIC labs and redirect to appropriate dashboard
External lab associations are to be managed manually
"""
user = self.request.user
if user.is_anonymous:
return redirect('login')
azuread_data = {}
try:
azuread_data = user.social_auth.get(provider='azuread-tenant-oauth2').extra_data
except:
pass
azuread_groups = azuread_data['groups'] if 'groups' in azuread_data else None
azuread_samaccountname = azuread_data['onPremisesSamAccountName'] if 'onPremisesSamAccountName' in azuread_data else None
if azuread_samaccountname and not user.profile.accountname:
user.profile.accountname = azuread_samaccountname
if hasattr(user, 'ldapuser') or azuread_groups:
user.profile.labs.remove(*Lab.objects.filter(ldap__startswith='iric-'))
if hasattr(user, 'ldapuser'):
user.profile.labs.add(*Lab.objects.filter(ldap__in=user.ldap_user.group_names))
elif azuread_groups:
user.profile.labs.add(*Lab.objects.filter(ldap__in=azuread_groups))
if self.request.user.is_staff:
# user is an admin
request.session['active_lab_id'] = Lab.objects.get(ldap=settings.AUTH_LDAP_STAFF_GROUP.split(',')[0][3:]).id
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment