2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Admin::SiteSettingsController < Admin::AdminController
|
2017-08-07 10:43:09 +09:00
|
|
|
rescue_from Discourse::InvalidParameters do |e|
|
|
|
|
render_json_error e.message, status: 422
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def index
|
2024-03-18 08:50:39 +10:00
|
|
|
params.permit(:categories, :plugin)
|
|
|
|
|
|
|
|
render_json_dump(
|
|
|
|
site_settings:
|
|
|
|
SiteSetting.all_settings(
|
|
|
|
filter_categories: params[:categories],
|
|
|
|
filter_plugin: params[:plugin],
|
|
|
|
),
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 16:45:24 +01:00
|
|
|
def update
|
2014-01-06 13:03:53 +01:00
|
|
|
params.require(:id)
|
|
|
|
id = params[:id]
|
2024-06-14 13:07:27 +03:00
|
|
|
update_existing_users = params[:update_existing_user].present?
|
2014-01-06 13:03:53 +01:00
|
|
|
value = params[id]
|
2021-02-22 18:10:54 +00:00
|
|
|
|
2022-10-11 11:14:13 +10: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 11:14:13 +10:00
|
|
|
break new_name
|
2023-01-09 12:20:10 +00:00
|
|
|
end
|
2022-10-11 11:14:13 +10:00
|
|
|
end
|
2022-10-27 06:38:50 +08:00
|
|
|
|
2021-02-22 18:10:54 +00: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 18:41:27 +05:30
|
|
|
|
2024-10-18 11:13:34 +02:00
|
|
|
SiteSetting::Update.call(service_params.merge(setting_name: id, new_value: value)) do
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 15:37:02 +02:00
|
|
|
on_success do |contract:|
|
2024-09-03 18:30:22 +02:00
|
|
|
if update_existing_users
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 15:37:02 +02:00
|
|
|
SiteSettingUpdateExistingUsers.call(id, contract.new_value, previous_value)
|
2024-09-03 18:30:22 +02:00
|
|
|
end
|
2024-06-14 13:07:27 +03: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 18:41:27 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-07 10:43:09 +09:00
|
|
|
end
|
|
|
|
|
2019-11-18 00:09:38 +05:30
|
|
|
def user_count
|
|
|
|
params.require(:site_setting_id)
|
|
|
|
id = params[:site_setting_id]
|
|
|
|
raise Discourse::NotFound unless id.start_with?("default_")
|
2022-05-23 15:20:51 +02:00
|
|
|
new_value = value_or_default(params[id])
|
2019-11-18 00:09:38 +05:30
|
|
|
|
|
|
|
raise_access_hidden_setting(id)
|
2022-05-23 15:20:51 +02:00
|
|
|
previous_value = value_or_default(SiteSetting.public_send(id))
|
2019-11-18 00:09:38 +05:30
|
|
|
json = {}
|
|
|
|
|
2024-06-14 13:07:27 +03:00
|
|
|
if (user_option = SiteSettingUpdateExistingUsers.user_options[id.to_sym]).present?
|
2019-11-18 00:09:38 +05:30
|
|
|
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 15:20:51 +02:00
|
|
|
json[:user_count] = UserOption.human_users.where(user_option => previous_value).count
|
2019-11-18 00:09:38 +05:30
|
|
|
elsif id.start_with?("default_categories_")
|
|
|
|
previous_category_ids = previous_value.split("|")
|
|
|
|
new_category_ids = new_value.split("|")
|
|
|
|
|
2024-06-14 13:07:27 +03:00
|
|
|
notification_level = SiteSettingUpdateExistingUsers.category_notification_level(id)
|
2019-11-18 00:09:38 +05:30
|
|
|
|
|
|
|
user_ids =
|
|
|
|
CategoryUser
|
|
|
|
.where(
|
|
|
|
category_id: previous_category_ids - new_category_ids,
|
|
|
|
notification_level: notification_level,
|
2023-01-09 12:20:10 +00:00
|
|
|
)
|
2019-11-18 00:09:38 +05:30
|
|
|
.distinct
|
|
|
|
.pluck(:user_id)
|
|
|
|
user_ids +=
|
|
|
|
User
|
2020-06-18 21:09:54 +03:00
|
|
|
.real
|
2019-11-18 00:09:38 +05:30
|
|
|
.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-18 21:09:54 +03:00
|
|
|
.where(staged: false)
|
2019-11-18 00:09:38 +05:30
|
|
|
.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 13:07:27 +03:00
|
|
|
notification_level = SiteSettingUpdateExistingUsers.tag_notification_level(id)
|
2019-11-18 00:09:38 +05:30
|
|
|
|
|
|
|
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-18 21:09:54 +03:00
|
|
|
.real
|
2019-11-18 00:09:38 +05:30
|
|
|
.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-18 21:09:54 +03:00
|
|
|
.where(staged: false)
|
2019-11-18 00:09:38 +05:30
|
|
|
.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 13:07:27 +03: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 00:09:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
render json: json
|
|
|
|
end
|
|
|
|
|
2017-08-07 10:43:09 +09:00
|
|
|
private
|
|
|
|
|
|
|
|
def raise_access_hidden_setting(id)
|
2023-05-10 15:21:48 +02:00
|
|
|
id = id.to_sym
|
|
|
|
|
|
|
|
if SiteSetting.hidden_settings.include?(id)
|
2017-08-07 10:43:09 +09:00
|
|
|
raise Discourse::InvalidParameters, "You are not allowed to change hidden settings"
|
2014-06-09 15:17:36 -04:00
|
|
|
end
|
2023-05-10 15:21:48 +02: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 16:45:24 +01:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2022-05-23 15:20:51 +02:00
|
|
|
def value_or_default(value)
|
|
|
|
value.nil? ? "" : value
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|