Skip to content

A platform for community discussion. Free, open, simple.

DEV: Convert policy_restrict_to_staff_posts to group based access (#36157)

Ted Johanssond85a6572572c
diff --git a/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb b/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb
index e66b61d001c..fdb0c1eac96 100644
--- a/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb
+++ b/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb
@@ -98,7 +98,7 @@ class DiscoursePolicy::PolicyController < ::ApplicationController
return render_json_error(I18n.t("discourse_policy.errors.group_not_found")) if @group_ids.blank?
- if SiteSetting.policy_restrict_to_staff_posts && [email protected]&.staff?
+ if [email protected]&.in_any_groups?(SiteSetting.create_policy_allowed_groups_map)
return render_json_error(I18n.t("discourse_policy.errors.staff_only"))
end
diff --git a/plugins/discourse-policy/assets/javascripts/discourse/initializers/add-policy-builder.js b/plugins/discourse-policy/assets/javascripts/discourse/initializers/add-policy-builder.js
index 2206015f327..483117eabbd 100644
--- a/plugins/discourse-policy/assets/javascripts/discourse/initializers/add-policy-builder.js
+++ b/plugins/discourse-policy/assets/javascripts/discourse/initializers/add-policy-builder.js
@@ -3,7 +3,6 @@ import PolicyBuilder from "../components/modal/policy-builder";
function initializePolicyBuilder(api, container) {
const currentUser = api.getCurrentUser();
- const siteSettings = container.lookup("service:site-settings");
const modal = container.lookup("service:modal");
if (currentUser) {
@@ -20,8 +19,7 @@ function initializePolicyBuilder(api, container) {
},
});
},
- condition: () =>
- !siteSettings.policy_restrict_to_staff_posts || currentUser.staff,
+ condition: () => currentUser.can_create_policy,
});
}
}
diff --git a/plugins/discourse-policy/config/locales/server.en.yml b/plugins/discourse-policy/config/locales/server.en.yml
index 00d21011648..ad200ec4cf2 100644
--- a/plugins/discourse-policy/config/locales/server.en.yml
+++ b/plugins/discourse-policy/config/locales/server.en.yml
@@ -2,6 +2,7 @@ en:
site_settings:
policy_enabled: "Allow policies?"
policy_restrict_to_staff_posts: "Policies may only appear on staff posts"
+ create_policy_allowed_groups: "Group membership required for adding policies to posts"
policy_easy_revoke: "Show the accept and revoke buttons at the same time"
discourse_policy:
errors:
diff --git a/plugins/discourse-policy/config/settings.yml b/plugins/discourse-policy/config/settings.yml
index 58db380747d..a6daa0e39de 100644
--- a/plugins/discourse-policy/config/settings.yml
+++ b/plugins/discourse-policy/config/settings.yml
@@ -5,5 +5,14 @@ plugins:
policy_restrict_to_staff_posts:
client: true
default: true
+ hidden: true
+ create_policy_allowed_groups:
+ client: true
+ type: group_list
+ default: "1|2" # Admins + moderators.
+ mandatory_values: "1|2"
+ allow_any: false
+ refresh: true
+ validator: "AtLeastOneGroupValidator"
policy_easy_revoke:
default: false
diff --git a/plugins/discourse-policy/db/migrate/20251121083103_fill_create_policy_allowed_groups_based_on_deprecated_settings.rb b/plugins/discourse-policy/db/migrate/20251121083103_fill_create_policy_allowed_groups_based_on_deprecated_settings.rb
new file mode 100644
index 00000000000..e3e1347ce95
--- /dev/null
+++ b/plugins/discourse-policy/db/migrate/20251121083103_fill_create_policy_allowed_groups_based_on_deprecated_settings.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class FillCreatePolicyAllowedGroupsBasedOnDeprecatedSettings < ActiveRecord::Migration[8.0]
+ def up
+ restricted =
+ DB.query_single(
+ "SELECT value FROM site_settings WHERE name = 'policy_restrict_to_staff_posts' LIMIT 1",
+ ).first
+
+ # If deprecated setting is unrestricted, allow staff and TL0. Else default is fine.
+ if restricted == "f"
+ # Data_type 20 is group_list.
+ DB.exec(
+ "INSERT INTO site_settings(name, value, data_type, created_at, updated_at)
+ VALUES('create_policy_allowed_groups', '1|2|10', '20', NOW(), NOW())",
+ )
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-policy/plugin.rb b/plugins/discourse-policy/plugin.rb
index 49af1901f31..65a4c3e977b 100644
--- a/plugins/discourse-policy/plugin.rb
+++ b/plugins/discourse-policy/plugin.rb
@@ -54,7 +54,7 @@ after_initialize do
on(:post_process_cooked) do |doc, post|
has_group = false
- if !SiteSetting.policy_restrict_to_staff_posts || post&.user&.staff?
+ if post&.user&.in_any_groups?(SiteSetting.create_policy_allowed_groups_map)
if policy = doc.search(".policy")&.first
post_policy = post.post_policy || post.build_post_policy
@@ -156,6 +156,10 @@ after_initialize do
end
end
+ add_to_serializer(:current_user, :can_create_policy) do
+ object.in_any_groups?(SiteSetting.create_policy_allowed_groups_map)
+ end
+
add_report("unaccepted-policies") do |report|
report.modes = [:table]
diff --git a/plugins/discourse-policy/spec/lib/pretty_text_spec.rb b/plugins/discourse-policy/spec/lib/pretty_text_spec.rb
index 88e9e5b9f24..7733a859813 100644
--- a/plugins/discourse-policy/spec/lib/pretty_text_spec.rb
+++ b/plugins/discourse-policy/spec/lib/pretty_text_spec.rb
@@ -39,7 +39,7 @@ describe PrettyText do
end
it "sets the custom attribute on posts with policies" do
- SiteSetting.policy_restrict_to_staff_posts = false
+ SiteSetting.create_policy_allowed_groups = "1|2|10"
raw = <<~MD
[policy group=staff reminder=weekly]
@@ -60,7 +60,7 @@ describe PrettyText do
end
it "allows policy to expire for end users on demand" do
- SiteSetting.policy_restrict_to_staff_posts = false
+ SiteSetting.create_policy_allowed_groups = "1|2|10"
freeze_time
@@ -85,7 +85,7 @@ describe PrettyText do
end
it "resets list of accepted users if version is bumped" do
- SiteSetting.policy_restrict_to_staff_posts = false
+ SiteSetting.create_policy_allowed_groups = "1|2|10"
freeze_time
diff --git a/plugins/discourse-policy/spec/plugin_spec.rb b/plugins/discourse-policy/spec/plugin_spec.rb
index 248ae665f1f..f81f3651dc5 100644
--- a/plugins/discourse-policy/spec/plugin_spec.rb
+++ b/plugins/discourse-policy/spec/plugin_spec.rb
@@ -87,4 +87,22 @@ describe DiscoursePolicy do
end
end
end
+
+ describe "current user serializer extensions" do
+ let(:serializer) { CurrentUserSerializer.new(user1, scope: Guardian.new(user1)) }
+
+ fab!(:group)
+
+ before { SiteSetting.create_policy_allowed_groups = "1|2|#{group.id}" }
+
+ it "returns false when user is not in a policy creation group" do
+ expect(serializer.can_create_policy).to be_falsey
+ end
+
+ it "returns true when user is in a policy creation group" do
+ group.add(user1)
+
+ expect(serializer.can_create_policy).to be_truthy
+ end
+ end
end
diff --git a/plugins/discourse-policy/test/javascripts/acceptance/policy-test.js b/plugins/discourse-policy/test/javascripts/acceptance/policy-test.js
index b27045fe19c..781a32e96d6 100644
--- a/plugins/discourse-policy/test/javascripts/acceptance/policy-test.js
+++ b/plugins/discourse-policy/test/javascripts/acceptance/policy-test.js
@@ -36,7 +36,7 @@ acceptance("Discourse Policy - post", function (needs) {
});
test("insert a policy", async function (assert) {
- updateCurrentUser({ admin: true });
+ updateCurrentUser({ can_create_policy: true });
await visit("/t/-/130");
await click(".actions .edit");