2024-06-14 18:07:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UpdateSiteSetting
|
|
|
|
include Service::Base
|
|
|
|
|
|
|
|
policy :current_user_is_admin
|
DEV: Have `contract` take a block in services
Currently in services, the `contract` step is only used to define where
the contract will be called in the execution flow. Then, a `Contract`
class has to be defined with validations in it.
This patch allows the `contract` step to take a block containing
validations, attributes, etc. directly. No need to then open a
`Contract` class later in the service.
It also has a nice side effect, as it’s now easy to define multiples
contracts inside the same service. Before, we had the `class_name:`
option, but it wasn’t really useful as you had to redefine a complete
new contract class.
Now, when using a name for the contract other than `default`, a new
contract will be created automatically using the provided name.
Example:
```ruby
contract(:user) do
attribute :user_id, :integer
validates :user_id, presence: true
end
```
This will create a `UserContract` class and use it, also putting the
resulting contract in `context[:user_contract]`.
2024-10-01 23:17:14 +08:00
|
|
|
contract do
|
2024-06-14 18:07:27 +08:00
|
|
|
attribute :setting_name
|
|
|
|
attribute :new_value
|
|
|
|
attribute :allow_changing_hidden, :boolean, default: false
|
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
before_validation { self.setting_name = setting_name&.to_sym }
|
|
|
|
|
2024-06-14 18:07:27 +08:00
|
|
|
validates :setting_name, presence: true
|
|
|
|
end
|
DEV: Have `contract` take a block in services
Currently in services, the `contract` step is only used to define where
the contract will be called in the execution flow. Then, a `Contract`
class has to be defined with validations in it.
This patch allows the `contract` step to take a block containing
validations, attributes, etc. directly. No need to then open a
`Contract` class later in the service.
It also has a nice side effect, as it’s now easy to define multiples
contracts inside the same service. Before, we had the `class_name:`
option, but it wasn’t really useful as you had to redefine a complete
new contract class.
Now, when using a name for the contract other than `default`, a new
contract will be created automatically using the provided name.
Example:
```ruby
contract(:user) do
attribute :user_id, :integer
validates :user_id, presence: true
end
```
This will create a `UserContract` class and use it, also putting the
resulting contract in `context[:user_contract]`.
2024-10-01 23:17:14 +08:00
|
|
|
policy :setting_is_visible
|
|
|
|
policy :setting_is_configurable
|
|
|
|
step :cleanup_value
|
|
|
|
step :save
|
2024-06-14 18:07:27 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_user_is_admin(guardian:)
|
|
|
|
guardian.is_admin?
|
|
|
|
end
|
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
def setting_is_visible(contract:)
|
|
|
|
contract.allow_changing_hidden || !SiteSetting.hidden_settings.include?(contract.setting_name)
|
2024-06-14 18:07:27 +08:00
|
|
|
end
|
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
def setting_is_configurable(contract:)
|
|
|
|
return true if !SiteSetting.plugins[contract.setting_name]
|
2024-06-14 18:07:27 +08:00
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
Discourse.plugins_by_name[SiteSetting.plugins[contract.setting_name]].configurable?
|
2024-06-14 18:07:27 +08:00
|
|
|
end
|
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
def cleanup_value(contract:)
|
|
|
|
new_value = contract.new_value
|
2024-06-14 18:07:27 +08:00
|
|
|
new_value = new_value.strip if new_value.is_a?(String)
|
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
case SiteSetting.type_supervisor.get_type(contract.setting_name)
|
2024-06-14 18:07:27 +08:00
|
|
|
when :integer
|
|
|
|
new_value = new_value.tr("^-0-9", "").to_i if new_value.is_a?(String)
|
|
|
|
when :file_size_restriction
|
|
|
|
new_value = new_value.tr("^0-9", "").to_i if new_value.is_a?(String)
|
|
|
|
when :uploaded_image_list
|
|
|
|
new_value = new_value.blank? ? "" : Upload.get_from_urls(new_value.split("|")).to_a
|
|
|
|
when :upload
|
|
|
|
new_value = Upload.get_from_url(new_value) || ""
|
|
|
|
end
|
2024-10-04 00:05:45 +08:00
|
|
|
context[:new_value] = new_value
|
2024-06-14 18:07:27 +08:00
|
|
|
end
|
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
def save(contract:, new_value:, guardian:)
|
|
|
|
SiteSetting.set_and_log(contract.setting_name, new_value, guardian.user)
|
2024-06-14 18:07:27 +08:00
|
|
|
end
|
|
|
|
end
|