object_manager.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class ObjectManager
  3. =begin
  4. list all backend managed object
  5. ObjectManager.list_objects()
  6. =end
  7. def self.list_objects
  8. %w(Ticket TicketArticle User Organization Group)
  9. end
  10. =begin
  11. list all frontend managed object
  12. ObjectManager.list_frontend_objects()
  13. =end
  14. def self.list_frontend_objects
  15. %w(Ticket User Organization) #, 'Group' ]
  16. end
  17. end
  18. class ObjectManager::Attribute < ApplicationModel
  19. self.table_name = 'object_manager_attributes'
  20. belongs_to :object_lookup, class_name: 'ObjectLookup'
  21. validates :name, presence: true
  22. store :screens
  23. store :data_option
  24. =begin
  25. list of all attributes
  26. result = ObjectManager::Attribute.list_full
  27. result = [
  28. {
  29. name: 'some name',
  30. display: '...',
  31. }.
  32. ],
  33. =end
  34. def self.list_full
  35. result = ObjectManager::Attribute.all
  36. attributes = []
  37. assets = {}
  38. result.each {|item|
  39. attribute = item.attributes
  40. attribute[:object] = ObjectLookup.by_id( item.object_lookup_id )
  41. attribute.delete('object_lookup_id')
  42. attributes.push attribute
  43. }
  44. attributes
  45. end
  46. =begin
  47. add a new attribute entry for an object
  48. ObjectManager::Attribute.add(
  49. :object => 'Ticket',
  50. :name => 'group_id',
  51. :frontend => 'Group',
  52. :data_type => 'select',
  53. :data_option => {
  54. :relation => 'Group',
  55. :relation_condition => { :access => 'rw' },
  56. :multiple => false,
  57. :null => true,
  58. :translate => false,
  59. },
  60. :editable => false,
  61. :active => true,
  62. :screens => {
  63. :create => {
  64. '-all-' => {
  65. :required => true,
  66. },
  67. },
  68. :edit => {
  69. :Agent => {
  70. :required => true,
  71. },
  72. },
  73. },
  74. :pending_migration => false,
  75. :position => 20,
  76. :created_by_id => 1,
  77. :updated_by_id => 1,
  78. :created_at => '2014-06-04 10:00:00',
  79. :updated_at => '2014-06-04 10:00:00',
  80. )
  81. =end
  82. def self.add(data)
  83. # lookups
  84. if data[:object]
  85. data[:object_lookup_id] = ObjectLookup.by_name( data[:object] )
  86. end
  87. data.delete(:object)
  88. # check newest entry - is needed
  89. result = ObjectManager::Attribute.find_by(
  90. object_lookup_id: data[:object_lookup_id],
  91. name: data[:name],
  92. )
  93. if result
  94. return result.update_attributes(data)
  95. end
  96. # create history
  97. ObjectManager::Attribute.create(data)
  98. end
  99. =begin
  100. get the attribute model based on object and name
  101. attribute = ObjectManager::Attribute.get(
  102. object: 'Ticket',
  103. name: 'group_id',
  104. )
  105. =end
  106. def self.get(data)
  107. # lookups
  108. if data[:object]
  109. data[:object_lookup_id] = ObjectLookup.by_name( data[:object] )
  110. end
  111. ObjectManager::Attribute.find_by(
  112. object_lookup_id: data[:object_lookup_id],
  113. name: data[:name],
  114. )
  115. end
  116. =begin
  117. get user based list of object attributes
  118. attribute_list = ObjectManager::Attribute.by_object('Ticket', user)
  119. returns:
  120. [
  121. { name: 'api_key', display: 'API KEY', tag: 'input', null: true, edit: true, maxlength: 32 },
  122. { name: 'api_ip_regexp', display: 'API IP RegExp', tag: 'input', null: true, edit: true },
  123. { name: 'api_ip_max', display: 'API IP Max', tag: 'input', null: true, edit: true },
  124. ]
  125. =end
  126. def self.by_object(object, user)
  127. # lookups
  128. if object
  129. object_lookup_id = ObjectLookup.by_name(object)
  130. end
  131. # get attributes in right order
  132. result = ObjectManager::Attribute.where(
  133. object_lookup_id: object_lookup_id,
  134. active: true,
  135. ).order('position ASC')
  136. attributes = []
  137. result.each {|item|
  138. data = {
  139. name: item.name,
  140. display: item.display,
  141. tag: item.data_type,
  142. #:null => item.null,
  143. }
  144. if item.screens
  145. data[:screen] = {}
  146. item.screens.each {|screen, roles_options|
  147. data[:screen][screen] = {}
  148. roles_options.each {|role, options|
  149. if role == '-all-'
  150. data[:screen][screen] = options
  151. elsif user && user.role?(role)
  152. data[:screen][screen] = options
  153. end
  154. }
  155. }
  156. end
  157. if item.data_option
  158. data = data.merge(item.data_option.symbolize_keys)
  159. end
  160. attributes.push data
  161. }
  162. attributes
  163. end
  164. =begin
  165. get user based list of object attributes as hash
  166. attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
  167. returns:
  168. {
  169. 'api_key' => { name: 'api_key', display: 'API KEY', tag: 'input', null: true, edit: true, maxlength: 32 },
  170. 'api_ip_regexp' => { name: 'api_ip_regexp', display: 'API IP RegExp', tag: 'input', null: true, edit: true },
  171. 'api_ip_max' => { name: 'api_ip_max', display: 'API IP Max', tag: 'input', null: true, edit: true },
  172. }
  173. =end
  174. def self.by_object_as_hash(object, user)
  175. list = by_object(object, user)
  176. hash = {}
  177. list.each {|item|
  178. hash[ item[:name] ] = item
  179. }
  180. hash
  181. end
  182. end