Refactor: Extract method from AuthSourceLdap#authenticate
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3439 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
7b6b147761
commit
b3330d3995
|
@ -44,10 +44,8 @@ class AuthSourceLdap < AuthSource
|
||||||
# only ask for the DN if on-the-fly registration is disabled
|
# only ask for the DN if on-the-fly registration is disabled
|
||||||
:attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
|
:attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
|
||||||
dn = entry.dn
|
dn = entry.dn
|
||||||
attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
|
attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register?
|
||||||
:lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
|
|
||||||
:mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
|
|
||||||
:auth_source_id => self.id ] if onthefly_register?
|
|
||||||
end
|
end
|
||||||
return nil if dn.empty?
|
return nil if dn.empty?
|
||||||
logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
|
logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
|
||||||
|
@ -90,6 +88,15 @@ class AuthSourceLdap < AuthSource
|
||||||
Net::LDAP.new options
|
Net::LDAP.new options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_user_attributes_from_ldap_entry(entry)
|
||||||
|
[
|
||||||
|
:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
|
||||||
|
:lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
|
||||||
|
:mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
|
||||||
|
:auth_source_id => self.id
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
def self.get_attr(entry, attr_name)
|
def self.get_attr(entry, attr_name)
|
||||||
if !attr_name.blank?
|
if !attr_name.blank?
|
||||||
entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
|
entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
|
||||||
|
|
|
@ -52,19 +52,22 @@ class AuthSourceLdapTest < ActiveSupport::TestCase
|
||||||
context 'with a valid LDAP user' do
|
context 'with a valid LDAP user' do
|
||||||
should 'return the firstname user attributes' do
|
should 'return the firstname user attributes' do
|
||||||
response = @auth.authenticate('example1','123456')
|
response = @auth.authenticate('example1','123456')
|
||||||
assert response
|
assert response.is_a?(Array), "An array was not returned"
|
||||||
|
assert response.first.present?, "No user data returned"
|
||||||
assert_equal 'Example', response.first[:firstname]
|
assert_equal 'Example', response.first[:firstname]
|
||||||
end
|
end
|
||||||
|
|
||||||
should 'return the lastname user attributes' do
|
should 'return the lastname user attributes' do
|
||||||
response = @auth.authenticate('example1','123456')
|
response = @auth.authenticate('example1','123456')
|
||||||
assert response
|
assert response.is_a?(Array), "An array was not returned"
|
||||||
|
assert response.first.present?, "No user data returned"
|
||||||
assert_equal 'One', response.first[:lastname]
|
assert_equal 'One', response.first[:lastname]
|
||||||
end
|
end
|
||||||
|
|
||||||
should 'return mail user attributes' do
|
should 'return mail user attributes' do
|
||||||
response = @auth.authenticate('example1','123456')
|
response = @auth.authenticate('example1','123456')
|
||||||
assert response
|
assert response.is_a?(Array), "An array was not returned"
|
||||||
|
assert response.first.present?, "No user data returned"
|
||||||
assert_equal 'example1@redmine.org', response.first[:mail]
|
assert_equal 'example1@redmine.org', response.first[:mail]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue