settings_controller.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class SettingsController < ApplicationController
  3. prepend_before_action { authentication_check && authorize! }
  4. # GET /settings
  5. def index
  6. list = []
  7. Setting.all.each do |setting|
  8. next if !authorized?(setting, :show?)
  9. list.push setting
  10. end
  11. render json: list, status: :ok
  12. end
  13. # GET /settings/1
  14. def show
  15. model_show_render(Setting, params)
  16. end
  17. # POST /settings
  18. def create
  19. raise Exceptions::Forbidden, __('Not authorized (feature not possible)')
  20. end
  21. # PUT /settings/1
  22. def update
  23. clean_params = keep_certain_attributes
  24. model_update_render(Setting, clean_params)
  25. end
  26. # PUT /settings/image/:id
  27. def update_image
  28. clean_params = keep_certain_attributes
  29. if !clean_params[:logo]
  30. render json: {
  31. result: 'invalid',
  32. message: __('Need logo param'),
  33. }
  34. return
  35. end
  36. # validate image
  37. if !clean_params[:logo].match?(%r{^data:image}i)
  38. render json: {
  39. result: 'invalid',
  40. message: __('Invalid payload, need data:image in logo param'),
  41. }
  42. return
  43. end
  44. # process image
  45. file = StaticAssets.data_url_attributes(clean_params[:logo])
  46. if !file[:content] || !file[:mime_type]
  47. render json: {
  48. result: 'invalid',
  49. message: __('The uploaded image could not be processed.'),
  50. }
  51. return
  52. end
  53. # store image 1:1
  54. StaticAssets.store_raw(file[:content], file[:mime_type])
  55. # store resized image 1:1
  56. setting = Setting.lookup(name: 'product_logo')
  57. if params[:logo_resize] && params[:logo_resize] =~ %r{^data:image}i
  58. # data:image/png;base64
  59. file = StaticAssets.data_url_attributes(params[:logo_resize])
  60. # store image 1:1
  61. setting.state = StaticAssets.store(file[:content], file[:mime_type])
  62. setting.save!
  63. end
  64. render json: {
  65. result: 'ok',
  66. settings: [setting],
  67. }
  68. end
  69. # DELETE /settings/1
  70. def destroy
  71. raise Exceptions::Forbidden, __('Not authorized (feature not possible)')
  72. end
  73. private
  74. def keep_certain_attributes
  75. setting = Setting.find(params[:id])
  76. %i[name area state_initial frontend options].each do |key|
  77. params.delete(key)
  78. end
  79. if params[:preferences].present?
  80. %i[online_service_disable permission render].each do |key|
  81. params[:preferences].delete(key)
  82. end
  83. params[:preferences].merge!(setting.preferences)
  84. end
  85. params
  86. end
  87. end