2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2013-08-19 06:29:49 +00:00
2015-04-27 20:55:17 +00:00
class User
2015-04-27 20:49:17 +00:00
module Search
2013-08-19 06:29:49 +00:00
= begin
2015-08-16 00:53:27 +00:00
search user preferences
result = User . search_preferences ( user_model )
returns if user has permissions to search
result = {
prio : 1000 ,
direct_search_index : true
}
returns if user has no permissions to search
result = false
= end
def search_preferences ( current_user )
2016-08-12 16:39:09 +00:00
return false if ! current_user . permissions? ( 'ticket.agent' ) && ! current_user . permissions? ( 'admin.user' )
2015-08-16 00:53:27 +00:00
{
prio : 2000 ,
direct_search_index : true ,
}
end
= begin
2013-08-19 06:29:49 +00:00
search user
result = User . search (
2016-08-12 16:39:09 +00:00
query : 'some search term' ,
2015-11-30 12:13:27 +00:00
limit : 15 ,
current_user : user_model ,
2013-08-19 06:29:49 +00:00
)
2017-09-11 00:50:05 +00:00
or with certain role_ids | permissions
result = User . search (
query : 'some search term' ,
limit : 15 ,
current_user : user_model ,
role_ids : [ 1 , 2 , 3 ] ,
permissions : [ 'ticket.agent' ]
)
2013-08-19 06:29:49 +00:00
returns
result = [ user_model1 , user_model2 , ... ]
= end
2015-04-27 20:49:17 +00:00
def search ( params )
# get params
query = params [ :query ]
limit = params [ :limit ] || 10
current_user = params [ :current_user ]
# enable search only for agents and admins
2015-08-16 00:53:27 +00:00
return [ ] if ! search_preferences ( current_user )
2015-04-27 20:49:17 +00:00
2017-09-11 00:50:05 +00:00
# lookup for roles of permission
if params [ :permissions ] . present?
params [ :role_ids ] || = [ ]
role_ids = Role . with_permissions ( params [ :permissions ] ) . pluck ( :id )
params [ :role_ids ] . concat ( role_ids )
end
2015-04-27 20:49:17 +00:00
# try search index backend
if SearchIndexBackend . enabled?
2017-09-11 00:50:05 +00:00
query_extention = { }
if params [ :role_ids ] . present?
query_extention [ 'bool' ] = { }
query_extention [ 'bool' ] [ 'must' ] = [ ]
if ! params [ :role_ids ] . is_a? ( Array )
params [ :role_ids ] = [ params [ :role_ids ] ]
end
access_condition = {
'query_string' = > { 'default_field' = > 'role_ids' , 'query' = > " \" #{ params [ :role_ids ] . join ( '" OR "' ) } \" " }
}
query_extention [ 'bool' ] [ 'must' ] . push access_condition
end
items = SearchIndexBackend . search ( query , limit , 'User' , query_extention )
2015-04-27 20:49:17 +00:00
users = [ ]
2017-10-01 12:25:52 +00:00
items . each do | item |
2016-12-13 13:58:13 +00:00
user = User . lookup ( id : item [ :id ] )
next if ! user
users . push user
2017-10-01 12:25:52 +00:00
end
2015-04-27 20:49:17 +00:00
return users
end
# fallback do sql query
# - stip out * we already search for *query* -
2015-08-21 23:55:59 +00:00
query . delete! '*'
2016-01-15 17:22:57 +00:00
users = if params [ :role_ids ]
2016-03-08 06:32:58 +00:00
User . joins ( :roles ) . where ( 'roles.id' = > params [ :role_ids ] ) . where (
2016-01-15 17:22:57 +00:00
'(users.firstname LIKE ? OR users.lastname LIKE ? OR users.email LIKE ? OR users.login LIKE ?) AND users.id != 1' , " % #{ query } % " , " % #{ query } % " , " % #{ query } % " , " % #{ query } % "
2016-09-12 06:52:01 +00:00
) . order ( 'updated_at DESC' ) . limit ( limit )
2016-01-15 17:22:57 +00:00
else
User . where (
'(firstname LIKE ? OR lastname LIKE ? OR email LIKE ? OR login LIKE ?) AND id != 1' , " % #{ query } % " , " % #{ query } % " , " % #{ query } % " , " % #{ query } % "
2016-09-12 06:52:01 +00:00
) . order ( 'updated_at DESC' ) . limit ( limit )
2016-01-15 17:22:57 +00:00
end
2015-04-27 20:49:17 +00:00
users
2014-01-29 23:55:25 +00:00
end
2013-08-19 06:29:49 +00:00
end
2014-02-03 19:23:00 +00:00
end