##// END OF EJS Templates
Refactor: Extract method from AuthSourceLdap#authenticate...
Eric Davis -
r3325:b3330d399543
parent child
Show More
@@ -44,10 +44,8 class AuthSourceLdap < AuthSource
44 # only ask for the DN if on-the-fly registration is disabled
44 # only ask for the DN if on-the-fly registration is disabled
45 :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
45 :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
46 dn = entry.dn
46 dn = entry.dn
47 attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
47 attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register?
48 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
48
49 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
50 :auth_source_id => self.id ] if onthefly_register?
51 end
49 end
52 return nil if dn.empty?
50 return nil if dn.empty?
53 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
51 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
@@ -89,6 +87,15 class AuthSourceLdap < AuthSource
89 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
87 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
90 Net::LDAP.new options
88 Net::LDAP.new options
91 end
89 end
90
91 def get_user_attributes_from_ldap_entry(entry)
92 [
93 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
94 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
95 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
96 :auth_source_id => self.id
97 ]
98 end
92
99
93 def self.get_attr(entry, attr_name)
100 def self.get_attr(entry, attr_name)
94 if !attr_name.blank?
101 if !attr_name.blank?
@@ -52,19 +52,22 class AuthSourceLdapTest < ActiveSupport::TestCase
52 context 'with a valid LDAP user' do
52 context 'with a valid LDAP user' do
53 should 'return the firstname user attributes' do
53 should 'return the firstname user attributes' do
54 response = @auth.authenticate('example1','123456')
54 response = @auth.authenticate('example1','123456')
55 assert response
55 assert response.is_a?(Array), "An array was not returned"
56 assert response.first.present?, "No user data returned"
56 assert_equal 'Example', response.first[:firstname]
57 assert_equal 'Example', response.first[:firstname]
57 end
58 end
58
59
59 should 'return the lastname user attributes' do
60 should 'return the lastname user attributes' do
60 response = @auth.authenticate('example1','123456')
61 response = @auth.authenticate('example1','123456')
61 assert response
62 assert response.is_a?(Array), "An array was not returned"
63 assert response.first.present?, "No user data returned"
62 assert_equal 'One', response.first[:lastname]
64 assert_equal 'One', response.first[:lastname]
63 end
65 end
64
66
65 should 'return mail user attributes' do
67 should 'return mail user attributes' do
66 response = @auth.authenticate('example1','123456')
68 response = @auth.authenticate('example1','123456')
67 assert response
69 assert response.is_a?(Array), "An array was not returned"
70 assert response.first.present?, "No user data returned"
68 assert_equal 'example1@redmine.org', response.first[:mail]
71 assert_equal 'example1@redmine.org', response.first[:mail]
69 end
72 end
70 end
73 end
General Comments 0
You need to be logged in to leave comments. Login now