trabajo-afectivo/app/controllers/settings_controller.rb

106 lines
2.5 KiB
Ruby
Raw Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2012-04-10 14:06:46 +00:00
class SettingsController < ApplicationController
prepend_before_action { authentication_check && authorize! }
2012-04-10 14:06:46 +00:00
# GET /settings
def index
list = []
Setting.all.each do |setting|
next if !authorized?(setting, :show?)
list.push setting
end
render json: list, status: :ok
2012-04-10 14:06:46 +00:00
end
# GET /settings/1
def show
model_show_render(Setting, params)
2012-04-10 14:06:46 +00:00
end
# POST /settings
def create
raise Exceptions::Forbidden, __('Not authorized (feature not possible)')
2012-04-10 14:06:46 +00:00
end
# PUT /settings/1
def update
clean_params = keep_certain_attributes
model_update_render(Setting, clean_params)
2012-04-10 14:06:46 +00:00
end
2015-07-12 01:32:40 +00:00
# PUT /settings/image/:id
def update_image
clean_params = keep_certain_attributes
2015-07-12 01:32:40 +00:00
if !clean_params[:logo]
2015-07-12 01:32:40 +00:00
render json: {
result: 'invalid',
message: __('Need logo param'),
2015-07-12 01:32:40 +00:00
}
return
end
# validate image
if !clean_params[:logo].match?(%r{^data:image}i)
2015-07-12 01:32:40 +00:00
render json: {
result: 'invalid',
message: __('Invalid payload, need data:image in logo param'),
2015-07-12 01:32:40 +00:00
}
return
end
# process image
file = StaticAssets.data_url_attributes(clean_params[:logo])
2015-07-12 01:32:40 +00:00
if !file[:content] || !file[:mime_type]
render json: {
result: 'invalid',
message: __('Unable to process image upload.'),
2015-07-12 01:32:40 +00:00
}
return
end
# store image 1:1
StaticAssets.store_raw(file[:content], file[:mime_type])
# store resized image 1:1
setting = Setting.lookup(name: 'product_logo')
if params[:logo_resize] && params[:logo_resize] =~ %r{^data:image}i
2015-07-12 01:32:40 +00:00
# data:image/png;base64
file = StaticAssets.data_url_attributes(params[:logo_resize])
2015-07-12 01:32:40 +00:00
# store image 1:1
setting.state = StaticAssets.store(file[:content], file[:mime_type])
setting.save!
2015-07-12 01:32:40 +00:00
end
render json: {
result: 'ok',
2015-07-12 01:32:40 +00:00
settings: [setting],
}
end
2012-04-10 14:06:46 +00:00
# DELETE /settings/1
def destroy
raise Exceptions::Forbidden, __('Not authorized (feature not possible)')
2012-04-10 14:06:46 +00:00
end
private
def keep_certain_attributes
setting = Setting.find(params[:id])
2017-11-23 08:09:44 +00:00
%i[name area state_initial frontend options].each do |key|
params.delete(key)
end
2017-11-23 08:09:44 +00:00
if params[:preferences].present?
%i[online_service_disable permission render].each do |key|
params[:preferences].delete(key)
end
params[:preferences].merge!(setting.preferences)
end
params
end
2012-04-10 14:06:46 +00:00
end