2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class Admin::SiteSettingsController < Admin::AdminController
|
2017-08-07 09:43:09 +08:00
|
|
|
rescue_from Discourse::InvalidParameters do |e|
|
|
|
|
render_json_error e.message, status: 422
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def index
|
2024-03-18 06:50:39 +08:00
|
|
|
params.permit(:categories, :plugin)
|
|
|
|
|
|
|
|
render_json_dump(
|
|
|
|
site_settings:
|
|
|
|
SiteSetting.all_settings(
|
|
|
|
filter_categories: params[:categories],
|
|
|
|
filter_plugin: params[:plugin],
|
|
|
|
),
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
def update
|
2014-01-06 20:03:53 +08:00
|
|
|
params.require(:id)
|
|
|
|
id = params[:id]
|
2024-06-14 18:07:27 +08:00
|
|
|
update_existing_users = params[:update_existing_user].present?
|
2014-01-06 20:03:53 +08:00
|
|
|
value = params[id]
|
2021-02-23 02:10:54 +08:00
|
|
|
|
2022-10-11 09:14:13 +08:00
|
|
|
new_setting_name =
|
|
|
|
SiteSettings::DeprecatedSettings::SETTINGS.find do |old_name, new_name, override, _|
|
|
|
|
if old_name == id
|
|
|
|
if !override
|
|
|
|
raise Discourse::InvalidParameters,
|
|
|
|
"You cannot change this site setting because it is deprecated, use #{new_name} instead."
|
|
|
|
end
|
2022-10-27 06:38:50 +08:00
|
|
|
|
2022-10-11 09:14:13 +08:00
|
|
|
break new_name
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2022-10-11 09:14:13 +08:00
|
|
|
end
|
2022-10-27 06:38:50 +08:00
|
|
|
|
2021-02-23 02:10:54 +08:00
|
|
|
id = new_setting_name if new_setting_name
|
|
|
|
|
2023-04-18 14:32:18 +08:00
|
|
|
previous_value = value_or_default(SiteSetting.get(id)) if update_existing_users
|
2019-10-15 21:11:27 +08:00
|
|
|
|
DEV: Provide user input to services using `params` key
Currently in services, we don’t make a distinction between input
parameters, options and dependencies.
This can lead to user input modifying the service behavior, whereas it
was not the developer intention.
This patch addresses the issue by changing how data is provided to
services:
- `params` is now used to hold all data coming from outside (typically
user input from a controller) and a contract will take its values from
`params`.
- `options` is a new key to provide options to a service. This typically
allows changing a service behavior at runtime. It is, of course,
totally optional.
- `dependencies` is actually anything else provided to the service (like
`guardian`) and available directly from the context object.
The `service_params` helper in controllers has been updated to reflect
those changes, so most of the existing services didn’t need specific
changes.
The options block has the same DSL as contracts, as it’s also based on
`ActiveModel`. There aren’t any validations, though. Here’s an example:
```ruby
options do
attribute :allow_changing_hidden, :boolean, default: false
end
```
And here’s an example of how to call a service with the new keys:
```ruby
MyService.call(params: { key1: value1, … }, options: { my_option: true }, guardian:, …)
```
2024-10-18 23:45:47 +08:00
|
|
|
SiteSetting::Update.call(params: { setting_name: id, new_value: value }, guardian:) do
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
on_success do |params:|
|
2024-09-04 00:30:22 +08:00
|
|
|
if update_existing_users
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
SiteSettingUpdateExistingUsers.call(id, params.new_value, previous_value)
|
2024-09-04 00:30:22 +08:00
|
|
|
end
|
2024-06-14 18:07:27 +08:00
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
on_failed_policy(:setting_is_visible) do
|
|
|
|
raise Discourse::InvalidParameters, I18n.t("errors.site_settings.site_setting_is_hidden")
|
|
|
|
end
|
|
|
|
on_failed_policy(:setting_is_configurable) do
|
|
|
|
raise Discourse::InvalidParameters,
|
|
|
|
I18n.t("errors.site_settings.site_setting_is_unconfigurable")
|
2019-10-15 21:11:27 +08:00
|
|
|
end
|
|
|
|
end
|
2017-08-07 09:43:09 +08:00
|
|
|
end
|
|
|
|
|
2019-11-18 02:39:38 +08:00
|
|
|
def user_count
|
|
|
|
params.require(:site_setting_id)
|
|
|
|
id = params[:site_setting_id]
|
|
|
|
raise Discourse::NotFound unless id.start_with?("default_")
|
2022-05-23 21:20:51 +08:00
|
|
|
new_value = value_or_default(params[id])
|
2019-11-18 02:39:38 +08:00
|
|
|
|
|
|
|
raise_access_hidden_setting(id)
|
2022-05-23 21:20:51 +08:00
|
|
|
previous_value = value_or_default(SiteSetting.public_send(id))
|
2019-11-18 02:39:38 +08:00
|
|
|
json = {}
|
|
|
|
|
2024-06-14 18:07:27 +08:00
|
|
|
if (user_option = SiteSettingUpdateExistingUsers.user_options[id.to_sym]).present?
|
2019-11-18 02:39:38 +08:00
|
|
|
if user_option == "text_size_key"
|
|
|
|
previous_value = UserOption.text_sizes[previous_value.to_sym]
|
|
|
|
elsif user_option == "title_count_mode_key"
|
|
|
|
previous_value = UserOption.title_count_modes[previous_value.to_sym]
|
|
|
|
end
|
|
|
|
|
2022-05-23 21:20:51 +08:00
|
|
|
json[:user_count] = UserOption.human_users.where(user_option => previous_value).count
|
2019-11-18 02:39:38 +08:00
|
|
|
elsif id.start_with?("default_categories_")
|
|
|
|
previous_category_ids = previous_value.split("|")
|
|
|
|
new_category_ids = new_value.split("|")
|
|
|
|
|
2024-06-14 18:07:27 +08:00
|
|
|
notification_level = SiteSettingUpdateExistingUsers.category_notification_level(id)
|
2019-11-18 02:39:38 +08:00
|
|
|
|
|
|
|
user_ids =
|
|
|
|
CategoryUser
|
|
|
|
.where(
|
|
|
|
category_id: previous_category_ids - new_category_ids,
|
|
|
|
notification_level: notification_level,
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2019-11-18 02:39:38 +08:00
|
|
|
.distinct
|
|
|
|
.pluck(:user_id)
|
|
|
|
user_ids +=
|
|
|
|
User
|
2020-06-19 02:09:54 +08:00
|
|
|
.real
|
2019-11-18 02:39:38 +08:00
|
|
|
.joins("CROSS JOIN categories c")
|
|
|
|
.joins("LEFT JOIN category_users cu ON users.id = cu.user_id AND c.id = cu.category_id")
|
2020-06-19 02:09:54 +08:00
|
|
|
.where(staged: false)
|
2019-11-18 02:39:38 +08:00
|
|
|
.where(
|
|
|
|
"c.id IN (?) AND cu.notification_level IS NULL",
|
|
|
|
new_category_ids - previous_category_ids,
|
|
|
|
)
|
|
|
|
.distinct
|
|
|
|
.pluck("users.id")
|
|
|
|
|
|
|
|
json[:user_count] = user_ids.uniq.count
|
|
|
|
elsif id.start_with?("default_tags_")
|
|
|
|
previous_tag_ids = Tag.where(name: previous_value.split("|")).pluck(:id)
|
|
|
|
new_tag_ids = Tag.where(name: new_value.split("|")).pluck(:id)
|
|
|
|
|
2024-06-14 18:07:27 +08:00
|
|
|
notification_level = SiteSettingUpdateExistingUsers.tag_notification_level(id)
|
2019-11-18 02:39:38 +08:00
|
|
|
|
|
|
|
user_ids =
|
|
|
|
TagUser
|
|
|
|
.where(tag_id: previous_tag_ids - new_tag_ids, notification_level: notification_level)
|
|
|
|
.distinct
|
|
|
|
.pluck(:user_id)
|
|
|
|
user_ids +=
|
|
|
|
User
|
2020-06-19 02:09:54 +08:00
|
|
|
.real
|
2019-11-18 02:39:38 +08:00
|
|
|
.joins("CROSS JOIN tags t")
|
|
|
|
.joins("LEFT JOIN tag_users tu ON users.id = tu.user_id AND t.id = tu.tag_id")
|
2020-06-19 02:09:54 +08:00
|
|
|
.where(staged: false)
|
2019-11-18 02:39:38 +08:00
|
|
|
.where("t.id IN (?) AND tu.notification_level IS NULL", new_tag_ids - previous_tag_ids)
|
|
|
|
.distinct
|
|
|
|
.pluck("users.id")
|
|
|
|
|
|
|
|
json[:user_count] = user_ids.uniq.count
|
2024-06-14 18:07:27 +08:00
|
|
|
elsif SiteSettingUpdateExistingUsers.is_sidebar_default_setting?(id)
|
2022-10-27 06:38:50 +08:00
|
|
|
json[:user_count] = SidebarSiteSettingsBackfiller.new(
|
|
|
|
id,
|
|
|
|
previous_value: previous_value,
|
|
|
|
new_value: new_value,
|
|
|
|
).number_of_users_to_backfill
|
2019-11-18 02:39:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: json
|
|
|
|
end
|
|
|
|
|
2017-08-07 09:43:09 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def raise_access_hidden_setting(id)
|
2023-05-10 21:21:48 +08:00
|
|
|
id = id.to_sym
|
|
|
|
|
|
|
|
if SiteSetting.hidden_settings.include?(id)
|
2017-08-07 09:43:09 +08:00
|
|
|
raise Discourse::InvalidParameters, "You are not allowed to change hidden settings"
|
2014-06-10 03:17:36 +08:00
|
|
|
end
|
2023-05-10 21:21:48 +08:00
|
|
|
|
|
|
|
if SiteSetting.plugins[id] && !Discourse.plugins_by_name[SiteSetting.plugins[id]].configurable?
|
|
|
|
raise Discourse::InvalidParameters, "You are not allowed to change unconfigurable settings"
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2022-05-23 21:20:51 +08:00
|
|
|
def value_or_default(value)
|
|
|
|
value.nil? ? "" : value
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|