0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/lib/discourse_plugin_registry.rb
Martin Brennan 5823e4e3b2
FEATURE: Access control list UI and backend (#41009)
Introduces a new way of handling permissions for a target entity
within Discourse, including integration with `Guardian` and `User`
models. For this initial PR, only group-based permissions will work
with this new model, a followup PR will introduce user-based
permissions.

The initial use case for this is securing Kanban Boards in Discourse
Kanban,
see https://github.com/discourse/discourse-kanban/pull/58 for the sister
PR.
In future, we will use this in more places in core, like Chat, Category,
etc.

The new model is called `AccessControlList`, here is the schema:

* `target_id`/`target_type` - Polymorphic columns, can point to any
other model
* `owner` - A string indicating whether the permission is owned by
`core` or a plugin e.g. `discourse-kanban`
* `permission` - A free text field, which can be whatever the target
requires, but defaults are `edit`, `view`, `manage`, and `own`. `own` at
this time is a special permission that should be added to whatever user
creates the ACL at first, but should not be shown in any UI
* `allowed_group_ids` - An array of group IDs which have this permission
* `allowed_user_ids` - An array of group IDs which have this permission

A component, called `DAccessControl`, is also introduced to display
these permissions
and the groups (and soon users) who have them. The component allows a
custom description
label, and the list of permissions can have their text and description
modified,
and can also have permissions removed or added as needed for the target:

<img width="612" height="222" alt="image"
src="https://github.com/user-attachments/assets/88f8d70a-f965-4ef2-9e64-5f8b2bf3fa12"
/>

<img width="598" height="380" alt="image"
src="https://github.com/user-attachments/assets/db90b956-86c2-4ca7-9b63-4c3eb0bad5ef"
/>

Each target entity can define their own `mandatory_acl` array which
is similar to how `mandatory_values` for site settings. For example,
Kanban Boards have a mandatory ACL of the admins group ID having
the `manage` permission for the board, since admins always need to
be able to see + manage boards. This allows us to avoid hardcoding
admin/staff escape hatches in guardian/permissions code.

These will be shown as disabled rows in the `DAccessControl` component:

<img width="577" height="248" alt="image"
src="https://github.com/user-attachments/assets/d9bf1754-0530-4edb-b89f-28887cd613d6"
/>

This commit also introduces a `full_name` calculation for automatic
groups, so we can display a nicer version of the name for things
like `admins`, `staff`, `trust_level_0` in the UI.
2026-06-29 09:44:58 +10:00

324 lines
11 KiB
Ruby
Vendored

# frozen_string_literal: true
# A class that handles interaction between a plugin and the Discourse App.
#
class DiscoursePluginRegistry
# Non-default plugin stylesheet targets, each rendered as its own <link> tag.
STYLESHEET_TARGETS = %i[desktop mobile admin]
@@register_names = Set.new
# Plugins often need to be able to register additional handlers, data, or
# classes that will be used by core classes. This should be used if you
# need to control which type the registry is, and if it doesn't need to
# be removed if the plugin is disabled.
#
# Shortcut to create new register in the plugin registry
# - Register is created in a class variable using the specified name/type
# - Defines singleton method to access the register
# - Defines instance method as a shortcut to the singleton method
# - Automatically deletes the register on registry.reset!
def self.define_register(register_name, type)
return if respond_to?(register_name)
@@register_names << register_name
define_singleton_method(register_name) do
instance_variable_get(:"@#{register_name}") ||
instance_variable_set(:"@#{register_name}", type.new)
end
define_method(register_name) { self.class.public_send(register_name) }
end
# Plugins often need to add values to a list, and we need to filter those
# lists at runtime to ignore values from disabled plugins. Unlike define_register,
# the type of the register cannot be defined, and is always Array.
#
# Create a new register (see `define_register`) with some additions:
# - Register is created in a class variable using the specified name/type
# - Defines singleton method to access the register
# - Defines instance method as a shortcut to the singleton method
# - Automatically deletes the register on registry.reset!
def self.define_filtered_register(register_name)
return if respond_to?(register_name)
define_register(register_name, Array)
singleton_class.alias_method :"_raw_#{register_name}", :"#{register_name}"
define_singleton_method(register_name) do
public_send(:"_raw_#{register_name}").filter_map { |h| h[:value] if h[:plugin].enabled? }.uniq
end
define_singleton_method("register_#{register_name.to_s.singularize}") do |value, plugin|
public_send(:"_raw_#{register_name}") << { plugin: plugin, value: value }
end
yield(self) if block_given?
end
define_register :javascripts, Set
define_register :auth_providers, Set
define_register :service_workers, Set
define_register :stylesheets, Hash
define_register :mobile_stylesheets, Hash
define_register :desktop_stylesheets, Hash
define_register :admin_stylesheets, Hash
define_register :color_definition_stylesheets, Hash
define_register :serialized_current_user_fields, Set
define_register :seed_data, ActiveSupport::HashWithIndifferentAccess
define_register :locales, ActiveSupport::HashWithIndifferentAccess
define_register :svg_icons, Set
define_register :custom_html, Hash
define_register :html_builders, Hash
define_register :seed_path_builders, Set
define_register :vendored_pretty_text, Set
define_register :vendored_core_pretty_text, Set
define_register :seedfu_filter, Set
define_register :demon_processes, Set
define_register :groups_callback_for_users_search_controller_action, Hash
define_register :mail_pollers, Set
define_register :site_setting_areas, Set
define_register :admin_config_login_routes, Set
define_register :discourse_dev_populate_reviewable_types, Set
define_register :category_update_param_with_callback, Hash
define_filtered_register :staff_user_custom_fields
define_filtered_register :public_user_custom_fields
define_filtered_register :staff_editable_topic_custom_fields
define_filtered_register :public_editable_topic_custom_fields
define_filtered_register :self_editable_user_custom_fields
define_filtered_register :staff_editable_user_custom_fields
define_filtered_register :editable_group_custom_fields
define_filtered_register :group_params
define_filtered_register :topic_thumbnail_sizes
define_filtered_register :topic_preloader_associations
define_filtered_register :category_list_topics_preloader_associations
define_filtered_register :api_parameter_routes
define_filtered_register :api_key_scope_mappings
define_filtered_register :user_api_key_scope_mappings
define_filtered_register :permitted_bulk_action_parameters
define_filtered_register :reviewable_params
define_filtered_register :reviewable_score_links
define_filtered_register :presence_channel_prefixes
define_filtered_register :email_notification_filters
define_filtered_register :push_notification_filters
define_filtered_register :notification_consolidation_plans
define_filtered_register :email_unsubscribers
define_filtered_register :user_destroyer_on_content_deletion_callbacks
define_filtered_register :hashtag_autocomplete_data_sources
define_filtered_register :hashtag_autocomplete_contextual_type_priorities
define_filtered_register :search_groups_set_query_callbacks
define_filtered_register :search_handlers
define_filtered_register :stats
define_filtered_register :admin_dashboard_highlight_kpis
define_filtered_register :bookmarkables
define_filtered_register :admin_dashboard_report_sources
define_filtered_register :list_suggested_for_providers
define_filtered_register :post_action_notify_user_handlers
define_filtered_register :post_strippers
define_filtered_register :problem_checks
define_filtered_register :flag_applies_to_types
define_filtered_register :calendar_subscription_feeds
define_filtered_register :custom_filter_mappings
define_filtered_register :acl_target_classes
define_filtered_register :reviewable_types do |singleton|
singleton.define_singleton_method("reviewable_types_lookup") do
public_send(:_raw_reviewable_types)
.filter_map { |h| { plugin: h[:plugin].name, klass: h[:value] } if h[:plugin].enabled? }
.uniq
end
end
def self.register_auth_provider(auth_provider)
auth_providers << auth_provider
end
def self.register_mail_poller(mail_poller)
mail_pollers << mail_poller
end
def register_js(filename, options = {})
# If we have a server side option, add that too.
self.class.javascripts << filename
end
def self.register_service_worker(filename, options = {})
service_workers << filename
end
def self.register_svg_icon(icon)
svg_icons << icon.strip
end
def register_css(filename, plugin_directory_name)
self.class.stylesheets[plugin_directory_name] ||= Set.new
self.class.stylesheets[plugin_directory_name] << filename
end
def self.register_locale(locale, options = {})
locales[locale] = options
end
def self.unregister_locale(locale)
raise "unregister_locale can only be used in tests" if !Rails.env.test?
locales.delete(locale)
end
def register_archetype(name, options = {})
Archetype.register(name, options)
end
JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6\z/
def self.register_asset(asset, opts = nil, plugin_directory_name = nil)
if asset =~ JS_REGEX
if opts == :vendored_pretty_text
vendored_pretty_text << asset
elsif opts == :vendored_core_pretty_text
vendored_core_pretty_text << asset
else
javascripts << asset
end
elsif asset =~ /\.css$|\.scss\z/
if opts == :mobile
mobile_stylesheets[plugin_directory_name] ||= Set.new
mobile_stylesheets[plugin_directory_name] << asset
elsif opts == :desktop
desktop_stylesheets[plugin_directory_name] ||= Set.new
desktop_stylesheets[plugin_directory_name] << asset
elsif opts == :admin
admin_stylesheets[plugin_directory_name] ||= Set.new
admin_stylesheets[plugin_directory_name] << asset
elsif opts == :color_definitions
color_definition_stylesheets[plugin_directory_name] = asset
else
stylesheets[plugin_directory_name] ||= Set.new
stylesheets[plugin_directory_name] << asset
end
end
end
def self.stylesheets_exists?(plugin_directory_name, target = nil)
register = target.in?(STYLESHEET_TARGETS) ? "#{target}_stylesheets" : "stylesheets"
public_send(register)[plugin_directory_name].present?
end
def self.register_seed_data(key, value)
seed_data[key] = value
end
def self.register_seed_path_builder(&block)
seed_path_builders << block
end
def self.register_html_builder(name, &block)
html_builders[name] ||= []
html_builders[name] << block
end
def self.build_html(name, ctx = nil, **kwargs)
builders = html_builders[name] || []
builders.map { |b| b.call(ctx, **kwargs) }.join("\n").html_safe
end
def self.seed_paths
result = SeedFu.fixture_paths.dup
seed_path_builders.each { |b| result += b.call } if GlobalSetting.load_plugins?
result.uniq
end
def self.register_seedfu_filter(filter = nil)
seedfu_filter << filter
end
VENDORED_CORE_PRETTY_TEXT_MAP = {
"moment.js" => "frontend/discourse/node_modules/moment/moment.js",
"moment-timezone.js" =>
"frontend/discourse/node_modules/moment-timezone/builds/moment-timezone-with-data.js",
}
def self.core_asset_for_name(name)
asset = VENDORED_CORE_PRETTY_TEXT_MAP[name]
raise KeyError, "Asset #{name} not found in #{VENDORED_CORE_PRETTY_TEXT_MAP}" unless asset
asset
end
def self.clear_modifiers!
if Rails.env.test? && GlobalSetting.load_plugins?
raise "Clearing modifiers during a plugin spec run will affect all future specs. Use unregister_modifier instead."
end
@modifiers = nil
end
def self.register_modifier(plugin_instance, name, &blk)
@modifiers ||= {}
modifiers = @modifiers[name] ||= []
modifiers << [plugin_instance, blk]
end
def self.unregister_modifier(plugin_instance, name, &blk)
raise "unregister_modifier can only be used in tests" if !Rails.env.test?
modifiers_for_name = @modifiers&.[](name)
raise "no #{name} modifiers found" if !modifiers_for_name
i = modifiers_for_name.find_index { |info| info == [plugin_instance, blk] }
raise "no modifier found for that plugin/block combination" if !i
modifiers_for_name.delete_at(i)
end
def self.apply_modifier(name, arg, *more_args)
return arg if !@modifiers
registered_modifiers = @modifiers[name]
return arg if !registered_modifiers
# iterate as fast as possible to minimize cost (avoiding each)
# also erases one stack frame
length = registered_modifiers.length
index = 0
while index < length
plugin_instance, block = registered_modifiers[index]
arg = block.call(arg, *more_args) if plugin_instance.enabled?
index += 1
end
arg
end
def self.reset!
@@register_names.each { |name| instance_variable_set(:"@#{name}", nil) }
clear_modifiers!
end
def self.reset_register!(register_name)
found_register = @@register_names.detect { |name| name == register_name }
instance_variable_set(:"@#{found_register}", nil) if found_register
end
end