A platform for community discussion. Free, open, simple.
DEV: Additional changes to ACL backend (#41251)
Martin Brennan9bc4254d4cd9
diff --git a/.skills/discourse-acl-authoring/SKILL.md b/.skills/discourse-acl-authoring/SKILL.mdindex 9c5ad37b7b0..4a569dba031 100644--- a/.skills/discourse-acl-authoring/SKILL.md+++ b/.skills/discourse-acl-authoring/SKILL.md@@ -20,7 +20,7 @@ Use this skill before adding or reviewing ACL-backed resource permissions in Dis- Put authorization at the caller boundary before `AccessControlListManager.call`; the manager is a destructive replacement service and currently assumes the caller already authorized the actor.- Always call `AccessControlListManager` for writes, including empty submitted ACL arrays, so mandatory ACLs are injected and old rows are replaced intentionally.-- Treat `AccessControlList.flattened_list` as the API shape for UI/client payloads and `AccessControlList.expand_list` as the DB insert shape.+- Treat `AccessControlList.flattened_list` as the API shape for UI/client payloads and `AccessControlList.expand_list_for_bulk_insert` as the DB insert shape.- Use Guardian ACL helpers or `AclTarget` visibility scopes for checks and target scopes instead of hand-querying ACL tables in controllers.- Register plugin ACL targets with `DiscoursePluginRegistry.register_acl_target_class` so `Site#access_control` exposes mandatory ACL metadata to the frontend.- Do not claim user ACL support is complete. Current authoring and UI flows are group-first; `allowed_user_ids` and user entries exist in lower layers but are not fully wired by `expand_list`, `preload_allowed`, cleanup jobs, or `DAccessControl`.diff --git a/.skills/discourse-acl-authoring/references/backend-model.md b/.skills/discourse-acl-authoring/references/backend-model.mdindex f90ae6f2268..490fc6f03ee 100644--- a/.skills/discourse-acl-authoring/references/backend-model.md+++ b/.skills/discourse-acl-authoring/references/backend-model.md@@ -27,7 +27,7 @@ The frontend and service params use flattened entries:]```-`AccessControlList.expand_list(list, target, owner)` groups those entries by permission and returns rows suitable for `insert_all!`.+`AccessControlList.expand_list_for_bulk_insert(list, target, owner)` groups those entries by permission and returns rows suitable for `insert_all!`.`AccessControlList.where(target: target).flattened_list` returns one entry per group per permission, including group metadata:diff --git a/.skills/discourse-acl-authoring/references/manager-and-plugin-integration.md b/.skills/discourse-acl-authoring/references/manager-and-plugin-integration.mdindex af949b5d003..1505a3ab280 100644--- a/.skills/discourse-acl-authoring/references/manager-and-plugin-integration.md+++ b/.skills/discourse-acl-authoring/references/manager-and-plugin-integration.md@@ -126,7 +126,7 @@ When migrating legacy permission columns into ACLs:## Current Limitations-- `AccessControlList.expand_list` is group-first. It does not fully handle `{ type: "user" }` authoring yet.+- `AccessControlList.expand_list_for_bulk_insert` is group-first. It does not fully handle `{ type: "user" }` authoring yet.- `preload_allowed`, `expand_list`, cleanup, and `DAccessControl` are still group-first even though lower-level matching scopes understand `allowed_user_ids`.- `flattened_list` skips missing groups and unknown target classes defensively. Validate group IDs at the service/contract boundary where user input enters; do not rely on read-path skipping as data hygiene.- The manager has caller-side authorization. Future core work may add a target policy hook; until then, do not expose it directly to untrusted callers.diff --git a/app/models/access_control_list.rb b/app/models/access_control_list.rbindex c90f35dad5c..e2227c407a4 100644--- a/app/models/access_control_list.rb+++ b/app/models/access_control_list.rb@@ -96,6 +96,8 @@ class AccessControlList < ActiveRecord::Basedef self.inject_mandatory_acl(flattened_acl, target)return flattened_acl if !target.class.has_mandatory_acl?+ flattened_acl = dedup_flattened_list(flattened_acl)+target.class.mandatory_acl.each do |mandatory_acl|next if flattened_acl.any? { |acl| AclTarget.acl_matches?(acl, mandatory_acl) }@@ -105,14 +107,32 @@ class AccessControlList < ActiveRecord::Baseflattened_aclend+ # Convenience method to expand & insert a flattened ACL in the following format:+ #+ # [{ type: "group", id: 3, permission: "edit" }]+ #+ # Converting it to include the allowed_group_ids, allowed_user_ids,+ # owner, and so on.+ def self.bulk_insert_flattened_acl!(flattened_acl, target, owner)+ bulk_insert_list = expand_list_for_bulk_insert(flattened_acl, target, owner)+ insert_all!(bulk_insert_list)+ end+# Takes a list in this format, which is the same# format from flattened_list that will come from the UI:#- # { type: "group", id: 3, permission: "edit" }]+ # [{ type: "group", id: 3, permission: "edit" }]## And converts into ACL records that can be inserted into the DB with- # .insert_all- def self.expand_list(list, target, owner)+ # .insert_all, ending up in a format like so:+ #+ # [{ permission: "edit", allowed_group_ids: [3], target_type: "Category", target_id: 123, owner: "core" }]+ #+ # The created_at and updated_at dates are automatically added+ # by AR when .insert_all! is used+ def self.expand_list_for_bulk_insert(list, target, owner)+ list = dedup_flattened_list(list)+permissions_expanded =list.each_with_object({}) do |entry, permissions|permissions[entry[:permission]] ||= {}@@ -137,6 +157,28 @@ class AccessControlList < ActiveRecord::Baseendend+ # Takes a flattened_list in this format below and removes+ # any duplicate entries, where duplicates are defined as having the same+ # type, id, and permission.+ #+ # [{ type: "group", id: 3, permission: "edit" }, { type: "group", id: 3, permission: "edit" }]+ #+ # Would be deduplicated to:+ #+ # [{ type: "group", id: 3, permission: "edit" }]+ def self.dedup_flattened_list(flattened_acl)+ flattened_acl.uniq { |acl| [acl[:type].to_sym, acl[:id], acl[:permission].to_s] }+ end++ # Takes a Group or User model instance and a provided permission and+ # converts it to the minimum needed format for a flattened ACL list+ # item like so:+ #+ # { type: :group, id: 3, permission: "edit" }+ def self.flat_acl_for(group_or_user, permission)+ { type: group_or_user.is_a?(Group) ? :group : :user, id: group_or_user.id, permission: }+ end+module AccessControlListRelationMethods# Batch-loads the allowed users and groups for every ACL in the relation# using two queries total (one per table), then memoizes the records ontodiff --git a/app/models/concerns/acl_target.rb b/app/models/concerns/acl_target.rbindex 2da848b9204..da46b7bc44f 100644--- a/app/models/concerns/acl_target.rb+++ b/app/models/concerns/acl_target.rb@@ -56,11 +56,6 @@ module AclTarget@permission_acl ||= AccessControlList.where(target: self).target_acl(self)end- def mandatory_acl_as_expanded_list(owner)- return [] if !self.class.has_mandatory_acl?- AccessControlList.expand_list(self.class.mandatory_acl, self, owner)- end-class_methods dodef acl_target_keynamediff --git a/app/services/access_control_list_manager.rb b/app/services/access_control_list_manager.rbindex acd775c07c1..5284bd73808 100644--- a/app/services/access_control_list_manager.rb+++ b/app/services/access_control_list_manager.rb@@ -52,7 +52,11 @@ class AccessControlListManagerdef insert_acls(params:, flattened_acl_with_mandatory:)bulk_insert_list =- AccessControlList.expand_list(flattened_acl_with_mandatory, params.target, params.owner)+ AccessControlList.expand_list_for_bulk_insert(+ flattened_acl_with_mandatory,+ params.target,+ params.owner,+ )@context[:new_permissions] = Acl::Target.new(flattened_acl_with_mandatory).permission_lookupAccessControlList.insert_all!(bulk_insert_list)enddiff --git a/spec/models/access_control_list_spec.rb b/spec/models/access_control_list_spec.rbindex 15e79ab01c3..c34dff26496 100644--- a/spec/models/access_control_list_spec.rb+++ b/spec/models/access_control_list_spec.rb@@ -5,14 +5,14 @@ RSpec.describe AccessControlList dofab!(:group) { Fabricate(:group, name: "marketing", full_name: "Marketing Team") }fab!(:other_group) { Fabricate(:group, name: "support", full_name: "Support Team") }- describe ".expand_list" do+ describe ".expand_list_for_bulk_insert" doit "collapses multiple groups sharing a permission into one record" dolist = [{ type: "group", id: group.id, permission: "view" },{ type: "group", id: other_group.id, permission: "view" },]- result = described_class.expand_list(list, target, "core")+ result = described_class.expand_list_for_bulk_insert(list, target, "core")expect(result.size).to eq(1)entry = result.first@@ -31,7 +31,7 @@ RSpec.describe AccessControlList do{ type: "group", id: group.id, permission: "edit" },]- result = described_class.expand_list(list, target, "core")+ result = described_class.expand_list_for_bulk_insert(list, target, "core")expect(result.map { |entry| entry[:permission] }).to contain_exactly("view", "edit")expect(result.find { |entry| entry[:permission] == "edit" }[:allowed_group_ids]).to(@@ -42,7 +42,7 @@ RSpec.describe AccessControlList doit "accepts a symbol type and a custom owner" dolist = [{ type: :group, id: group.id, permission: "view" }]- result = described_class.expand_list(list, target, "chat")+ result = described_class.expand_list_for_bulk_insert(list, target, "chat")expect(result.first[:allowed_group_ids]).to contain_exactly(group.id)expect(result.first[:owner]).to eq("chat")@@ -54,7 +54,7 @@ RSpec.describe AccessControlList do{ type: "group", id: other_group.id, permission: "edit" },]- described_class.insert_all!(described_class.expand_list(list, target, "core"))+ described_class.insert_all!(described_class.expand_list_for_bulk_insert(list, target, "core"))acls = described_class.where(target: target)expect(acls.pluck(:permission)).to contain_exactly("view", "edit")