##// END OF EJS Templates
r18633@gaspard (orig r1875): jplang | 2008-09-17 21:18:31 +0200...
r18633@gaspard (orig r1875): jplang | 2008-09-17 21:18:31 +0200 reposman: change #log arguments. r18634@gaspard (orig r1876): jplang | 2008-09-17 21:38:20 +0200 Slight change on git repository creation command. r18635@gaspard (orig r1877): jplang | 2008-09-17 21:47:36 +0200 Make --command option usable on Windows. r18636@gaspard (orig r1878): jplang | 2008-09-19 17:32:52 +0200 Adds watch/unwatch functionality at forum topic level (#1912). Users who create/reply a topic are automatically added as watchers but are now able to unwatch the topic. r18637@gaspard (orig r1879): winterheart | 2008-09-19 18:15:49 +0200 update of ru.yml r18638@gaspard (orig r1880): winterheart | 2008-09-19 18:17:35 +0200 #1918, translation for zh-tw r18639@gaspard (orig r1881): winterheart | 2008-09-19 18:20:45 +0200 fixed #1920, patch for Hungarian language r18640@gaspard (orig r1882): winterheart | 2008-09-19 18:23:04 +0200 patch #1922, update for nl.yml r18641@gaspard (orig r1883): winterheart | 2008-09-19 18:26:19 +0200 #1923, updated zh.yml r18642@gaspard (orig r1884): winterheart | 2008-09-19 18:30:39 +0200 #1924, update for da.yml r18643@gaspard (orig r1885): winterheart | 2008-09-19 18:33:08 +0200 #1925, patch for lt.yml git-svn-id: http://redmine.rubyforge.org/svn/branches/nbc@1886 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r1661:eb1d969237a9
r1884:d8549c55411a
Show More
auth_source_ldap.rb
87 lines | 3.5 KiB | text/x-ruby | RubyLexer
/ app / models / auth_source_ldap.rb
# redMine - project management software
# Copyright (C) 2006 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'net/ldap'
require 'iconv'
class AuthSourceLdap < AuthSource
validates_presence_of :host, :port, :attr_login
validates_length_of :name, :host, :account_password, :maximum => 60, :allow_nil => true
validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
validates_numericality_of :port, :only_integer => true
def after_initialize
self.port = 389 if self.port == 0
end
def authenticate(login, password)
return nil if login.blank? || password.blank?
attrs = []
# get user's DN
ldap_con = initialize_ldap_con(self.account, self.account_password)
login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
dn = String.new
ldap_con.search( :base => self.base_dn,
:filter => object_filter & login_filter,
# 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|
dn = entry.dn
attrs = [: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 ] if onthefly_register?
end
return nil if dn.empty?
logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
# authenticate user
ldap_con = initialize_ldap_con(dn, password)
return nil unless ldap_con.bind
# return user's attributes
logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
attrs
rescue Net::LDAP::LdapError => text
raise "LdapError: " + text
end
# test the connection to the LDAP
def test_connection
ldap_con = initialize_ldap_con(self.account, self.account_password)
ldap_con.open { }
rescue Net::LDAP::LdapError => text
raise "LdapError: " + text
end
def auth_method_name
"LDAP"
end
private
def initialize_ldap_con(ldap_user, ldap_password)
options = { :host => self.host,
:port => self.port,
:encryption => (self.tls ? :simple_tls : nil)
}
options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
Net::LDAP.new options
end
def self.get_attr(entry, attr_name)
entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
end
end