Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix LDAP group functionality to respect service

Oliver Güntherd16859ba7787
diff --git a/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb b/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb
index 20719e6a9c7..cda0d81a458 100644
--- a/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb
+++ b/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb
@@ -36,20 +36,26 @@ module LdapGroups
def perform
return unless EnterpriseToken.allows_to?(:ldap_groups)
- begin
- LdapAuthSource.find_each do |ldap|
- Rails.logger.info { "[LDAP groups] Retrieving groups from filters for ldap auth source #{ldap.name}" }
- LdapGroups::SynchronizedFilter
- .where(auth_source_id: ldap.id)
- .find_each { |filter| OpenProject::LdapGroups::SynchronizeFilter.new(filter) }
+ User.system.run_given do
+ synchronize!
+ end
+ end
+
+ private
+
+ def synchronize!
+ LdapAuthSource.find_each do |ldap|
+ Rails.logger.info { "[LDAP groups] Retrieving groups from filters for ldap auth source #{ldap.name}" }
+ LdapGroups::SynchronizedFilter
+ .where(auth_source_id: ldap.id)
+ .find_each { |filter| OpenProject::LdapGroups::SynchronizeFilter.new(filter) }
- Rails.logger.info { "[LDAP groups] Start group synchronization for ldap auth source #{ldap.name}" }
- OpenProject::LdapGroups::Synchronization.new(ldap)
- end
- rescue StandardError => e
- msg = "[LDAP groups] Failed to run LDAP group synchronization. #{e.class.name}: #{e.message}"
- Rails.logger.error msg
+ Rails.logger.info { "[LDAP groups] Start group synchronization for ldap auth source #{ldap.name}" }
+ OpenProject::LdapGroups::Synchronization.new(ldap)
end
+ rescue StandardError => e
+ msg = "[LDAP groups] Failed to run LDAP group synchronization. #{e.class.name}: #{e.message}"
+ Rails.logger.error msg
end
end
end
\ No newline at end of file
diff --git a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
index 78047bfa962..8201545e3c2 100644
--- a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
+++ b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
@@ -108,9 +108,7 @@ module OpenProject::LdapGroups
}
end
::LdapGroups::Membership.insert_all memberships
-
- # Have to manually add the users to the group :-/
- sync.group.users << User.where(id: new_member_ids).select(:id)
+ sync.group.add_members! new_member_ids
end
##
diff --git a/modules/ldap_groups/spec/lib/synchronization_spec.rb b/modules/ldap_groups/spec/lib/synchronization_spec.rb
index 4a032f0101b..ae2cfd114f8 100644
--- a/modules/ldap_groups/spec/lib/synchronization_spec.rb
+++ b/modules/ldap_groups/spec/lib/synchronization_spec.rb
@@ -45,7 +45,12 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
let(:synced_foo) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=foo,ou=groups,dc=example,dc=com', group: group_foo, auth_source: auth_source }
let(:synced_bar) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=bar,ou=groups,dc=example,dc=com', group: group_bar, auth_source: auth_source }
- subject { described_class.new auth_source }
+ subject do
+ # Need the system user for admin permission
+ User.system.run_given do
+ described_class.new auth_source
+ end
+ end
shared_examples 'does not change membership count' do
it 'does not change membership count' do
@@ -199,14 +204,14 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
end
context 'foo group exists' do
+ let(:group_foo) { FactoryBot.create :group, lastname: 'foo_internal', members: user_aa729 }
+
before do
group_foo
synced_foo
end
it 'ignores users that are already in the group' do
- group_foo.users << user_aa729
-
# Outputs that nothing was added to sync group
expect(Rails.logger).to receive(:info).with("[LDAP groups] No users to remove for cn=foo,ou=groups,dc=example,dc=com")
expect(Rails.logger).to receive(:info).with("[LDAP groups] No new users to add for cn=foo,ou=groups,dc=example,dc=com")
@@ -221,8 +226,9 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
describe 'removing memberships' do
context 'with a user in a group thats not in ldap' do
+ let(:group_foo) { FactoryBot.create :group, lastname: 'foo_internal', members: [user_cc414, user_aa729] }
+
before do
- group_foo.users << [user_cc414, user_aa729]
synced_foo.users.create(user: user_aa729)
synced_foo.users.create(user: user_cc414)