##// END OF EJS Templates
Firstname, lastname and email LDAP attributes are now required if "on-the-fly register" is checked....
Jean-Philippe Lang -
r628:7e755a53b853
parent child
Show More
@@ -1,79 +1,80
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'net/ldap'
18 require 'net/ldap'
19 require 'iconv'
19 require 'iconv'
20
20
21 class AuthSourceLdap < AuthSource
21 class AuthSourceLdap < AuthSource
22 validates_presence_of :host, :port, :attr_login
22 validates_presence_of :host, :port, :attr_login
23 validates_presence_of :attr_firstname, :attr_lastname, :attr_mail, :if => Proc.new { |a| a.onthefly_register? }
23
24
24 def after_initialize
25 def after_initialize
25 self.port = 389 if self.port == 0
26 self.port = 389 if self.port == 0
26 end
27 end
27
28
28 def authenticate(login, password)
29 def authenticate(login, password)
29 attrs = []
30 attrs = []
30 # get user's DN
31 # get user's DN
31 ldap_con = initialize_ldap_con(self.account, self.account_password)
32 ldap_con = initialize_ldap_con(self.account, self.account_password)
32 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
33 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
33 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
34 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
34 dn = String.new
35 dn = String.new
35 ldap_con.search( :base => self.base_dn,
36 ldap_con.search( :base => self.base_dn,
36 :filter => object_filter & login_filter,
37 :filter => object_filter & login_filter,
37 :attributes=> ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]) do |entry|
38 :attributes=> ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]) do |entry|
38 dn = entry.dn
39 dn = entry.dn
39 attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
40 attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
40 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
41 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
41 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
42 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
42 :auth_source_id => self.id ]
43 :auth_source_id => self.id ]
43 end
44 end
44 return nil if dn.empty?
45 return nil if dn.empty?
45 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
46 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
46 # authenticate user
47 # authenticate user
47 ldap_con = initialize_ldap_con(dn, password)
48 ldap_con = initialize_ldap_con(dn, password)
48 return nil unless ldap_con.bind
49 return nil unless ldap_con.bind
49 # return user's attributes
50 # return user's attributes
50 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
51 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
51 attrs
52 attrs
52 rescue Net::LDAP::LdapError => text
53 rescue Net::LDAP::LdapError => text
53 raise "LdapError: " + text
54 raise "LdapError: " + text
54 end
55 end
55
56
56 # test the connection to the LDAP
57 # test the connection to the LDAP
57 def test_connection
58 def test_connection
58 ldap_con = initialize_ldap_con(self.account, self.account_password)
59 ldap_con = initialize_ldap_con(self.account, self.account_password)
59 ldap_con.open { }
60 ldap_con.open { }
60 rescue Net::LDAP::LdapError => text
61 rescue Net::LDAP::LdapError => text
61 raise "LdapError: " + text
62 raise "LdapError: " + text
62 end
63 end
63
64
64 def auth_method_name
65 def auth_method_name
65 "LDAP"
66 "LDAP"
66 end
67 end
67
68
68 private
69 private
69 def initialize_ldap_con(ldap_user, ldap_password)
70 def initialize_ldap_con(ldap_user, ldap_password)
70 Net::LDAP.new( {:host => self.host,
71 Net::LDAP.new( {:host => self.host,
71 :port => self.port,
72 :port => self.port,
72 :auth => { :method => :simple, :username => Iconv.new('iso-8859-15', 'utf-8').iconv(ldap_user), :password => Iconv.new('iso-8859-15', 'utf-8').iconv(ldap_password) }}
73 :auth => { :method => :simple, :username => Iconv.new('iso-8859-15', 'utf-8').iconv(ldap_user), :password => Iconv.new('iso-8859-15', 'utf-8').iconv(ldap_password) }}
73 )
74 )
74 end
75 end
75
76
76 def self.get_attr(entry, attr_name)
77 def self.get_attr(entry, attr_name)
77 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
78 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
78 end
79 end
79 end
80 end
General Comments 0
You need to be logged in to leave comments. Login now