Ensure whe get the current group user IDs instead of a cached one

When first removing a user, that updates group_users, but `group.user_ids` is still cached by rails.

This results in re-adding the just-removed user to the group again.

https://community.openproject.org/wp/41256
pull/11383/head
Oliver Günther 2 years ago
parent 247fc505d1
commit 77fe3507b1
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 10
      modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb

@ -79,9 +79,12 @@ module LdapGroups
def add_members_to_group(new_users) def add_members_to_group(new_users)
user_ids = new_users.map { |user| user_id(user) } user_ids = new_users.map { |user| user_id(user) }
# Ensure we use pluck to get the current DB version of user_ids
current_user_ids = group.group_users.pluck(:user_id)
call = Groups::UpdateService call = Groups::UpdateService
.new(user: User.current, model: group) .new(user: User.current, model: group)
.call(user_ids: group.user_ids + user_ids) .call(user_ids: (current_user_ids + user_ids).uniq)
call.on_success do call.on_success do
Rails.logger.debug "[LDAP groups] Added users #{user_ids} to #{group.name}" Rails.logger.debug "[LDAP groups] Added users #{user_ids} to #{group.name}"
@ -94,9 +97,12 @@ module LdapGroups
end end
def remove_members_from_group(user_ids) def remove_members_from_group(user_ids)
# Ensure we use pluck to get the current DB version of user_ids
current_user_ids = group.group_users.pluck(:user_id)
call = Groups::UpdateService call = Groups::UpdateService
.new(user: User.system, model: group) .new(user: User.system, model: group)
.call(user_ids: group.user_ids - user_ids) .call(user_ids: current_user_ids - user_ids)
call.on_success do call.on_success do
Rails.logger.debug "[LDAP groups] Removed users #{user_ids} from #{group.name}" Rails.logger.debug "[LDAP groups] Removed users #{user_ids} from #{group.name}"

Loading…
Cancel
Save