annotate .svn/pristine/94/9430aa85788ebdba5f87faaba28140f73390ecbb.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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