From 2c8d2b089f99672d9fbf61dde0ce5ce77759b34b Mon Sep 17 00:00:00 2001 From: Drew Blessing Date: Thu, 15 Sep 2016 22:41:42 -0500 Subject: [PATCH] Add ssh key attr to return attrs --- lib/ee/gitlab/ldap/adapter.rb | 8 +++++++- lib/gitlab/ldap/adapter.rb | 8 ++++++-- spec/lib/ee/gitlab/ldap/adapter_spec.rb | 11 +++++++++-- spec/requests/api/ldap_spec.rb | 6 +++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/ee/gitlab/ldap/adapter.rb b/lib/ee/gitlab/ldap/adapter.rb index a2aa35e25063e9..c91ce4645d7f4b 100644 --- a/lib/ee/gitlab/ldap/adapter.rb +++ b/lib/ee/gitlab/ldap/adapter.rb @@ -1,7 +1,7 @@ # LDAP connection adapter EE mixin # # This module is intended to encapsulate EE-specific adapter methods -# and be included in the `Gitlab::LDAP::Adapter` class. +# and be **prepended** in the `Gitlab::LDAP::Adapter` class. module EE module Gitlab module LDAP @@ -39,6 +39,12 @@ def dns_for_filter(filter) attributes: %w{dn} ).map(&:dn) end + + def user_attributes + attributes = super + attributes << config.sync_ssh_keys if config.sync_ssh_keys + attributes + end end end end diff --git a/lib/gitlab/ldap/adapter.rb b/lib/gitlab/ldap/adapter.rb index 68f61fa293caa3..3f84cd816c8018 100644 --- a/lib/gitlab/ldap/adapter.rb +++ b/lib/gitlab/ldap/adapter.rb @@ -5,7 +5,7 @@ module Gitlab module LDAP class Adapter - include EE::Gitlab::LDAP::Adapter + prepend EE::Gitlab::LDAP::Adapter attr_reader :provider, :ldap @@ -76,7 +76,7 @@ def ldap_search(*args) private def user_options(field, value, limit) - options = { attributes: %W(#{config.uid} cn mail dn) } + options = { attributes: user_attributes } options[:size] = limit if limit if field.to_sym == :dn @@ -104,6 +104,10 @@ def user_filter(filter = nil) filter end end + + def user_attributes + %W(#{config.uid} cn mail dn) + end end end end diff --git a/spec/lib/ee/gitlab/ldap/adapter_spec.rb b/spec/lib/ee/gitlab/ldap/adapter_spec.rb index 5296cb28fa25f8..e8ce58e419f109 100644 --- a/spec/lib/ee/gitlab/ldap/adapter_spec.rb +++ b/spec/lib/ee/gitlab/ldap/adapter_spec.rb @@ -7,9 +7,9 @@ expect(Gitlab::LDAP::Adapter).to include_module(EE::Gitlab::LDAP::Adapter) end - describe '#groups' do - let(:adapter) { ldap_adapter('ldapmain') } + let(:adapter) { ldap_adapter('ldapmain') } + describe '#groups' do before do stub_ldap_config( group_base: 'ou=groups,dc=example,dc=com', @@ -39,4 +39,11 @@ expect(results.first.member_dns).to match_array(%w(john mary)) end end + + describe '#user_attributes' do + it 'appends EE-specific attributes' do + stub_ldap_config(uid: 'uid', sync_ssh_keys: 'sshPublicKey') + expect(adapter.user_attributes).to match_array(%w(uid dn cn mail sshPublicKey)) + end + end end diff --git a/spec/requests/api/ldap_spec.rb b/spec/requests/api/ldap_spec.rb index 81790f9d44c9fa..4161989f38282f 100644 --- a/spec/requests/api/ldap_spec.rb +++ b/spec/requests/api/ldap_spec.rb @@ -2,7 +2,10 @@ describe API::API do include ApiHelpers + include LdapHelpers + let(:user) { create(:user) } + let(:adapter) { ldap_adapter } before do groups = [ @@ -10,7 +13,8 @@ OpenStruct.new(cn: 'students') ] - allow_any_instance_of(Gitlab::LDAP::Adapter).to receive_messages(groups: groups) + allow(Gitlab::LDAP::Adapter).to receive(:new).and_return(adapter) + allow(adapter).to receive_messages(groups: groups) end describe "GET /ldap/groups" do -- GitLab