2014-09-09 23:42:20 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class ObjectManager
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-11-06 23:37:05 +00:00
|
|
|
list all backend managed object
|
2014-09-09 23:42:20 +00:00
|
|
|
|
2015-05-05 10:51:19 +00:00
|
|
|
ObjectManager.list_objects()
|
2014-09-09 23:42:20 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-05-05 10:51:19 +00:00
|
|
|
def self.list_objects
|
2015-05-07 07:23:16 +00:00
|
|
|
%w(Ticket TicketArticle User Organization Group)
|
2014-11-06 23:37:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
list all frontend managed object
|
|
|
|
|
2015-05-05 10:51:19 +00:00
|
|
|
ObjectManager.list_frontend_objects()
|
2014-11-06 23:37:05 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-05-05 10:51:19 +00:00
|
|
|
def self.list_frontend_objects
|
2015-05-07 07:23:16 +00:00
|
|
|
%w(Ticket User Organization) #, 'Group' ]
|
2014-09-09 23:42:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class ObjectManager::Attribute < ApplicationModel
|
|
|
|
self.table_name = 'object_manager_attributes'
|
2015-04-27 13:42:53 +00:00
|
|
|
belongs_to :object_lookup, class_name: 'ObjectLookup'
|
|
|
|
validates :name, presence: true
|
2014-09-09 23:42:20 +00:00
|
|
|
store :screens
|
|
|
|
store :data_option
|
|
|
|
|
2014-11-02 19:44:15 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
list of all attributes
|
|
|
|
|
|
|
|
result = ObjectManager::Attribute.list_full
|
|
|
|
|
|
|
|
result = [
|
|
|
|
{
|
2016-01-20 22:03:10 +00:00
|
|
|
name: 'some name',
|
|
|
|
display: '...',
|
2014-11-02 19:44:15 +00:00
|
|
|
}.
|
|
|
|
],
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.list_full
|
|
|
|
result = ObjectManager::Attribute.all
|
|
|
|
attributes = []
|
|
|
|
assets = {}
|
|
|
|
result.each {|item|
|
|
|
|
attribute = item.attributes
|
|
|
|
attribute[:object] = ObjectLookup.by_id( item.object_lookup_id )
|
|
|
|
attribute.delete('object_lookup_id')
|
|
|
|
attributes.push attribute
|
|
|
|
}
|
|
|
|
attributes
|
|
|
|
end
|
2014-09-09 23:42:20 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-02-08 08:16:01 +00:00
|
|
|
add a new attribute entry for an object
|
2014-09-09 23:42:20 +00:00
|
|
|
|
|
|
|
ObjectManager::Attribute.add(
|
2015-01-04 12:49:04 +00:00
|
|
|
:object => 'Ticket',
|
|
|
|
:name => 'group_id',
|
|
|
|
:frontend => 'Group',
|
|
|
|
:data_type => 'select',
|
2014-09-09 23:42:20 +00:00
|
|
|
:data_option => {
|
2015-01-04 12:49:04 +00:00
|
|
|
:relation => 'Group',
|
2014-09-09 23:42:20 +00:00
|
|
|
:relation_condition => { :access => 'rw' },
|
2015-01-04 12:49:04 +00:00
|
|
|
:multiple => false,
|
|
|
|
:null => true,
|
|
|
|
:translate => false,
|
2014-09-09 23:42:20 +00:00
|
|
|
},
|
2015-01-04 12:49:04 +00:00
|
|
|
:editable => false,
|
|
|
|
:active => true,
|
|
|
|
:screens => {
|
2014-09-09 23:42:20 +00:00
|
|
|
:create => {
|
|
|
|
'-all-' => {
|
|
|
|
:required => true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
:edit => {
|
|
|
|
:Agent => {
|
|
|
|
:required => true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-04 12:49:04 +00:00
|
|
|
:pending_migration => false,
|
|
|
|
:position => 20,
|
|
|
|
:created_by_id => 1,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
:created_at => '2014-06-04 10:00:00',
|
|
|
|
:updated_at => '2014-06-04 10:00:00',
|
2014-09-09 23:42:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.add(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
|
|
|
data[:object_lookup_id] = ObjectLookup.by_name( data[:object] )
|
|
|
|
end
|
|
|
|
data.delete(:object)
|
|
|
|
|
|
|
|
# check newest entry - is needed
|
2015-05-07 10:15:40 +00:00
|
|
|
result = ObjectManager::Attribute.find_by(
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: data[:object_lookup_id],
|
|
|
|
name: data[:name],
|
2015-05-07 10:15:40 +00:00
|
|
|
)
|
2014-09-09 23:42:20 +00:00
|
|
|
if result
|
|
|
|
return result.update_attributes(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
|
|
|
ObjectManager::Attribute.create(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-05-06 10:37:23 +00:00
|
|
|
remove attribute entry for an object
|
|
|
|
|
|
|
|
ObjectManager::Attribute.remove(
|
|
|
|
object: 'Ticket',
|
|
|
|
name: 'group_id',
|
|
|
|
)
|
|
|
|
|
|
|
|
use "force: true" to delete also not editable fields
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
|
|
|
data[:object_lookup_id] = ObjectLookup.by_name(data[:object])
|
|
|
|
end
|
|
|
|
|
|
|
|
# check newest entry - is needed
|
|
|
|
result = ObjectManager::Attribute.find_by(
|
|
|
|
object_lookup_id: data[:object_lookup_id],
|
|
|
|
name: data[:name],
|
|
|
|
)
|
|
|
|
if !result
|
|
|
|
raise "ERROR: No such field #{data[:object]}.#{data[:name]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if !data[:force] && !result.editable
|
|
|
|
raise "ERROR: #{data[:object]}.#{data[:name]} can't be removed!"
|
|
|
|
end
|
|
|
|
result.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-02-08 08:16:01 +00:00
|
|
|
get the attribute model based on object and name
|
|
|
|
|
|
|
|
attribute = ObjectManager::Attribute.get(
|
2016-01-20 22:03:10 +00:00
|
|
|
object: 'Ticket',
|
|
|
|
name: 'group_id',
|
2015-02-08 08:16:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.get(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
|
|
|
data[:object_lookup_id] = ObjectLookup.by_name( data[:object] )
|
|
|
|
end
|
|
|
|
|
2015-05-07 10:15:40 +00:00
|
|
|
ObjectManager::Attribute.find_by(
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: data[:object_lookup_id],
|
|
|
|
name: data[:name],
|
2015-05-07 10:15:40 +00:00
|
|
|
)
|
2015-02-08 08:16:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-11-02 19:44:15 +00:00
|
|
|
get user based list of object attributes
|
2014-09-09 23:42:20 +00:00
|
|
|
|
|
|
|
attribute_list = ObjectManager::Attribute.by_object('Ticket', user)
|
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
[
|
2016-01-20 22:03:10 +00:00
|
|
|
{ name: 'api_key', display: 'API KEY', tag: 'input', null: true, edit: true, maxlength: 32 },
|
|
|
|
{ name: 'api_ip_regexp', display: 'API IP RegExp', tag: 'input', null: true, edit: true },
|
|
|
|
{ name: 'api_ip_max', display: 'API IP Max', tag: 'input', null: true, edit: true },
|
2014-09-09 23:42:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.by_object(object, user)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if object
|
2016-01-24 11:50:29 +00:00
|
|
|
object_lookup_id = ObjectLookup.by_name(object)
|
2014-09-09 23:42:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get attributes in right order
|
|
|
|
result = ObjectManager::Attribute.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: object_lookup_id,
|
|
|
|
active: true,
|
2014-09-09 23:42:20 +00:00
|
|
|
).order('position ASC')
|
|
|
|
attributes = []
|
|
|
|
result.each {|item|
|
|
|
|
data = {
|
2015-04-27 13:42:53 +00:00
|
|
|
name: item.name,
|
|
|
|
display: item.display,
|
|
|
|
tag: item.data_type,
|
2014-09-09 23:42:20 +00:00
|
|
|
#:null => item.null,
|
|
|
|
}
|
|
|
|
if item.screens
|
|
|
|
data[:screen] = {}
|
2015-04-30 18:13:25 +00:00
|
|
|
item.screens.each {|screen, roles_options|
|
2014-09-09 23:42:20 +00:00
|
|
|
data[:screen][screen] = {}
|
|
|
|
roles_options.each {|role, options|
|
|
|
|
if role == '-all-'
|
|
|
|
data[:screen][screen] = options
|
2015-05-08 08:15:45 +00:00
|
|
|
elsif user && user.role?(role)
|
2014-09-09 23:42:20 +00:00
|
|
|
data[:screen][screen] = options
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if item.data_option
|
2016-01-24 11:50:29 +00:00
|
|
|
data = data.merge(item.data_option.symbolize_keys)
|
2014-09-09 23:42:20 +00:00
|
|
|
end
|
|
|
|
attributes.push data
|
|
|
|
}
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
2015-01-04 12:49:04 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get user based list of object attributes as hash
|
|
|
|
|
|
|
|
attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
|
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
{
|
2016-01-20 22:03:10 +00:00
|
|
|
'api_key' => { name: 'api_key', display: 'API KEY', tag: 'input', null: true, edit: true, maxlength: 32 },
|
|
|
|
'api_ip_regexp' => { name: 'api_ip_regexp', display: 'API IP RegExp', tag: 'input', null: true, edit: true },
|
|
|
|
'api_ip_max' => { name: 'api_ip_max', display: 'API IP Max', tag: 'input', null: true, edit: true },
|
2015-01-04 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.by_object_as_hash(object, user)
|
2015-05-07 12:10:38 +00:00
|
|
|
list = by_object(object, user)
|
2015-01-04 12:49:04 +00:00
|
|
|
hash = {}
|
|
|
|
list.each {|item|
|
|
|
|
hash[ item[:name] ] = item
|
|
|
|
}
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|