Added support of group permissions.

This commit is contained in:
Martin Edenhofer 2014-02-02 19:58:31 +01:00
parent 2a4467f5b2
commit bfb507dee5
7 changed files with 117 additions and 29 deletions

View file

@ -20,8 +20,6 @@ returns
# default ignored attributes # default ignored attributes
ignore_attributes = { ignore_attributes = {
:created_at => true,
:updated_at => true,
:created_by_id => true, :created_by_id => true,
:updated_by_id => true, :updated_by_id => true,
:active => true, :active => true,
@ -94,12 +92,22 @@ returns
=end =end
def search_index_attribute_lookup(attributes, ref_object) 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_new = {}
attributes.each {|key, value| attributes.each {|key, value|
next if !value next if !value
# get attribute name # 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' next if attribute_name[-3,3] != '_id'
attribute_name = attribute_name[ 0, attribute_name.length-3 ] attribute_name = attribute_name[ 0, attribute_name.length-3 ]
@ -123,7 +131,9 @@ returns
# save name of ref object # save name of ref object
attributes_new[ attribute_name ] = value 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) attributes_new.merge(attributes)
end end

View file

@ -29,7 +29,7 @@ returns
return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin') return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin')
# try search index backend # try search index backend
if Setting.get('es_url') if SearchIndexBackend.enabled?
ids = SearchIndexBackend.search( query, limit, 'Organization' ) ids = SearchIndexBackend.search( query, limit, 'Organization' )
organizations = [] organizations = []
ids.each { |id| ids.each { |id|

View file

@ -33,11 +33,17 @@ class Ticket < ApplicationModel
:article_count => true, :article_count => true,
} }
search_index_support :ignore_attributes => { search_index_support(
:create_article_type_id => true, :ignore_attributes => {
:create_article_sender_id => true, :create_article_type_id => true,
:article_count => true, :create_article_sender_id => true,
} :article_count => true,
},
:keep_attributes => {
:customer_id => true,
:organization_id => true,
},
)
belongs_to :group belongs_to :group
has_many :articles, :class_name => 'Ticket::Article', :after_add => :cache_update, :after_remove => :cache_update has_many :articles, :class_name => 'Ticket::Article', :after_add => :cache_update, :after_remove => :cache_update

View file

@ -25,6 +25,50 @@ returns
limit = params[:limit] || 12 limit = params[:limit] || 12
current_user = params[:current_user] 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 = [] conditions = []
if current_user.is_role('Agent') if current_user.is_role('Agent')
group_ids = Group.select( 'groups.id' ).joins(:users). group_ids = Group.select( 'groups.id' ).joins(:users).
@ -40,17 +84,6 @@ returns
end end
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 # do query
tickets_all = Ticket.select('DISTINCT(tickets.id)'). tickets_all = Ticket.select('DISTINCT(tickets.id)').
where(conditions). where(conditions).

View file

@ -45,7 +45,7 @@ returns
return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin') return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin')
# try search index backend # try search index backend
if Setting.get('es_url') if SearchIndexBackend.enabled?
ids = SearchIndexBackend.search( query, limit, 'User' ) ids = SearchIndexBackend.search( query, limit, 'User' )
users = [] users = []
ids.each { |id| ids.each { |id|

View file

@ -17,8 +17,8 @@ returns
def search_index_data def search_index_data
attributes = { 'fullname' => "#{ self['firstname'] } #{ self['lastname'] }" } attributes = { 'fullname' => "#{ self['firstname'] } #{ self['lastname'] }" }
['login', 'firstname', 'lastname', 'phone', 'email', 'city', 'country', 'note'].each { |key| ['login', 'firstname', 'lastname', 'phone', 'email', 'city', 'country', 'note', 'created_at'].each { |key|
if self[key] && !self[key].empty? if self[key] && (!self.respond_to?('empty?') || !self[key].empty?)
attributes[key] = self[key] attributes[key] = self[key]
end end
} }

View file

@ -63,7 +63,7 @@ return search result
=end =end
def self.search( query, limit = 10, index = nil ) def self.search( query, limit = 10, index = nil, query_extention = {} )
return [] if !query return [] if !query
url = build_url() url = build_url()
@ -76,16 +76,40 @@ return search result
data = {} data = {}
data['from'] = 0 data['from'] = 0
data['size'] = 10 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'] = {}
# data['query']['text_phrase']['to'] = query # data['query']['text_phrase']['to'] = query
# data['query']['bool'] = {} # data['query']['bool'] = {}
# data['query']['bool']['must'] = {} # data['query']['bool']['must'] = {}
# data['query']['bool']['must']['term'] = {} # data['query']['bool']['must']['term'] = {}
# data['query']['bool']['must']['term']['title'] = '*z*' # data['query']['bool']['must']['term']['title'] = '*z*'
data['query']['query_string'] = {} if !data['query']['bool']['must']
data['query']['query_string']['query'] = query 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'] = {}
# data['filtered']['query'] = {} # data['filtered']['query'] = {}
# data['filtered']['query']['match'] = {} # data['filtered']['query']['match'] = {}
@ -115,12 +139,27 @@ return search result
return ids return ids
end 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 private
def self.build_url( type = nil, o_id = nil ) def self.build_url( type = nil, o_id = nil )
return if !SearchIndexBackend.enabled?
index = Setting.get('es_index').to_s + "_#{Rails.env}" index = Setting.get('es_index').to_s + "_#{Rails.env}"
url = Setting.get('es_url') url = Setting.get('es_url')
return if !url
if type if type
if o_id if o_id
url = "#{url}/#{index}/#{type}/#{o_id}" url = "#{url}/#{index}/#{type}/#{o_id}"