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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Correctly skip user admin mapping when syncing users
Oliver Güntherffdf1caf0dc4
diff --git a/app/models/ldap_auth_source.rb b/app/models/ldap_auth_source.rbindex e57285473af..27cc5983074 100644--- a/app/models/ldap_auth_source.rb+++ b/app/models/ldap_auth_source.rb@@ -88,15 +88,23 @@ class LdapAuthSource < AuthSourceenddef get_user_attributes_from_ldap_entry(entry)- {+ base_attributes = {dn: entry.dn,- login: LdapAuthSource.get_attr(entry, attr_login),- firstname: LdapAuthSource.get_attr(entry, attr_firstname),- lastname: LdapAuthSource.get_attr(entry, attr_lastname),- mail: LdapAuthSource.get_attr(entry, attr_mail),- admin: !!LdapAuthSource.get_attr(entry, attr_admin),auth_source_id: id}++ base_attributes.merge mapped_attributes(entry)+ end++ def mapped_attributes(entry)+ %i[login firstname lastname mail admin].each_with_object({}) do |key, hash|+ ldap_attribute = send(:"attr_#{key}")+ next if ldap_attribute.blank?++ val = LdapAuthSource.get_attr(entry, ldap_attribute)+ val = !!ActiveRecord::Type::Boolean.new.cast(val) if key == :admin+ hash[key] = val+ endend# Return the attributes needed for the LDAP search.diff --git a/spec/models/ldap_auth_source_spec.rb b/spec/models/ldap_auth_source_spec.rbindex c541db3eb53..302aab6ca07 100644--- a/spec/models/ldap_auth_source_spec.rb+++ b/spec/models/ldap_auth_source_spec.rb@@ -55,6 +55,87 @@ describe LdapAuthSource, type: :model doendend+ describe 'admin attribute mapping' do+ let(:auth_source) do+ build :ldap_auth_source,+ attr_login: 'uid',+ attr_firstname: 'givenName',+ attr_lastname: 'sn',+ attr_mail: 'mail',+ attr_admin: attr_admin+ end+ let(:entry) do+ Net::LDAP::Entry.new('uid=login,foo=bar').tap do |entry|+ entry['uid'] = 'login'+ entry['givenName'] = 'abc'+ entry['sn'] = 'lastname'+ entry['mail'] = '[email protected]'+ entry['admin'] = admin_value+ end+ end++ subject { auth_source.get_user_attributes_from_ldap_entry(entry) }++ context 'when attribute defined and not present' do+ let(:attr_admin) { 'admin' }+ let(:admin_value) { nil }++ it 'returns it as false' do+ expect(subject).to have_key(:admin)+ expect(subject[:admin]).to be false+ end+ end++ context 'when attribute defined and castable number' do+ let(:attr_admin) { 'admin' }+ let(:admin_value) { '1' }++ it 'does return the mapping' do+ expect(subject).to have_key(:admin)+ expect(subject[:admin]).to be true+ end+ end++ context 'when attribute defined and boolean' do+ let(:attr_admin) { 'admin' }+ let(:admin_value) { false }++ it 'does return the mapping' do+ expect(subject).to have_key(:admin)+ expect(subject[:admin]).to be false+ end+ end++ context 'when attribute defined and true string' do+ let(:attr_admin) { 'admin' }+ let(:admin_value) { 'true' }++ it 'does return the mapping' do+ expect(subject).to have_key(:admin)+ expect(subject[:admin]).to be true+ end+ end++ context 'when attribute defined and false string' do+ let(:attr_admin) { 'admin' }+ let(:admin_value) { 'false' }++ it 'does return the mapping' do+ expect(subject).to have_key(:admin)+ expect(subject[:admin]).to be false+ end+ end++ context 'when attribute not defined and set' do+ let(:attr_admin) { nil }+ let(:admin_value) { true }++ it 'does not return an admin mapping' do+ expect(subject).not_to have_key(:admin)+ end+ end+ end+describe 'with live LDAP' dobefore(:all) doldif = Rails.root.join('spec/fixtures/ldap/users.ldif')diff --git a/spec/services/ldap/synchronize_users_service_integration_spec.rb b/spec/services/ldap/synchronize_users_service_integration_spec.rbindex 52e9b5f24ed..367925d10ee 100644--- a/spec/services/ldap/synchronize_users_service_integration_spec.rb+++ b/spec/services/ldap/synchronize_users_service_integration_spec.rb@@ -7,6 +7,18 @@ describe Ldap::SynchronizeUsersService dodescribed_class.new(auth_source).callend+ context 'when updating an admin' do+ let!(:user_aa729) { create :user, login: 'aa729', firstname: 'Foobar', auth_source: auth_source, admin: true }++ it 'does not update the admin attribute if not defined (Regression #42396)' do+ expect(user_aa729).to be_admin++ subject++ expect(user_aa729.reload).to be_admin+ end+ end+context 'when updating users' dolet!(:user_aa729) { create :user, login: 'aa729', firstname: 'Foobar', auth_source: auth_source }let!(:user_bb459) { create :user, login: 'bb459', firstname: 'Bla', auth_source: auth_source }