mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: allow user search API to restrict to group
This commit is contained in:
@@ -44,7 +44,8 @@ export default TextField.extend({
|
|||||||
exclude: excludedUsernames(),
|
exclude: excludedUsernames(),
|
||||||
includeGroups,
|
includeGroups,
|
||||||
allowedUsers,
|
allowedUsers,
|
||||||
includeMentionableGroups
|
includeMentionableGroups,
|
||||||
|
group: self.get("group")
|
||||||
});
|
});
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ var cache = {},
|
|||||||
currentTerm,
|
currentTerm,
|
||||||
oldSearch;
|
oldSearch;
|
||||||
|
|
||||||
function performSearch(term, topicId, includeGroups, includeMentionableGroups, allowedUsers, resultsFn) {
|
function performSearch(term, topicId, includeGroups, includeMentionableGroups, allowedUsers, group, resultsFn) {
|
||||||
var cached = cache[term];
|
var cached = cache[term];
|
||||||
if (cached) {
|
if (cached) {
|
||||||
resultsFn(cached);
|
resultsFn(cached);
|
||||||
@@ -19,6 +19,7 @@ function performSearch(term, topicId, includeGroups, includeMentionableGroups, a
|
|||||||
topic_id: topicId,
|
topic_id: topicId,
|
||||||
include_groups: includeGroups,
|
include_groups: includeGroups,
|
||||||
include_mentionable_groups: includeMentionableGroups,
|
include_mentionable_groups: includeMentionableGroups,
|
||||||
|
group: group,
|
||||||
topic_allowed_users: allowedUsers }
|
topic_allowed_users: allowedUsers }
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,7 +80,8 @@ export default function userSearch(options) {
|
|||||||
includeGroups = options.includeGroups,
|
includeGroups = options.includeGroups,
|
||||||
includeMentionableGroups = options.includeMentionableGroups,
|
includeMentionableGroups = options.includeMentionableGroups,
|
||||||
allowedUsers = options.allowedUsers,
|
allowedUsers = options.allowedUsers,
|
||||||
topicId = options.topicId;
|
topicId = options.topicId,
|
||||||
|
group = options.group;
|
||||||
|
|
||||||
|
|
||||||
if (oldSearch) {
|
if (oldSearch) {
|
||||||
@@ -105,10 +107,16 @@ export default function userSearch(options) {
|
|||||||
resolve(CANCELLED_STATUS);
|
resolve(CANCELLED_STATUS);
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
|
||||||
debouncedSearch(term, topicId, includeGroups, includeMentionableGroups, allowedUsers, function(r) {
|
debouncedSearch(term,
|
||||||
clearTimeout(clearPromise);
|
topicId,
|
||||||
resolve(organizeResults(r, options));
|
includeGroups,
|
||||||
});
|
includeMentionableGroups,
|
||||||
|
allowedUsers,
|
||||||
|
group,
|
||||||
|
function(r) {
|
||||||
|
clearTimeout(clearPromise);
|
||||||
|
resolve(organizeResults(r, options));
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -587,7 +587,17 @@ class UsersController < ApplicationController
|
|||||||
topic_id = topic_id.to_i if topic_id
|
topic_id = topic_id.to_i if topic_id
|
||||||
topic_allowed_users = params[:topic_allowed_users] || false
|
topic_allowed_users = params[:topic_allowed_users] || false
|
||||||
|
|
||||||
results = UserSearch.new(term, topic_id: topic_id, topic_allowed_users: topic_allowed_users, searching_user: current_user).search
|
if params[:group].present?
|
||||||
|
@group = Group.find_by(name: params[:group])
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
results = UserSearch.new(term,
|
||||||
|
topic_id: topic_id,
|
||||||
|
topic_allowed_users: topic_allowed_users,
|
||||||
|
searching_user: current_user,
|
||||||
|
group: @group
|
||||||
|
).search
|
||||||
|
|
||||||
user_fields = [:username, :upload_avatar_template]
|
user_fields = [:username, :upload_avatar_template]
|
||||||
user_fields << :name if SiteSetting.enable_names?
|
user_fields << :name if SiteSetting.enable_names?
|
||||||
|
|||||||
@@ -10,11 +10,20 @@ class UserSearch
|
|||||||
@topic_allowed_users = opts[:topic_allowed_users]
|
@topic_allowed_users = opts[:topic_allowed_users]
|
||||||
@searching_user = opts[:searching_user]
|
@searching_user = opts[:searching_user]
|
||||||
@limit = opts[:limit] || 20
|
@limit = opts[:limit] || 20
|
||||||
|
@group = opts[:group]
|
||||||
|
@guardian = Guardian.new(@searching_user)
|
||||||
|
@guardian.ensure_can_see_group!(@group) if @group
|
||||||
end
|
end
|
||||||
|
|
||||||
def scoped_users
|
def scoped_users
|
||||||
users = User.where(active: true, staged: false)
|
users = User.where(active: true, staged: false)
|
||||||
|
|
||||||
|
if @group
|
||||||
|
users = users.where('users.id IN (
|
||||||
|
SELECT user_id FROM group_users WHERE group_id = ?
|
||||||
|
)', @group.id)
|
||||||
|
end
|
||||||
|
|
||||||
unless @searching_user && @searching_user.staff?
|
unless @searching_user && @searching_user.staff?
|
||||||
users = users.not_suspended
|
users = users.not_suspended
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -43,6 +43,16 @@ describe UserSearch do
|
|||||||
expect(search_for("under_").length).to eq(1)
|
expect(search_for("under_").length).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'allows filtering by group' do
|
||||||
|
group = Fabricate(:group)
|
||||||
|
sam = Fabricate(:user, username: 'sam')
|
||||||
|
_samantha = Fabricate(:user, username: 'samantha')
|
||||||
|
group.add(sam)
|
||||||
|
|
||||||
|
results = search_for("sam", group: group)
|
||||||
|
expect(results.count).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
# this is a seriously expensive integration test,
|
# this is a seriously expensive integration test,
|
||||||
# re-creating this entire test db is too expensive reuse
|
# re-creating this entire test db is too expensive reuse
|
||||||
it "operates correctly" do
|
it "operates correctly" do
|
||||||
|
|||||||
Reference in New Issue
Block a user