DEV: Add plugin_modifier for groups in `users_controller#search_users` (#22329)
This commit is contained in:
parent
ed70b16db6
commit
acaea2b5c5
|
@ -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 } }
|
||||
|
|
|
@ -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: {
|
||||
|
|
Loading…
Reference in New Issue