Skip to content

Commit

Permalink
fixes #10482 - get external user group members only once during refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Cleal authored and dLobatog committed May 19, 2015
1 parent df8887a commit 0fd7412
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
8 changes: 5 additions & 3 deletions app/models/external_usergroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ class ExternalUsergroup < ActiveRecord::Base
def refresh
return false unless auth_source.respond_to?(:users_in_group)

current_users = usergroup.users.map(&:login)
all_users = usergroup.external_usergroups.map(&:users).flatten.uniq
current_users = usergroup.users.map(&:login)
my_users = users
all_other_users = (usergroup.external_usergroups - [self]).map(&:users)
all_users = (all_other_users + my_users).flatten.uniq

# We need to make sure when we refresh a external_usergroup
# other external_usergroup users remain in. Otherwise refreshing
# a external user group with no users in will empty the user group.
old_users = current_users - all_users
new_users = users - current_users
new_users = my_users - current_users

usergroup.remove_users(old_users)
usergroup.add_users(new_users)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Api::V2::ExternalUsergroupsControllerTest < ActionController::TestCase
end

test 'refresh external user group' do
ExternalUsergroup.any_instance.expects(:users).returns([]).twice
ExternalUsergroup.any_instance.expects(:users).returns([])
put :refresh, { :usergroup_id => @external_usergroup.usergroup_id,
:id => @external_usergroup.id }
assert_response :success
Expand Down
12 changes: 6 additions & 6 deletions test/unit/usergroup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,31 +211,31 @@ def populate_usergroups
end

test "delete user if not in LDAP directory" do
LdapFluff.any_instance.stubs(:valid_group?).at_least_once.with('aname').returns(false)
LdapFluff.any_instance.stubs(:valid_group?).with('aname').returns(false)
@usergroup.users << users(:one)
@usergroup.save

AuthSourceLdap.any_instance.expects(:users_in_group).at_least_once.with('aname').returns([])
AuthSourceLdap.any_instance.expects(:users_in_group).with('aname').returns([])
@usergroup.external_usergroups.select { |eu| eu.name == 'aname'}.first.refresh

refute_includes @usergroup.users, users(:one)
end

test "add user if in LDAP directory" do
LdapFluff.any_instance.stubs(:valid_group?).at_least_once.with('aname').returns(true)
LdapFluff.any_instance.stubs(:valid_group?).with('aname').returns(true)
@usergroup.save

AuthSourceLdap.any_instance.expects(:users_in_group).at_least_once.with('aname').returns([users(:one).login])
AuthSourceLdap.any_instance.expects(:users_in_group).with('aname').returns([users(:one).login])
@usergroup.external_usergroups.select { |eu| eu.name == 'aname'}.first.refresh
assert_includes @usergroup.users, users(:one)
end

test "keep user if in LDAP directory" do
LdapFluff.any_instance.stubs(:valid_group?).at_least_once.with('aname').returns(true)
LdapFluff.any_instance.stubs(:valid_group?).with('aname').returns(true)
@usergroup.users << users(:one)
@usergroup.save

AuthSourceLdap.any_instance.expects(:users_in_group).at_least_once.with('aname').returns([users(:one).login])
AuthSourceLdap.any_instance.expects(:users_in_group).with('aname').returns([users(:one).login])
@usergroup.external_usergroups.select { |eu| eu.name == 'aname'}.first.refresh
assert_includes @usergroup.users, users(:one)
end
Expand Down

0 comments on commit 0fd7412

Please sign in to comment.