Skip to content
Snippets Groups Projects

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

3 files
+ 29
27
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -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
Loading