Skip to content

Commit

Permalink
Merge pull request #20532 from kbrock/lower_name
Browse files Browse the repository at this point in the history
introduce User.lower_userid to remove some arel across a few repos
  • Loading branch information
Fryguy committed Jan 14, 2021
2 parents 2cadb1d + 668e834 commit a23cd5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/authenticator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def failure_reason(_username, _request)

def case_insensitive_find_by_userid(username)
user = User.lookup_by_userid(username)
user || User.in_my_region.where('lower(userid) = ?', username.downcase).order(:lastlogon).last
user || User.in_my_region.where(:lower_userid => username.downcase).order(:lastlogon).last
end

def userid_for(_identity, username)
Expand Down
18 changes: 16 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,26 @@ def self.lookup_by_email(email)
# often we have the most probably user object onhand. so use that if possible
def self.lookup_by_lower_email(email, cache = [])
email = email.downcase
Array.wrap(cache).detect { |u| u.email.try(:downcase) == email } || find_by(['lower(email) = ?', email])
Array.wrap(cache).detect { |u| u.lower_email == email } || find_by(:lower_email => email)
end

singleton_class.send(:alias_method, :find_by_lower_email, :lookup_by_lower_email)
Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_lower_email => :lookup_by_lower_email)

def lower_email
email&.downcase
end

virtual_attribute :lower_email, :string, :arel => ->(t) { t.grouping(t[:email].lower) }
hide_attribute :lower_email

def lower_userid
userid&.downcase
end

virtual_attribute :lower_userid, :string, :arel => ->(t) { t.grouping(t[:userid].lower) }
hide_attribute :lower_userid

virtual_column :ldap_group, :type => :string, :uses => :current_group
# FIXME: amazon_group too?
virtual_column :miq_group_description, :type => :string, :uses => :current_group
Expand Down Expand Up @@ -333,7 +347,7 @@ def regional_users
end

def self.regional_users(user)
where(arel_table.grouping(Arel::Nodes::NamedFunction.new("LOWER", [arel_attribute(:userid)]).eq(user.userid.downcase)))
where(:lower_userid => user.userid.downcase)
end

def self.super_admin
Expand Down
2 changes: 1 addition & 1 deletion lib/token_store/sql_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def session_key(token)
end

def find_user_by_userid(userid)
User.in_my_region.where('lower(userid) = ?', userid.downcase).first
User.in_my_region.where(:lower_userid => userid.downcase).first
end

def find_user_by_id(id)
Expand Down
2 changes: 1 addition & 1 deletion tools/miq_config_sssd_ldap/configure_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def dn_to_upn(userid)

def find_user(userid)
user = User.lookup_by_userid(userid)
user || User.in_my_region.where('lower(userid) = ?', userid).order(:lastlogon).last
user || User.in_my_region.where(:lower_userid => userid).order(:lastlogon).last
end
end
end

0 comments on commit a23cd5b

Please sign in to comment.