mozreview: Don't overwrite valid ldap associations (bug 1287988) r?smacleod draft
authorbyron jones <glob@mozilla.com>
Fri, 22 Jul 2016 14:28:34 +0800
changeset 9139 d83a44611ab1e6e5be1c629bf43822003a919dc1
parent 9138 3149b5800858fb66b22ae205a1ef28a9083270ba
child 9140 aa211f087688b5432f2bc0fcceeb8b945da19fa9
push id1091
push userbjones@mozilla.com
push dateThu, 11 Aug 2016 05:18:30 +0000
reviewerssmacleod
bugs1287988
mozreview: Don't overwrite valid ldap associations (bug 1287988) r?smacleod Ensure that if a user's ldap association is correct, we don't overwrite the stored link. This resolves a bug impacting Mozilla employees who use a different email address in LDAP vs BMO. MozReview-Commit-ID: A49fGBzPrX9
pylib/mozreview/mozreview/ldap/__init__.py
--- a/pylib/mozreview/mozreview/ldap/__init__.py
+++ b/pylib/mozreview/mozreview/ldap/__init__.py
@@ -138,29 +138,35 @@ def associate_employee_ldap(user, ldap_c
     checked), and associate the user to LDAP.
 
     Raises an exception if no matches or multiple matches were found.
     Returns the found LDAP username, and a boolean indicating if the user
     was updated.
     """
 
     from mozreview.models import get_profile
+    mozreview_profile = get_profile(user)
+
+    # Don't overwrite existing and valid associations.
+    if mozreview_profile.ldap_username and user_exists(
+            mozreview_profile.ldap_username, ldap_connection):
+        return mozreview_profile.ldap_username, False
+
     ldap_users = find_employee_ldap(user.email, ldap_connection)
 
     if not ldap_users:
         raise LDAPAssociationException(
             'Failed to find match for %s' % user.email)
 
     if len(ldap_users) > 1:
         raise LDAPAssociationException(
             'More than one match for %s' % user.email)
 
     ldap_username = ldap_users[0]
     updated = False
-    mozreview_profile = get_profile(user)
     if mozreview_profile.ldap_username != ldap_username:
         if mozreview_profile.ldap_username:
             logging.info("Existing ldap association '%s' replaced by '%s'"
                          % (mozreview_profile.ldap_username, ldap_username))
         else:
             logging.info('Associating user: %s with ldap_username: %s'
                          % (user.email, ldap_username))