search.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class User
  3. module Search
  4. extend ActiveSupport::Concern
  5. include CanSearch
  6. included do
  7. scope :search_sql_extension, lambda { |params|
  8. statement = all
  9. if params[:role_ids]
  10. statement = statement.joins(:roles).where('roles.id' => params[:role_ids])
  11. end
  12. if params[:group_ids]
  13. user_ids = []
  14. params[:group_ids].each do |group_id, access|
  15. user_ids |= User.group_access(group_id.to_i, access).pluck(:id)
  16. end
  17. statement = if user_ids.present?
  18. statement.where(id: user_ids)
  19. else
  20. statement.none
  21. end
  22. end
  23. # Fixes #3755 - User with user_id 1 is show in admin interface (which should not)
  24. statement.where('users.id != 1')
  25. }
  26. end
  27. # methods defined here are going to extend the class, not the instance of it
  28. class_methods do
  29. =begin
  30. search user preferences
  31. result = User.search_preferences(user_model)
  32. returns if user has permissions to search
  33. result = {
  34. prio: 1000,
  35. direct_search_index: true
  36. }
  37. returns if user has no permissions to search
  38. result = false
  39. =end
  40. def search_preferences(current_user)
  41. return false if !current_user.permissions?(['ticket.agent', 'admin.user'])
  42. {
  43. prio: 2000,
  44. direct_search_index: true,
  45. }
  46. end
  47. def search_default_sort_by
  48. %w[active updated_at]
  49. end
  50. def search_default_order_by
  51. %w[desc desc]
  52. end
  53. def search_params_pre(params)
  54. return if params[:permissions].blank?
  55. params[:role_ids] ||= []
  56. params[:role_ids] |= Role.with_permissions(params[:permissions]).pluck(:id)
  57. end
  58. def search_query_extension(params)
  59. query_extension = {}
  60. if params[:role_ids].present?
  61. query_extension['bool'] ||= {}
  62. query_extension['bool']['must'] ||= []
  63. if !params[:role_ids].is_a?(Array)
  64. params[:role_ids] = [params[:role_ids]]
  65. end
  66. access_condition = {
  67. 'query_string' => { 'default_field' => 'role_ids', 'query' => "\"#{params[:role_ids].join('" OR "')}\"" }
  68. }
  69. query_extension['bool']['must'].push access_condition
  70. end
  71. if params[:group_ids].present?
  72. user_ids = []
  73. params[:group_ids].each do |group_id, access|
  74. user_ids |= User.group_access(group_id.to_i, access).pluck(:id)
  75. end
  76. if user_ids.present?
  77. query_extension['bool'] ||= {}
  78. query_extension['bool']['must'] ||= []
  79. query_extension['bool']['must'].push({ 'terms' => { '_id' => user_ids } })
  80. else
  81. query_extension = {
  82. bool: {
  83. must: [
  84. {
  85. 'query_string' => { 'query' => 'id:0' }
  86. },
  87. ],
  88. }
  89. }
  90. end
  91. end
  92. query_extension
  93. end
  94. end
  95. end
  96. end