2014-02-03 19:24:49 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-05-21 22:30:09 +00:00
|
|
|
class SearchController < ApplicationController
|
2015-05-07 11:23:55 +00:00
|
|
|
before_action :authentication_check
|
2013-05-21 22:30:09 +00:00
|
|
|
|
2015-08-05 09:47:00 +00:00
|
|
|
# GET|POST /api/v1/search/:objects
|
|
|
|
|
|
|
|
def search_generic
|
2014-09-19 21:35:40 +00:00
|
|
|
|
|
|
|
# enable search only for agents and admins
|
2015-05-08 08:15:45 +00:00
|
|
|
if !current_user.role?(Z_ROLENAME_AGENT) && !current_user.role?(Z_ROLENAME_ADMIN)
|
2014-09-19 21:35:40 +00:00
|
|
|
response_access_deny
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# get params
|
|
|
|
query = params[:query]
|
|
|
|
limit = params[:limit] || 10
|
|
|
|
|
2015-08-05 09:47:00 +00:00
|
|
|
# convert objects string into array of class names
|
|
|
|
# e.g. user-ticket-another_object = %w( User Ticket AnotherObject )
|
2015-08-05 09:48:21 +00:00
|
|
|
objects = params[:objects].split('-').map(&:camelize)
|
2015-08-05 11:58:53 +00:00
|
|
|
search_tickets = objects.delete('Ticket')
|
2015-08-05 09:47:00 +00:00
|
|
|
|
2014-09-19 21:35:40 +00:00
|
|
|
# try search index backend
|
|
|
|
assets = {}
|
|
|
|
result = []
|
|
|
|
if SearchIndexBackend.enabled?
|
2015-08-05 09:47:00 +00:00
|
|
|
items = SearchIndexBackend.search( query, limit, objects )
|
2014-09-19 21:35:40 +00:00
|
|
|
items.each { |item|
|
|
|
|
require item[:type].to_filename
|
|
|
|
record = Kernel.const_get( item[:type] ).find( item[:id] )
|
|
|
|
assets = record.assets(assets)
|
|
|
|
result.push item
|
|
|
|
}
|
2015-08-05 11:58:53 +00:00
|
|
|
|
|
|
|
# do ticket query by Ticket class to handle ticket permissions
|
|
|
|
if search_tickets
|
|
|
|
tickets = Ticket.search(
|
|
|
|
query: query,
|
|
|
|
limit: limit,
|
|
|
|
current_user: current_user,
|
|
|
|
)
|
|
|
|
tickets.each do |ticket|
|
|
|
|
assets = ticket.assets(assets)
|
|
|
|
item = {
|
|
|
|
id: ticket.id,
|
|
|
|
type: 'Ticket',
|
|
|
|
}
|
|
|
|
result.push item
|
|
|
|
end
|
|
|
|
end
|
2014-09-19 21:35:40 +00:00
|
|
|
else
|
2015-08-05 11:58:53 +00:00
|
|
|
|
2014-09-19 21:35:40 +00:00
|
|
|
# do query
|
2015-08-05 09:47:00 +00:00
|
|
|
objects.each { |object|
|
|
|
|
|
|
|
|
found_objects = object.constantize.search(
|
|
|
|
query: query,
|
|
|
|
limit: limit,
|
|
|
|
current_user: current_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
found_objects.each do |found_object|
|
|
|
|
item = {
|
|
|
|
id: found_object.id,
|
|
|
|
type: found_object.class.to_s
|
|
|
|
}
|
|
|
|
result.push item
|
|
|
|
assets = found_object.assets(assets)
|
|
|
|
end
|
|
|
|
}
|
2014-09-19 21:35:40 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
assets: assets,
|
|
|
|
result: result,
|
2014-09-19 21:35:40 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/search
|
2013-05-21 22:30:09 +00:00
|
|
|
def search
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2015-08-05 11:58:53 +00:00
|
|
|
# get params
|
|
|
|
query = params[:term]
|
|
|
|
limit = params[:limit] || 10
|
|
|
|
|
2015-08-05 09:47:00 +00:00
|
|
|
assets = {}
|
|
|
|
result = []
|
2015-08-05 14:59:14 +00:00
|
|
|
objects = %w( Ticket User Organization )
|
2015-08-05 09:47:00 +00:00
|
|
|
if SearchIndexBackend.enabled?
|
2013-05-21 22:30:09 +00:00
|
|
|
|
2015-08-05 14:59:14 +00:00
|
|
|
# to ticket search in serparate call
|
|
|
|
objects.delete('Ticket')
|
|
|
|
|
2015-08-05 11:58:53 +00:00
|
|
|
# to query search index backend (excluse tickets here, see below)
|
2015-08-05 09:47:00 +00:00
|
|
|
found_objects = {}
|
2015-08-05 14:59:14 +00:00
|
|
|
items = SearchIndexBackend.search( query, limit, objects )
|
2015-08-05 09:47:00 +00:00
|
|
|
items.each { |item|
|
|
|
|
require item[:type].to_filename
|
|
|
|
record = Kernel.const_get( item[:type] ).find( item[:id] )
|
|
|
|
assets = record.assets(assets)
|
2013-05-21 22:30:09 +00:00
|
|
|
|
2015-08-05 09:47:00 +00:00
|
|
|
found_objects[ item[:type] ] ||= []
|
|
|
|
found_objects[ item[:type] ].push item[:id]
|
|
|
|
}
|
2013-05-22 01:40:24 +00:00
|
|
|
|
2015-08-05 11:58:53 +00:00
|
|
|
# do ticket query by Ticket class to handle ticket permissions
|
|
|
|
tickets = Ticket.search(
|
|
|
|
query: query,
|
|
|
|
limit: limit,
|
|
|
|
current_user: current_user,
|
|
|
|
)
|
|
|
|
tickets.each do |ticket|
|
|
|
|
found_objects[ 'Ticket' ] ||= []
|
|
|
|
found_objects[ 'Ticket' ].push ticket.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# generate whole result
|
2015-08-05 09:47:00 +00:00
|
|
|
found_objects.each { |object, object_ids|
|
2013-05-21 22:30:09 +00:00
|
|
|
|
2015-08-05 09:47:00 +00:00
|
|
|
data = {
|
|
|
|
name: object,
|
|
|
|
ids: object_ids,
|
|
|
|
}
|
|
|
|
result.push data
|
2013-05-21 22:30:09 +00:00
|
|
|
}
|
2015-08-05 09:47:00 +00:00
|
|
|
else
|
|
|
|
|
|
|
|
objects.each { |object|
|
|
|
|
|
|
|
|
found_objects = object.constantize.search(
|
2015-08-05 11:58:53 +00:00
|
|
|
query: query,
|
|
|
|
limit: limit,
|
2015-08-05 09:47:00 +00:00
|
|
|
current_user: current_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
object_ids = []
|
|
|
|
found_objects.each do |found_object|
|
|
|
|
object_ids.push found_object.id
|
|
|
|
assets = found_object.assets(assets)
|
|
|
|
end
|
|
|
|
|
|
|
|
next if object_ids.empty?
|
|
|
|
|
|
|
|
data = {
|
|
|
|
name: object,
|
|
|
|
ids: object_ids,
|
|
|
|
}
|
|
|
|
result.push data
|
2013-05-21 22:30:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
assets: assets,
|
|
|
|
result: result,
|
2013-05-21 22:30:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|