Consolidate account scopes for LOWER (index using) username/domain queries (#30451)
				
					
				
			This commit is contained in:
		@@ -25,42 +25,11 @@ module Account::FinderConcern
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def find_remote(username, domain)
 | 
			
		||||
      AccountFinder.new(username, domain).account
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  class AccountFinder
 | 
			
		||||
    attr_reader :username, :domain
 | 
			
		||||
 | 
			
		||||
    def initialize(username, domain)
 | 
			
		||||
      @username = username
 | 
			
		||||
      @domain = domain
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def account
 | 
			
		||||
      scoped_accounts.order(id: :asc).take
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def scoped_accounts
 | 
			
		||||
      Account.unscoped.tap do |scope|
 | 
			
		||||
        scope.merge! with_usernames
 | 
			
		||||
        scope.merge! matching_username
 | 
			
		||||
        scope.merge! matching_domain
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def with_usernames
 | 
			
		||||
      Account.where.not(Account.arel_table[:username].lower.eq '')
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def matching_username
 | 
			
		||||
      Account.where(Account.arel_table[:username].lower.eq username.to_s.downcase)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def matching_domain
 | 
			
		||||
      Account.where(Account.arel_table[:domain].lower.eq(domain.nil? ? nil : domain.to_s.downcase))
 | 
			
		||||
      Account
 | 
			
		||||
        .with_username(username)
 | 
			
		||||
        .with_domain(domain)
 | 
			
		||||
        .order(id: :asc)
 | 
			
		||||
        .take
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ module User::LdapAuthenticable
 | 
			
		||||
        safe_username = safe_username.gsub(keys, replacement)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      resource = joins(:account).merge(Account.where(Account.arel_table[:username].lower.eq safe_username.downcase)).take
 | 
			
		||||
      resource = joins(:account).merge(Account.with_username(safe_username)).take
 | 
			
		||||
 | 
			
		||||
      if resource.blank?
 | 
			
		||||
        resource = new(
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user