123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- module Ticket::Search
- extend ActiveSupport::Concern
- include CanSearch
- included do
- scope :search_sql_query_extension, lambda { |params|
- return if params[:query].blank?
- fields = %w[title number]
- fields << Ticket::Article.arel_table[:body]
- fields << Ticket::Article.arel_table[:from]
- fields << Ticket::Article.arel_table[:to]
- fields << Ticket::Article.arel_table[:subject]
- where_or_cis(fields, "%#{SqlHelper.quote_like(params[:query])}%")
- .joins(:articles)
- }
- end
- # methods defined here are going to extend the class, not the instance of it
- class_methods do
- =begin
- search tickets preferences
- result = Ticket.search_preferences(user_model)
- returns if user has permissions to search
- result = {
- prio: 3000,
- direct_search_index: false
- }
- returns if user has no permissions to search
- result = false
- =end
- def search_preferences(current_user)
- return false if !current_user.permissions?(['ticket.agent', 'ticket.customer'])
- {
- prio: 3000,
- direct_search_index: false,
- }
- end
- def search_params_pre(params)
- params[:scope] ||= TicketPolicy::ReadScope
- end
- def search_query_extension(params)
- query_or = []
- if params[:current_user].permissions?('ticket.agent')
- group_ids = params[:current_user].group_ids_access(params[:scope].const_get(:ACCESS_TYPE))
- if group_ids.present?
- access_condition = {
- 'query_string' => { 'default_field' => 'group_id', 'query' => "\"#{group_ids.join('" OR "')}\"" }
- }
- query_or.push(access_condition)
- end
- end
- if params[:current_user].permissions?('ticket.customer')
- organizations_query = params[:current_user].all_organizations.where(shared: true).map { |row| "organization_id:#{row.id}" }.join(' OR ')
- access_condition = if organizations_query.present?
- {
- 'query_string' => { 'query' => "customer_id:#{params[:current_user].id} OR #{organizations_query}" }
- }
- else
- {
- 'query_string' => { 'default_field' => 'customer_id', 'query' => params[:current_user].id }
- }
- end
- query_or.push(access_condition)
- end
- if query_or.blank?
- return {
- bool: {
- must: [
- {
- 'query_string' => { 'query' => 'id:0' }
- },
- ],
- }
- }
- end
- {
- bool: {
- must: [
- {
- bool: {
- should: query_or,
- },
- },
- ],
- }
- }
- end
- end
- end
|