From e4fd1190f171012f5ec409020acfe01bf1d6ae70 Mon Sep 17 00:00:00 2001 From: John Davis Date: Tue, 3 Oct 2023 00:34:00 -0400 Subject: [PATCH] Refactor get user by email --- lib/galaxy/managers/users.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/galaxy/managers/users.py b/lib/galaxy/managers/users.py index 5dbef0f9844f..1ccc22502dee 100644 --- a/lib/galaxy/managers/users.py +++ b/lib/galaxy/managers/users.py @@ -359,8 +359,7 @@ def get_user_by_identity(self, identity): user = get_user_by_email(self.session(), identity, self.model_class) if not user: # Try a case-insensitive match on the email - stmt = select(self.model_class).where(func.lower(self.model_class.email) == identity.lower()).limit(1) - user = self.session().scalars(stmt).first() + user = self._get_user_by_email_case_insensitive(self.session(), identity) else: user = get_user_by_username(self.session(), identity, self.model_class) return user @@ -572,8 +571,7 @@ def send_reset_email(self, trans, payload, **kwd): def get_reset_token(self, trans, email): reset_user = get_user_by_email(trans.sa_session, email, self.app.model.User) if not reset_user and email != email.lower(): - stmt = select(self.app.model.User).where(func.lower(self.app.model.User.email) == email.lower()).limit(1) - reset_user = trans.sa_session.scalars(stmt).first() + reset_user = self._get_user_by_email_case_insensitive(trans.sa_session, email) if reset_user: prt = self.app.model.PasswordResetToken(reset_user) trans.sa_session.add(prt) @@ -650,6 +648,10 @@ def get_or_create_remote_user(self, remote_user_email): # self.log_event( "Automatically created account '%s'", user.email ) return user + def _get_user_by_email_case_insensitive(self, session, email): + stmt = select(self.app.model.User).where(func.lower(self.app.model.User.email) == email.lower()).limit(1) + return session.scalars(stmt).first() + class UserSerializer(base.ModelSerializer, deletable.PurgableSerializerMixin): model_manager_class = UserManager