object_manager_attributes_controller.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ObjectManagerAttributesController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. # GET /object_manager_attributes_list
  5. def list
  6. render json: {
  7. objects: ObjectManager.list_frontend_objects,
  8. }
  9. end
  10. # GET /object_manager_attributes
  11. def index
  12. render json: ObjectManager::Attribute.list_full
  13. end
  14. # GET /object_manager_attributes/1
  15. def show
  16. model_show_render(ObjectManager::Attribute, params)
  17. end
  18. # POST /object_manager_attributes
  19. def create
  20. # check if attribute already exists
  21. exists = ObjectManager::Attribute.get(
  22. object: permitted_params[:object],
  23. name: permitted_params[:name],
  24. )
  25. raise Exceptions::UnprocessableEntity, 'already exists' if exists
  26. add_attribute_using_params(permitted_params, status: :created)
  27. end
  28. # PUT /object_manager_attributes/1
  29. def update
  30. # check if attribute already exists
  31. exists = ObjectManager::Attribute.get(
  32. object: permitted_params[:object],
  33. name: permitted_params[:name],
  34. )
  35. raise Exceptions::UnprocessableEntity, 'does not exist' if !exists
  36. add_attribute_using_params(permitted_params, status: :ok)
  37. end
  38. # DELETE /object_manager_attributes/1
  39. def destroy
  40. object_manager_attribute = ObjectManager::Attribute.find(params[:id])
  41. ObjectManager::Attribute.remove(
  42. object_lookup_id: object_manager_attribute.object_lookup_id,
  43. name: object_manager_attribute.name,
  44. )
  45. model_destroy_render_item
  46. rescue => e
  47. logger.error e
  48. raise Exceptions::UnprocessableEntity, e
  49. end
  50. # POST /object_manager_attributes_discard_changes
  51. def discard_changes
  52. ObjectManager::Attribute.discard_changes
  53. render json: {}, status: :ok
  54. end
  55. # POST /object_manager_attributes_execute_migrations
  56. def execute_migrations
  57. ObjectManager::Attribute.migration_execute
  58. render json: {}, status: :ok
  59. end
  60. private
  61. def permitted_params
  62. @permitted_params ||= begin
  63. permitted = params.permit!.to_h
  64. if permitted[:data_type].match?(%r{^(boolean)$}) && permitted[:data_option][:options]
  65. # rubocop:disable Lint/BooleanSymbol
  66. if permitted[:data_option][:options][:false]
  67. permitted[:data_option][:options][false] = permitted[:data_option][:options].delete(:false)
  68. end
  69. if permitted[:data_option][:options][:true]
  70. permitted[:data_option][:options][true] = permitted[:data_option][:options].delete(:true)
  71. end
  72. case permitted[:data_option][:default]
  73. when 'true'
  74. permitted[:data_option][:default] = true
  75. when 'false'
  76. permitted[:data_option][:default] = false
  77. end
  78. # rubocop:enable Lint/BooleanSymbol
  79. end
  80. if permitted[:data_option]
  81. if !permitted[:data_option].key?(:default)
  82. permitted[:data_option][:default] = if permitted[:data_type].match?(%r{^(input|select|multiselect|tree_select)$})
  83. ''
  84. end
  85. end
  86. if permitted[:data_option][:null].nil?
  87. permitted[:data_option][:null] = true
  88. end
  89. if !permitted[:data_option][:options].is_a?(Hash) &&
  90. !permitted[:data_option][:options].is_a?(Array)
  91. permitted[:data_option][:options] = {}
  92. end
  93. if !permitted[:data_option][:relation].is_a?(String)
  94. permitted[:data_option][:relation] = ''
  95. end
  96. else
  97. permitted[:data_option] = {
  98. default: '',
  99. options: {},
  100. relation: '',
  101. null: true
  102. }
  103. end
  104. permitted
  105. end
  106. end
  107. def add_attribute_using_params(given_params, status:)
  108. attributes = given_params.slice(:object, :name, :display, :data_type, :data_option, :active, :screens, :position)
  109. attributes[:editable] = true
  110. object_manager_attribute = ObjectManager::Attribute.add(attributes)
  111. render json: object_manager_attribute.attributes_with_association_ids, status: status
  112. rescue => e
  113. logger.error e
  114. raise Exceptions::UnprocessableEntity, e
  115. end
  116. end