Skip to content
This repository has been archived by the owner on Sep 24, 2023. It is now read-only.

Commit

Permalink
Fix IntegrityError when user has multiple email addresses (#29)
Browse files Browse the repository at this point in the history
Since you can set up multiple email addresses for users, it is possible that sentry_ldap_auth updates the wrong email address when changing the UserEmail field. 

Say, you have two UserEmail objects for an user, 'personal' and 'system-wide'. When 'personal' gets updated to "system-wide", this generates an IntegrityError because (user, email) is unique in the database, preventing logon.

I doubt this entire structure is even necessary because when the 'email' attribute is set correctly on ``AUTH_LDAP_USER_ATTR_MAP`` this should all happen automatically.
  • Loading branch information
ralphje authored and barronhagerman committed Apr 20, 2018
1 parent f06770f commit 33d74bb
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions sentry_ldap_auth/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,23 @@ def get_or_create_user(self, username, ldap_user):

user.is_managed = True

# Add the user email address
try:
from sentry.models import (UserEmail)
except ImportError:
pass
else:
userEmail = UserEmail.objects.filter(user=user)
if not userEmail:
userEmail = UserEmail.objects.create(user=user)
else:
userEmail = userEmail[0]

if not hasattr(settings, 'AUTH_LDAP_DEFAULT_EMAIL_DOMAIN'):
email = ' '
else:
email = username + '@' + settings.AUTH_LDAP_DEFAULT_EMAIL_DOMAIN

if 'mail' in ldap_user.attrs:
userEmail.email = ldap_user.attrs.get('mail')[0]
email = ldap_user.attrs.get('mail')[0]
elif not hasattr(settings, 'AUTH_LDAP_DEFAULT_EMAIL_DOMAIN'):
email = ''
else:
userEmail.email = email
userEmail.save()
email = username + '@' + settings.AUTH_LDAP_DEFAULT_EMAIL_DOMAIN

# django-auth-ldap may have accidentally created an empty email address
UserEmail.objects.filter(user=user, email='').delete()
if email:
UserEmail.objects.get_or_create(user=user, email=email)

# Check to see if we need to add the user to an organization
if not settings.AUTH_LDAP_DEFAULT_SENTRY_ORGANIZATION:
Expand Down

0 comments on commit 33d74bb

Please sign in to comment.