Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce User.lower_userid to remove some arel across a few repos #20532

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
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
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) }
Copy link
Member

Choose a reason for hiding this comment

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

Only thing I don't like about this is that it's now exposed to reporting.

Copy link
Member Author

@kbrock kbrock Sep 10, 2020

Choose a reason for hiding this comment

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

we need to come up with a way to hide attributes from reporting and from the api
This is also useful when we want to deprecate a fields. Have them work but not show up in the drop downs/documentation

I have had an idea mulling in my head for a while, but never know if this is viable or not - due to the complexity of add ing a concept that isn't really in rails at this time. attributes and hidden_attributes kinda like methods (privatet_methods) and public_methods?

Copy link
Member

@Fryguy Fryguy Sep 10, 2020

Choose a reason for hiding this comment

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

I like the public/private designation idea. What if we just literally make them private, then in reporting, as we enumerate the methods we only take public virtual methods?

private

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

and/or

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

Copy link
Member Author

Choose a reason for hiding this comment

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

If we are using send to call these parameters, then that will require changes in using them
but when we are constructing the api or the drop down, we can look at them.

Nice to not introduce any new metadata

I do wonder how this will work if we use the class when it hasn't been used yet. the attributes may not be defined yet.

will look into this

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