mirror of
https://github.com/discourse/discourse.git
synced 2026-08-05 20:29:55 +08:00
Further following on from bee1be8599,
this brings greater parity between theme settings and site settings.
This commit adds a new `disallowed_groups` field to theme and component
settings, which allows theme authors to specify groups that cannot be
selected
by admins for a particular list type or object group type setting.
This is useful for cases where the group-based setting isn't usable
for a group like e.g. anonymous_users, no point allowing them for
a setting that requires a user to be logged in to take effect.
c.f.
https://meta.discourse.org/t/granular-group-based-permissions-for-anonymous-and-logged-in-users/402273/18?u=martin
### List groups
With `disallowed_groups: "4|5"` specified (`logged_in_users` &
`anonymous_users`)
<img width="734" height="450" alt="image"
src="https://github.com/user-attachments/assets/ed8d7338-65fa-44e6-8dce-7073a87076e6"
/>
### Object groups
With `disallowed_groups: "4|5"` specified (`logged_in_users` &
`anonymous_users`)
<img width="696" height="725" alt="image"
src="https://github.com/user-attachments/assets/1a9103d4-cf44-4038-9d62-3838cb779b49"
/>
135 lines
2.7 KiB
Ruby
Vendored
135 lines
2.7 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
class ThemeSettingsSerializer < ApplicationSerializer
|
|
attributes :setting,
|
|
:humanized_name,
|
|
:type,
|
|
:default,
|
|
:value,
|
|
:description,
|
|
:valid_values,
|
|
:list_type,
|
|
:disallowed_groups,
|
|
:resolve_group_membership,
|
|
:textarea,
|
|
:json_schema,
|
|
:objects_schema
|
|
|
|
def setting
|
|
object.name
|
|
end
|
|
|
|
def humanized_name
|
|
SiteSetting.humanized_name(object.name)
|
|
end
|
|
|
|
def type
|
|
object.type_name
|
|
end
|
|
|
|
def default
|
|
object.default
|
|
end
|
|
|
|
def value
|
|
object.value
|
|
end
|
|
|
|
def description
|
|
description_regexp = /^theme_metadata\.settings\.#{setting}(\.description)?$/
|
|
|
|
locale_file_description =
|
|
object.theme.internal_translations.find { |t| t.key.match?(description_regexp) }&.value
|
|
|
|
resolved_description = locale_file_description || object.description
|
|
|
|
if resolved_description
|
|
catch(:exception) do
|
|
return I18n.interpolate(resolved_description, base_path: Discourse.base_path)
|
|
end
|
|
resolved_description
|
|
end
|
|
end
|
|
|
|
def valid_values
|
|
choices = object.choices
|
|
labels = choice_labels
|
|
|
|
choices.map do |choice|
|
|
label = labels[choice.to_s]
|
|
label ? { name: label, value: choice } : choice
|
|
end
|
|
end
|
|
|
|
def include_valid_values?
|
|
object.type == ThemeSetting.types[:enum]
|
|
end
|
|
|
|
def include_description?
|
|
description.present?
|
|
end
|
|
|
|
def list_type
|
|
object.list_type
|
|
end
|
|
|
|
def include_list_type?
|
|
object.type == ThemeSetting.types[:list]
|
|
end
|
|
|
|
def disallowed_groups
|
|
object.disallowed_groups
|
|
end
|
|
|
|
def include_disallowed_groups?
|
|
object.disallowed_groups.present?
|
|
end
|
|
|
|
def textarea
|
|
object.textarea
|
|
end
|
|
|
|
def include_textarea?
|
|
object.type == ThemeSetting.types[:string]
|
|
end
|
|
|
|
def objects_schema
|
|
object.schema
|
|
end
|
|
|
|
def include_objects_schema?
|
|
object.type == ThemeSetting.types[:objects]
|
|
end
|
|
|
|
def json_schema
|
|
object.json_schema
|
|
end
|
|
|
|
def include_json_schema?
|
|
object.type == ThemeSetting.types[:string] && object.json_schema.present?
|
|
end
|
|
|
|
def resolve_group_membership
|
|
object.resolve_group_membership?
|
|
end
|
|
|
|
def include_resolve_group_membership?
|
|
object.type == ThemeSetting.types[:list] && object.list_type == "group"
|
|
end
|
|
|
|
private
|
|
|
|
def choice_labels
|
|
labels = {}
|
|
key_prefix = "theme_metadata.settings.#{setting}.choices."
|
|
|
|
object.theme.internal_translations.each do |translation|
|
|
if translation.key.start_with?(key_prefix)
|
|
choice_key = translation.key.delete_prefix(key_prefix)
|
|
labels[choice_key] = translation.value
|
|
end
|
|
end
|
|
|
|
labels
|
|
end
|
|
end
|