Unverified Commit b84f10a5 authored by Ahmad Farhat's avatar Ahmad Farhat Committed by GitHub
Browse files

GRN2-xx: Cleaned up manage users query (#1786)

* Cleaned up manage users query

* Bring back filter by role
parent a32a383b
......@@ -37,9 +37,8 @@ class AdminsController < ApplicationController
@search = params[:search] || ""
@order_column = params[:column] && params[:direction] != "none" ? params[:column] : "created_at"
@order_direction = params[:direction] && params[:direction] != "none" ? params[:direction] : "DESC"
@role = params[:role] ? Role.find_by(name: params[:role], provider: @user_domain) : nil
@tab = params[:tab] || "active"
@role = params[:role] ? Role.find_by(name: params[:role], provider: @user_domain) : nil
@user_list = merge_user_list
......
......@@ -21,25 +21,27 @@ module Populator
# Returns a list of users that are in the same context of the current user
def manage_users_list
current_role = @role
initial_list = case @tab
when "active"
User.without_role([:pending, :denied])
when "deleted"
User.deleted
when "pending"
User.with_role(:pending)
when "denied"
User.with_role(:denied)
else
User.all
end
current_role = Role.find_by(name: @tab, provider: @user_domain) if @tab == "pending" || @tab == "denied"
initial_list = initial_list.with_role(@role.name) if @role.present?
initial_list = initial_list.without_role(:super_admin) unless current_user.has_role? :super_admin
initial_list = initial_list.where(provider: @user_domain) if Rails.configuration.loadbalanced_configuration
initial_list.where.not(id: current_user.id)
.admins_search(@search, current_role)
.admins_search(@search)
.admins_order(@order_column, @order_direction)
end
......
......@@ -70,7 +70,9 @@ class User < ApplicationRecord
end
end
def self.admins_search(string, role)
def self.admins_search(string)
return all if string.blank?
active_database = Rails.configuration.database_configuration[Rails.env]["adapter"]
# Postgres requires created_at to be cast to a string
created_at_query = if active_database == "postgresql"
......@@ -79,22 +81,12 @@ class User < ApplicationRecord
"created_at"
end
search_query = ""
role_search_param = ""
if role.nil?
search_query = "users.name LIKE :search OR email LIKE :search OR username LIKE :search" \
" OR users.#{created_at_query} LIKE :search OR users.provider LIKE :search" \
" OR roles.name LIKE :roles_search"
role_search_param = "%#{sanitize_sql_like(string)}%"
else
search_query = "(users.name LIKE :search OR email LIKE :search OR username LIKE :search" \
" OR users.#{created_at_query} LIKE :search OR users.provider LIKE :search)" \
" AND roles.name = :roles_search"
role_search_param = role.name
end
search_query = "users.name LIKE :search OR email LIKE :search OR username LIKE :search" \
" OR users.#{created_at_query} LIKE :search OR users.provider LIKE :search" \
" OR roles.name LIKE :search"
search_param = "%#{sanitize_sql_like(string)}%"
where(search_query, search: search_param, roles_search: role_search_param)
where(search_query, search: search_param)
end
def self.admins_order(column, direction)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment