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

Fix IntegrityError when user has multiple email addresses #29

Merged
merged 1 commit into from
Apr 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions sentry_ldap_auth/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,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 = ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the currently "empty" getsentry-ldap-auth email from ' ' to '', possibly leaving dangling database rows with 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()
Copy link

@aleksihakli aleksihakli Apr 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something like

from django.db.models import Q

# ...

UserEmail.objects.filter(user=user, Q(email='') | Q(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