Skip to content

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

DEV: Add plugin_modifier for groups in `users_controller#search_users` (#22329)

Mark VanLandinghamacaea2b5c5c1
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 815266b0dd4..29063caae6b 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1234,6 +1234,11 @@ class UsersController < ApplicationController
groups = block.call(groups, current_user) if params[param_name.to_s]
end
+ # the plugin registry callbacks above are only evaluated when a param
+ # is present matching the name of the callback. Any modifier registered using
+ # register_modifier(:groups_for_users_search) will be evaluated without needing the
+ # param.
+ groups = DiscoursePluginRegistry.apply_modifier(:groups_for_users_search, groups)
groups = Group.search_groups(term, groups: groups, sort: :auto)
to_render[:groups] = groups.map { |m| { name: m.name, full_name: m.full_name } }
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 2dac1fcadb6..d11c80cef33 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -4977,6 +4977,26 @@ RSpec.describe UsersController do
DiscoursePluginRegistry.reset!
end
+ it "allows plugins to use apply modifiers to the groups filter" do
+ get "/u/search/users.json", params: { include_groups: "true", term: "a" }
+
+ expect(response.status).to eq(200)
+ initial_groups = response.parsed_body["groups"]
+ expect(initial_groups.count).to eq(6)
+
+ Plugin::Instance
+ .new
+ .register_modifier(:groups_for_users_search) do |groups|
+ groups.where(name: initial_groups.first["name"])
+ end
+
+ get "/u/search/users.json", params: { include_groups: "true", term: "a" }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["groups"].count).to eq(1)
+
+ DiscoursePluginRegistry.reset!
+ end
+
it "doesn't search for groups" do
get "/u/search/users.json",
params: {