From bfb507dee53006dce7d5edb41ca10d2b1be519fe Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 2 Feb 2014 19:58:31 +0100 Subject: [PATCH] Added support of group permissions. --- .../application_model/search_index_base.rb | 18 ++++-- app/models/organization/search.rb | 2 +- app/models/ticket.rb | 16 ++++-- app/models/ticket/search.rb | 55 +++++++++++++++---- app/models/user/search.rb | 2 +- app/models/user/search_index.rb | 4 +- lib/search_index_backend.rb | 49 +++++++++++++++-- 7 files changed, 117 insertions(+), 29 deletions(-) diff --git a/app/models/application_model/search_index_base.rb b/app/models/application_model/search_index_base.rb index 4df038a69..fd7f4b76e 100644 --- a/app/models/application_model/search_index_base.rb +++ b/app/models/application_model/search_index_base.rb @@ -20,8 +20,6 @@ returns # default ignored attributes ignore_attributes = { - :created_at => true, - :updated_at => true, :created_by_id => true, :updated_by_id => true, :active => true, @@ -94,12 +92,22 @@ returns =end def search_index_attribute_lookup(attributes, ref_object) + + # default keep attributes + keep_attributes = {} + if self.class.search_index_support_config[:keep_attributes] + self.class.search_index_support_config[:keep_attributes].each {|key, value| + keep_attributes[key] = value + } + end + attributes_new = {} attributes.each {|key, value| next if !value # get attribute name - attribute_name = key.to_s + attribute_name_with_id = key.to_s + attribute_name = key.to_s next if attribute_name[-3,3] != '_id' attribute_name = attribute_name[ 0, attribute_name.length-3 ] @@ -123,7 +131,9 @@ returns # save name of ref object attributes_new[ attribute_name ] = value - attributes.delete(key) + if !keep_attributes[ attribute_name_with_id.to_sym ] + attributes.delete(key) + end } attributes_new.merge(attributes) end diff --git a/app/models/organization/search.rb b/app/models/organization/search.rb index 7d0634ca1..8607e44b6 100644 --- a/app/models/organization/search.rb +++ b/app/models/organization/search.rb @@ -29,7 +29,7 @@ returns return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin') # try search index backend - if Setting.get('es_url') + if SearchIndexBackend.enabled? ids = SearchIndexBackend.search( query, limit, 'Organization' ) organizations = [] ids.each { |id| diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 65bbd85e5..5da4c773f 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -33,11 +33,17 @@ class Ticket < ApplicationModel :article_count => true, } - search_index_support :ignore_attributes => { - :create_article_type_id => true, - :create_article_sender_id => true, - :article_count => true, - } + search_index_support( + :ignore_attributes => { + :create_article_type_id => true, + :create_article_sender_id => true, + :article_count => true, + }, + :keep_attributes => { + :customer_id => true, + :organization_id => true, + }, + ) belongs_to :group has_many :articles, :class_name => 'Ticket::Article', :after_add => :cache_update, :after_remove => :cache_update diff --git a/app/models/ticket/search.rb b/app/models/ticket/search.rb index f9318f613..c5268cabd 100644 --- a/app/models/ticket/search.rb +++ b/app/models/ticket/search.rb @@ -25,6 +25,50 @@ returns limit = params[:limit] || 12 current_user = params[:current_user] + # try search index backend + if SearchIndexBackend.enabled? + query_extention = {} + query_extention['bool'] = {} + query_extention['bool']['must'] = [] + + if current_user.is_role('Agent') + groups = Group.joins(:users). + where( 'groups_users.user_id = ?', current_user.id ). + where( 'groups.active = ?', true ) + group_condition = [] + groups.each {|group| + group_condition.push group.name + } + condition = { + 'query_string' => { 'default_field' => 'Ticket.group.name', 'query' => "\"#{group_condition.join('" OR "')}\"" } + } + query_extention['bool']['must'].push condition + else + if !current_user.organization || ( !current_user.organization.shared || current_user.organization.shared == false ) + condition = { + 'query_string' => { 'default_field' => 'Ticket.customer_id', 'query' => current_user.id } + } + # customer_id: XXX +# conditions = [ 'customer_id = ?', current_user.id ] + else + condition = { + 'query_string' => { 'query' => "Ticket.customer_id:#{current_user.id} OR Ticket.organization_id:#{current_user.organization.id}" } + } + # customer_id: XXX OR organization_id: XXX +# conditions = [ '( customer_id = ? OR organization_id = ? )', current_user.id, current_user.organization.id ] + end + query_extention['bool']['must'].push condition + end + + ids = SearchIndexBackend.search( query, limit, 'Ticket', query_extention ) + tickets = [] + ids.each { |id| + tickets.push Ticket.lookup( :id => id ) + } + return tickets + end + + # fallback do sql query conditions = [] if current_user.is_role('Agent') group_ids = Group.select( 'groups.id' ).joins(:users). @@ -40,17 +84,6 @@ returns end end - # try search index backend - if Setting.get('es_url') - ids = SearchIndexBackend.search( query, limit, 'Ticket' ) - tickets = [] - ids.each { |id| - tickets.push Ticket.lookup( :id => id ) - } - return tickets - end - - # fallback do sql query # do query tickets_all = Ticket.select('DISTINCT(tickets.id)'). where(conditions). diff --git a/app/models/user/search.rb b/app/models/user/search.rb index 8948d3cd6..a706d4c1e 100644 --- a/app/models/user/search.rb +++ b/app/models/user/search.rb @@ -45,7 +45,7 @@ returns return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin') # try search index backend - if Setting.get('es_url') + if SearchIndexBackend.enabled? ids = SearchIndexBackend.search( query, limit, 'User' ) users = [] ids.each { |id| diff --git a/app/models/user/search_index.rb b/app/models/user/search_index.rb index 3254a3a5e..b37356836 100644 --- a/app/models/user/search_index.rb +++ b/app/models/user/search_index.rb @@ -17,8 +17,8 @@ returns def search_index_data attributes = { 'fullname' => "#{ self['firstname'] } #{ self['lastname'] }" } - ['login', 'firstname', 'lastname', 'phone', 'email', 'city', 'country', 'note'].each { |key| - if self[key] && !self[key].empty? + ['login', 'firstname', 'lastname', 'phone', 'email', 'city', 'country', 'note', 'created_at'].each { |key| + if self[key] && (!self.respond_to?('empty?') || !self[key].empty?) attributes[key] = self[key] end } diff --git a/lib/search_index_backend.rb b/lib/search_index_backend.rb index 01b20ff92..c716613b9 100644 --- a/lib/search_index_backend.rb +++ b/lib/search_index_backend.rb @@ -63,7 +63,7 @@ return search result =end - def self.search( query, limit = 10, index = nil ) + def self.search( query, limit = 10, index = nil, query_extention = {} ) return [] if !query url = build_url() @@ -76,16 +76,40 @@ return search result data = {} data['from'] = 0 data['size'] = 10 - data['query'] = {} + data['sort'] = + [ + { + 'updated_at' => { + 'order' => 'desc' + } + }, + "_score" + ] + data['query'] = query_extention || {} + if !data['query']['bool'] + data['query']['bool'] = {} + end # data['query']['text_phrase'] = {} # data['query']['text_phrase']['to'] = query # data['query']['bool'] = {} # data['query']['bool']['must'] = {} # data['query']['bool']['must']['term'] = {} # data['query']['bool']['must']['term']['title'] = '*z*' - data['query']['query_string'] = {} - data['query']['query_string']['query'] = query + if !data['query']['bool']['must'] + data['query']['bool']['must'] = [] + end +# if !data['query']['query_string'] +# data['query']['query_string'] = {} +# end + condition = { + 'query_string' => { + 'query' => query + } + } + data['query']['bool']['must'].push condition +# data['query']['query_string']['query'] = query +#query_string":{"query":"10005 # data['filtered'] = {} # data['filtered']['query'] = {} # data['filtered']['query']['match'] = {} @@ -115,12 +139,27 @@ return search result return ids end +=begin + +return true if backend is configured + + result = SearchIndexBackend.enabled? + +=end + + def self.enabled? + return if !Setting.get('es_url') + return if Setting.get('es_url').empty? + true + end + + private def self.build_url( type = nil, o_id = nil ) + return if !SearchIndexBackend.enabled? index = Setting.get('es_index').to_s + "_#{Rails.env}" url = Setting.get('es_url') - return if !url if type if o_id url = "#{url}/#{index}/#{type}/#{o_id}"