Skip to content
Snippets Groups Projects
Commit 11f3da9d authored by Rémy Coutable's avatar Rémy Coutable
Browse files

Merge branch 'set_ldap_config_attributes_defaults' into 'master'

LDAP attributes needs default values

See merge request !8465
parents 6003f6ea de4eee08
No related branches found
No related tags found
No related merge requests found
Loading
@@ -25,7 +25,7 @@ module Gitlab
Loading
@@ -25,7 +25,7 @@ module Gitlab
end end
   
def get_raw(key) def get_raw(key)
auth_hash.extra[:raw_info][key] auth_hash.extra[:raw_info][key] if auth_hash.extra
end end
   
def ldap_config def ldap_config
Loading
Loading
Loading
@@ -107,7 +107,7 @@ module Gitlab
Loading
@@ -107,7 +107,7 @@ module Gitlab
end end
   
def attributes def attributes
options['attributes'] default_attributes.merge(options['attributes'])
end end
   
def timeout def timeout
Loading
@@ -130,6 +130,16 @@ module Gitlab
Loading
@@ -130,6 +130,16 @@ module Gitlab
end end
end end
   
def default_attributes
{
'username' => %w(uid userid sAMAccountName),
'email' => %w(mail email userPrincipalName),
'name' => 'cn',
'first_name' => 'givenName',
'last_name' => 'sn'
}
end
protected protected
   
def base_options def base_options
Loading
Loading
Loading
@@ -28,7 +28,7 @@ module Gitlab
Loading
@@ -28,7 +28,7 @@ module Gitlab
end end
   
def name def name
attribute_value(:name) attribute_value(:name).first
end end
   
def uid def uid
Loading
@@ -62,14 +62,12 @@ module Gitlab
Loading
@@ -62,14 +62,12 @@ module Gitlab
# this method looks for 'mail', 'email' and 'userPrincipalName' and # this method looks for 'mail', 'email' and 'userPrincipalName' and
# returns the first with a value. # returns the first with a value.
def attribute_value(attribute) def attribute_value(attribute)
attributes = Array(config.attributes[attribute.to_sym]) attributes = Array(config.attributes[attribute.to_s])
selected_attr = attributes.find { |attr| entry.respond_to?(attr) } selected_attr = attributes.find { |attr| entry.respond_to?(attr) }
   
return nil unless selected_attr return nil unless selected_attr
   
# Some LDAP attributes return an array, entry.public_send(selected_attr)
# even if it is a single value (like 'cn')
Array(entry.public_send(selected_attr)).first
end end
end end
end end
Loading
Loading
Loading
@@ -129,4 +129,27 @@ describe Gitlab::LDAP::Config, lib: true do
Loading
@@ -129,4 +129,27 @@ describe Gitlab::LDAP::Config, lib: true do
expect(config.has_auth?).to be_falsey expect(config.has_auth?).to be_falsey
end end
end end
describe '#attributes' do
it 'uses default attributes when no custom attributes are configured' do
expect(config.attributes).to eq(config.default_attributes)
end
it 'merges the configuration attributes with default attributes' do
stub_ldap_config(
options: {
'attributes' => {
'username' => %w(sAMAccountName),
'email' => %w(userPrincipalName)
}
}
)
expect(config.attributes).to include({
'username' => %w(sAMAccountName),
'email' => %w(userPrincipalName),
'name' => 'cn'
})
end
end
end end
Loading
@@ -7,9 +7,11 @@ describe Gitlab::LDAP::Person do
Loading
@@ -7,9 +7,11 @@ describe Gitlab::LDAP::Person do
   
before do before do
stub_ldap_config( stub_ldap_config(
attributes: { options: {
name: 'cn', 'attributes' => {
email: %w(mail email userPrincipalName) 'name' => 'cn',
'email' => %w(mail email userPrincipalName)
}
} }
) )
end end
Loading
@@ -30,7 +32,7 @@ describe Gitlab::LDAP::Person do
Loading
@@ -30,7 +32,7 @@ describe Gitlab::LDAP::Person do
entry['mail'] = mail entry['mail'] = mail
person = Gitlab::LDAP::Person.new(entry, 'ldapmain') person = Gitlab::LDAP::Person.new(entry, 'ldapmain')
   
expect(person.email).to eq(mail) expect(person.email).to eq([mail])
end end
   
it 'returns the value of userPrincipalName, if mail and email are not present' do it 'returns the value of userPrincipalName, if mail and email are not present' do
Loading
@@ -38,7 +40,7 @@ describe Gitlab::LDAP::Person do
Loading
@@ -38,7 +40,7 @@ describe Gitlab::LDAP::Person do
entry['userPrincipalName'] = user_principal_name entry['userPrincipalName'] = user_principal_name
person = Gitlab::LDAP::Person.new(entry, 'ldapmain') person = Gitlab::LDAP::Person.new(entry, 'ldapmain')
   
expect(person.email).to eq(user_principal_name) expect(person.email).to eq([user_principal_name])
end end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment