Skip to content
Snippets Groups Projects
Commit 226e755d authored by Eric's avatar Eric
Browse files

Merge branch '160-dont-update-profile-info-on-every-api-call' into 'master'

Resolve "dont update profile info on every api call"

Closes #160

See merge request !84
parents 0ade3049 3c7b77c7
No related branches found
No related tags found
1 merge request!84Resolve "dont update profile info on every api call"
...@@ -132,7 +132,7 @@ class LDAPUserForm(forms.Form): ...@@ -132,7 +132,7 @@ class LDAPUserForm(forms.Form):
class ProfileForm(forms.ModelForm): class ProfileForm(forms.ModelForm):
class Meta: class Meta:
model = Profile model = Profile
exclude = ('user', 'accountname', 'api_token') exclude = ('user', 'accountname', 'api_token', 'update_timestamp')
widgets = {'labs': forms.CheckboxSelectMultiple()} widgets = {'labs': forms.CheckboxSelectMultiple()}
......
...@@ -4,9 +4,10 @@ from django.contrib.auth.signals import user_logged_in ...@@ -4,9 +4,10 @@ from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver from django.dispatch import receiver
from django_auth_ldap.backend import LDAPBackend, populate_user from django_auth_ldap.backend import LDAPBackend, populate_user
from django.db import transaction from django.db import transaction
from django.utils import timezone
from .models import Profile, Lab from .models import Profile, Lab
@receiver(user_logged_in) @receiver(user_logged_in)
@transaction.atomic @transaction.atomic
def user_login(sender, user, **kwargs): def user_login(sender, user, **kwargs):
...@@ -14,24 +15,25 @@ def user_login(sender, user, **kwargs): ...@@ -14,24 +15,25 @@ def user_login(sender, user, **kwargs):
Update user association with IRIC labs. External lab associations are to be managed manually. Update user association with IRIC labs. External lab associations are to be managed manually.
""" """
azuread_data = {} if ((timezone.now() - user.profile.update_timestamp).total_seconds() / 60) > 15:
try: azuread_data = {}
azuread_data = user.social_auth.get(provider='azuread-tenant-oauth2').extra_data try:
except: azuread_data = user.social_auth.get(provider='azuread-tenant-oauth2').extra_data
pass except:
azuread_groups = azuread_data['groups'] if 'groups' in azuread_data else None pass
azuread_samaccountname = azuread_data['onPremisesSamAccountName'] if 'onPremisesSamAccountName' in azuread_data else None 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: if azuread_samaccountname and not user.profile.accountname:
user.profile.accountname = azuread_samaccountname user.profile.accountname = azuread_samaccountname
if hasattr(user, 'ldapuser') or azuread_groups: if hasattr(user, 'ldapuser') or azuread_groups:
user.profile.labs.remove(*Lab.objects.filter(ldap__startswith='iric-')) user.profile.labs.remove(*Lab.objects.filter(ldap__startswith='iric-'))
if hasattr(user, 'ldapuser'): if hasattr(user, 'ldapuser'):
user.profile.labs.add(*Lab.objects.filter(ldap__in=user.ldap_user.group_names)) user.profile.labs.add(*Lab.objects.filter(ldap__in=user.ldap_user.group_names))
elif azuread_groups: elif azuread_groups:
user.profile.labs.add(*Lab.objects.filter(ldap__in=azuread_groups)) user.profile.labs.add(*Lab.objects.filter(ldap__in=azuread_groups))
user.profile.save() user.profile.save()
@receiver(populate_user, sender=LDAPBackend) @receiver(populate_user, sender=LDAPBackend)
......
# Generated by Django 2.2.17 on 2022-12-23 16:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('portal', '0027_auto_20210311_1422'),
]
operations = [
migrations.AddField(
model_name='profile',
name='update_timestamp',
field=models.DateTimeField(auto_now=True, verbose_name='Last Update Timestamp'),
),
]
...@@ -36,6 +36,8 @@ class Profile(models.Model): ...@@ -36,6 +36,8 @@ class Profile(models.Model):
api_token = models.CharField(max_length=64, null=True, unique=True, blank=False, default=None) api_token = models.CharField(max_length=64, null=True, unique=True, blank=False, default=None)
update_timestamp = models.DateTimeField(verbose_name=_('Last Update Timestamp'), auto_now=True)
objects = ProfileManager() objects = ProfileManager()
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
......
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