Skip to content

Commit

Permalink
Dev/refactor access to buttons (#16)
Browse files Browse the repository at this point in the history
* DEV: refactor access to buttons to use user.in_any_groups?

* DEV: update description in schema to use ids instead of names
  • Loading branch information
Grubba27 authored Jun 5, 2024
1 parent b793de8 commit fa4f1fb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def self.schema
title: "Who can create",
type: "string",
description:
"Enter comma, or space separated user group names. Only the members of those groups can create this topic. Leave empty to allow all logged-in users.",
"Enter comma, or space separated user group ids. Only the members of those groups can create this topic. Leave empty to allow all logged-in users.",
},
tagGroups: {
title: "Tag group dropdowns",
Expand Down
8 changes: 4 additions & 4 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ module ::DiscoursePresetTopicComposer
after_initialize do
add_to_serializer(:current_user, :topic_preset_buttons) do
buttons = JSON.parse(SiteSetting.button_types) || []
current_user_groups = scope.user.groups.pluck(:name)
current_user = scope.user

buttons.select do |button|
allowed_groups = button["access"].split(/(?:,|\s)\s*/)
allowed_groups.empty? ||
allowed_groups.any? { |group| current_user_groups.include?(group.strip) }
allowed_groups = button["access"].split(/(?:,|\s)\s*/).map(&:to_i)
allowed_groups = [Group::AUTO_GROUPS[:everyone]] if allowed_groups.empty?
current_user.in_any_groups?(allowed_groups)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/system/preset_topic_composer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def self.add_new_json(json)
],
"showTags" => false,
"tags" => "",
"access" => admin.groups.first.name,
"access" => admin.groups.first.id.to_s,
},
)
end
Expand Down

0 comments on commit fa4f1fb

Please sign in to comment.