##// END OF EJS Templates
Refactor: extract AuthSourceLdap#search_attributes...
Eric Davis -
r3339:d828122009cd
parent child
Show More
@@ -1,110 +1,119
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'net/ldap'
19 19 require 'iconv'
20 20
21 21 class AuthSourceLdap < AuthSource
22 22 validates_presence_of :host, :port, :attr_login
23 23 validates_length_of :name, :host, :account_password, :maximum => 60, :allow_nil => true
24 24 validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true
25 25 validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
26 26 validates_numericality_of :port, :only_integer => true
27 27
28 28 before_validation :strip_ldap_attributes
29 29
30 30 def after_initialize
31 31 self.port = 389 if self.port == 0
32 32 end
33 33
34 34 def authenticate(login, password)
35 35 return nil if login.blank? || password.blank?
36 36 attrs = []
37 37 # get user's DN
38 38 ldap_con = initialize_ldap_con(self.account, self.account_password)
39 39 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
40 40 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
41 41 dn = String.new
42 42 ldap_con.search( :base => self.base_dn,
43 43 :filter => object_filter & login_filter,
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|
44 :attributes=> search_attributes) do |entry|
46 45 dn = entry.dn
47 46 attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register?
48 47 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
49 48
50 49 end
51 50
52 51 if authenticate_dn(dn, password)
53 52 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
54 53 return attrs
55 54 end
56 55 rescue Net::LDAP::LdapError => text
57 56 raise "LdapError: " + text
58 57 end
59 58
60 59 # test the connection to the LDAP
61 60 def test_connection
62 61 ldap_con = initialize_ldap_con(self.account, self.account_password)
63 62 ldap_con.open { }
64 63 rescue Net::LDAP::LdapError => text
65 64 raise "LdapError: " + text
66 65 end
67 66
68 67 def auth_method_name
69 68 "LDAP"
70 69 end
71 70
72 71 private
73 72
74 73 def strip_ldap_attributes
75 74 [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
76 75 write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
77 76 end
78 77 end
79 78
80 79 def initialize_ldap_con(ldap_user, ldap_password)
81 80 options = { :host => self.host,
82 81 :port => self.port,
83 82 :encryption => (self.tls ? :simple_tls : nil)
84 83 }
85 84 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
86 85 Net::LDAP.new options
87 86 end
88 87
89 88 def get_user_attributes_from_ldap_entry(entry)
90 89 [
91 90 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
92 91 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
93 92 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
94 93 :auth_source_id => self.id
95 94 ]
96 95 end
97 96
97 # Return the attributes needed for the LDAP search. It will only
98 # include the user attributes if on-the-fly registration is enabled
99 def search_attributes
100 if onthefly_register?
101 ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]
102 else
103 ['dn']
104 end
105 end
106
98 107 # Check if a DN (user record) authenticates with the password
99 108 def authenticate_dn(dn, password)
100 109 if dn.present? && password.present?
101 110 initialize_ldap_con(dn, password).bind
102 111 end
103 112 end
104 113
105 114 def self.get_attr(entry, attr_name)
106 115 if !attr_name.blank?
107 116 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
108 117 end
109 118 end
110 119 end
General Comments 0
You need to be logged in to leave comments. Login now