object_manager.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class ObjectManager
  3. =begin
  4. list all backend managed object
  5. ObjectManager.listObjects()
  6. =end
  7. def self.listObjects
  8. ['Ticket', 'TicketArticle', 'User', 'Organization', 'Group' ]
  9. end
  10. =begin
  11. list all frontend managed object
  12. ObjectManager.listFrontendObjects()
  13. =end
  14. def self.listFrontendObjects
  15. ['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 activity 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. },
  59. :editable => false,
  60. :active => true,
  61. :screens => {
  62. :create => {
  63. '-all-' => {
  64. :required => true,
  65. },
  66. },
  67. :edit => {
  68. :Agent => {
  69. :required => true,
  70. },
  71. },
  72. },
  73. :pending_migration => false,
  74. :position => 20,
  75. :created_by_id => 1,
  76. :updated_by_id => 1,
  77. :created_at => '2014-06-04 10:00:00',
  78. :updated_at => '2014-06-04 10:00:00',
  79. )
  80. =end
  81. def self.add(data)
  82. # lookups
  83. if data[:object]
  84. data[:object_lookup_id] = ObjectLookup.by_name( data[:object] )
  85. end
  86. data.delete(:object)
  87. # check newest entry - is needed
  88. result = ObjectManager::Attribute.where(
  89. :object_lookup_id => data[:object_lookup_id],
  90. :name => data[:name],
  91. ).first
  92. if result
  93. # raise "ERROR: attribute #{data[:name]} for #{data[:object]} already exists"
  94. return result.update_attributes(data)
  95. end
  96. # create history
  97. ObjectManager::Attribute.create(data)
  98. end
  99. =begin
  100. get user based list of object attributes
  101. attribute_list = ObjectManager::Attribute.by_object('Ticket', user)
  102. returns:
  103. [
  104. { :name => 'api_key', :display => 'API KEY', :tag => 'input', :null => true, :edit => true, :maxlength => 32 },
  105. { :name => 'api_ip_regexp', :display => 'API IP RegExp', :tag => 'input', :null => true, :edit => true },
  106. { :name => 'api_ip_max', :display => 'API IP Max', :tag => 'input', :null => true, :edit => true },
  107. ]
  108. =end
  109. def self.by_object(object, user)
  110. # lookups
  111. if object
  112. object_lookup_id = ObjectLookup.by_name( object )
  113. end
  114. # get attributes in right order
  115. result = ObjectManager::Attribute.where(
  116. :object_lookup_id => object_lookup_id,
  117. :active => true,
  118. ).order('position ASC')
  119. attributes = []
  120. result.each {|item|
  121. data = {
  122. :name => item.name,
  123. :display => item.display,
  124. :tag => item.data_type,
  125. #:null => item.null,
  126. }
  127. if item.screens
  128. data[:screen] = {}
  129. item.screens.each {|screen, roles_options |
  130. data[:screen][screen] = {}
  131. roles_options.each {|role, options|
  132. if role == '-all-'
  133. data[:screen][screen] = options
  134. elsif user && user.is_role(role)
  135. data[:screen][screen] = options
  136. end
  137. }
  138. }
  139. end
  140. if item.data_option
  141. data = data.merge( item.data_option )
  142. end
  143. attributes.push data
  144. }
  145. attributes
  146. end
  147. end