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
|
|
|
|
2014-09-19 21:35:40 +00:00
|
|
|
# GET /api/v1/search_user_org
|
|
|
|
def search_user_org
|
|
|
|
|
|
|
|
# enable search only for agents and admins
|
2015-02-15 10:01:12 +00:00
|
|
|
if !current_user.is_role(Z_ROLENAME_AGENT) && !current_user.is_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
|
|
|
|
|
|
|
|
# try search index backend
|
|
|
|
assets = {}
|
|
|
|
result = []
|
|
|
|
if SearchIndexBackend.enabled?
|
2015-05-07 07:23:16 +00:00
|
|
|
items = SearchIndexBackend.search( query, limit, %w(User Organization) )
|
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
|
|
|
|
}
|
|
|
|
else
|
|
|
|
# do query
|
|
|
|
users = User.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
query: query,
|
|
|
|
limit: limit,
|
|
|
|
current_user: current_user,
|
2014-09-19 21:35:40 +00:00
|
|
|
)
|
|
|
|
user_result = []
|
|
|
|
users.each do |user|
|
|
|
|
item = {
|
2015-04-27 13:42:53 +00:00
|
|
|
id: user.id,
|
|
|
|
type: user.class.to_s
|
2014-09-19 21:35:40 +00:00
|
|
|
}
|
|
|
|
result.push item
|
|
|
|
assets = user.assets(assets)
|
|
|
|
end
|
|
|
|
|
|
|
|
organizations = Organization.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
query: query,
|
|
|
|
limit: limit,
|
|
|
|
current_user: current_user,
|
2014-09-19 21:35:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
organization_result = []
|
|
|
|
organizations.each do |organization|
|
|
|
|
item = {
|
2015-04-27 13:42:53 +00:00
|
|
|
id: organization.id,
|
|
|
|
type: organization.class.to_s
|
2014-09-19 21:35:40 +00:00
|
|
|
}
|
|
|
|
result.push item
|
|
|
|
assets = organization.assets(assets)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2013-05-21 22:30:09 +00:00
|
|
|
# build result list
|
|
|
|
tickets = Ticket.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
limit: params[:limit],
|
|
|
|
query: params[:term],
|
|
|
|
current_user: current_user,
|
2013-05-21 22:30:09 +00:00
|
|
|
)
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = {}
|
2013-05-21 22:30:09 +00:00
|
|
|
ticket_result = []
|
|
|
|
tickets.each do |ticket|
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = ticket.assets(assets)
|
2013-05-21 22:30:09 +00:00
|
|
|
ticket_result.push ticket.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# do query
|
|
|
|
users = User.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
query: params[:term],
|
|
|
|
limit: params[:limit],
|
|
|
|
current_user: current_user,
|
2013-05-21 22:30:09 +00:00
|
|
|
)
|
|
|
|
user_result = []
|
|
|
|
users.each do |user|
|
|
|
|
user_result.push user.id
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = user.assets(assets)
|
2013-05-21 22:30:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
organizations = Organization.search(
|
2015-04-27 13:42:53 +00:00
|
|
|
query: params[:term],
|
|
|
|
limit: params[:limit],
|
|
|
|
current_user: current_user,
|
2013-05-21 22:30:09 +00:00
|
|
|
)
|
2013-05-22 01:40:24 +00:00
|
|
|
|
2013-05-21 22:30:09 +00:00
|
|
|
organization_result = []
|
|
|
|
organizations.each do |organization|
|
|
|
|
organization_result.push organization.id
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = organization.assets(assets)
|
2013-05-21 22:30:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
result = []
|
|
|
|
if ticket_result[0]
|
|
|
|
data = {
|
2015-04-27 13:42:53 +00:00
|
|
|
name: 'Ticket',
|
|
|
|
ids: ticket_result,
|
2013-05-21 22:30:09 +00:00
|
|
|
}
|
|
|
|
result.push data
|
|
|
|
end
|
|
|
|
if user_result[0]
|
|
|
|
data = {
|
2015-04-27 13:42:53 +00:00
|
|
|
name: 'User',
|
|
|
|
ids: user_result,
|
2013-05-21 22:30:09 +00:00
|
|
|
}
|
|
|
|
result.push data
|
|
|
|
end
|
|
|
|
if organization_result[0]
|
|
|
|
data = {
|
2015-04-27 13:42:53 +00:00
|
|
|
name: 'Organization',
|
|
|
|
ids: organization_result,
|
2013-05-21 22:30:09 +00:00
|
|
|
}
|
|
|
|
result.push data
|
|
|
|
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
|