A platform for community discussion. Free, open, simple.
DEV: Add modifier for restricting theme creation endpoints (#33313)
Osama Sayegh12dbbadbc18f
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rbindex 2928910aa8c..ea2f924773a 100644--- a/app/controllers/admin/themes_controller.rb+++ b/app/controllers/admin/themes_controller.rb@@ -7,6 +7,7 @@ class Admin::ThemesController < Admin::AdminControllerskip_before_action :check_xhr, only: %i[show preview export]before_action :ensure_admin+ before_action :ensure_theme_creation_is_allowed, only: %i[create import]def previewtheme = Theme.find_by(id: params[:id])@@ -506,4 +507,8 @@ class Admin::ThemesController < Admin::AdminControllerdef theme_usercurrent_userend++ def ensure_theme_creation_is_allowed+ guardian.ensure_can_create_theme!+ endenddiff --git a/lib/guardian/user_guardian.rb b/lib/guardian/user_guardian.rbindex 11833a2f2b5..00f1e907c9d 100644--- a/lib/guardian/user_guardian.rb+++ b/lib/guardian/user_guardian.rb@@ -224,4 +224,12 @@ module UserGuardiandef can_change_tracking_preferences?(user)(SiteSetting.allow_changing_staged_user_tracking || !user.staged) && can_edit_user?(user)end++ def can_create_theme?+ return false if !is_admin?+ # this modifier is used to further restrict theme creation, it's not+ # possible to use this modifier to open up theme creation permissions (e.g.+ # to non-admins)+ DiscoursePluginRegistry.apply_modifier(:user_guardian_can_create_theme, true, self)+ endenddiff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rbindex 285d487d781..ef0ed36b55c 100644--- a/spec/requests/admin/themes_controller_spec.rb+++ b/spec/requests/admin/themes_controller_spec.rb@@ -403,6 +403,39 @@ RSpec.describe Admin::ThemesController doexpect(json["theme"]["auto_update"]).to eq(false)expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)end++ context "with registered user_guardian_can_create_theme" do+ after { DiscoursePluginRegistry.reset! }++ it "doesn't allow importing a theme if the modifier returns false" do+ plugin_instance = Plugin::Instance.new+ can_create = true++ plugin_instance.register_modifier(:user_guardian_can_create_theme) do |val, guardian|+ expect(guardian.user).to eq(admin)+ can_create+ end++ RemoteTheme.stubs(:import_theme).returns(Fabricate(:theme))+ post "/admin/themes/import.json",+ params: {+ remote: "https://github.com/discourse/discourse-brand-header.git",+ }+ expect(response.status).to eq(201)+ json = response.parsed_body+ expect(json["theme"]).to be_present++ can_create = false++ expect do+ post "/admin/themes/import.json",+ params: {+ remote: "https://github.com/discourse/discourse-brand-header-2.git",+ }+ end.not_to change { Theme.count }+ expect(response.status).to eq(403)+ end+ endendshared_examples "theme import not allowed" do@@ -635,6 +668,44 @@ RSpec.describe Admin::ThemesController do)endend++ context "with registered user_guardian_can_create_theme" do+ after { DiscoursePluginRegistry.reset! }++ it "doesn't allow theme creation if the modifier returns false" do+ plugin_instance = Plugin::Instance.new+ can_create = true++ plugin_instance.register_modifier(:user_guardian_can_create_theme) do |val, guardian|+ expect(guardian.user).to eq(admin)+ can_create+ end++ post "/admin/themes.json",+ params: {+ theme: {+ name: "my test name",+ theme_fields: [name: "scss", target: "common", value: "body{color: red;}"],+ },+ }+ expect(response.status).to eq(201)+ json = response.parsed_body+ expect(json["theme"]).to be_present++ can_create = false++ expect do+ post "/admin/themes.json",+ params: {+ theme: {+ name: "my test name 2",+ theme_fields: [name: "scss", target: "common", value: "body{color: red;}"],+ },+ }+ end.not_to change { Theme.count }+ expect(response.status).to eq(403)+ end+ endendshared_examples "theme creation not allowed" do