Makes AuthSource.authenticate return a hash instead of an array.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3492 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2010-02-26 09:13:12 +00:00
parent f1d16bc007
commit d6f9e576e8
4 changed files with 12 additions and 14 deletions

View File

@ -35,9 +35,9 @@ class AuthSourceLdap < AuthSource
return nil if login.blank? || password.blank? return nil if login.blank? || password.blank?
attrs = get_user_dn(login) attrs = get_user_dn(login)
if attrs.first && attrs.first[:dn] && authenticate_dn(attrs.first[:dn], password) if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password)
logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
return [] << attrs.first.except(:dn) return attrs.except(:dn)
end end
rescue Net::LDAP::LdapError => text rescue Net::LDAP::LdapError => text
raise "LdapError: " + text raise "LdapError: " + text
@ -73,13 +73,13 @@ class AuthSourceLdap < AuthSource
end end
def get_user_attributes_from_ldap_entry(entry) def get_user_attributes_from_ldap_entry(entry)
[ {
:dn => entry.dn, :dn => entry.dn,
:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
:lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
:mail => AuthSourceLdap.get_attr(entry, self.attr_mail), :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
:auth_source_id => self.id :auth_source_id => self.id
] }
end end
# Return the attributes needed for the LDAP search. It will only # Return the attributes needed for the LDAP search. It will only
@ -104,7 +104,7 @@ class AuthSourceLdap < AuthSource
ldap_con = initialize_ldap_con(self.account, self.account_password) ldap_con = initialize_ldap_con(self.account, self.account_password)
login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
attrs = [] attrs = {}
ldap_con.search( :base => self.base_dn, ldap_con.search( :base => self.base_dn,
:filter => object_filter & login_filter, :filter => object_filter & login_filter,
@ -113,10 +113,10 @@ class AuthSourceLdap < AuthSource
if onthefly_register? if onthefly_register?
attrs = get_user_attributes_from_ldap_entry(entry) attrs = get_user_attributes_from_ldap_entry(entry)
else else
attrs = [:dn => entry.dn] attrs = {:dn => entry.dn}
end end
logger.debug "DN found for #{login}: #{attrs.first[:dn]}" if logger && logger.debug? logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug?
end end
attrs attrs

View File

@ -111,7 +111,7 @@ class User < Principal
# user is not yet registered, try to authenticate with available sources # user is not yet registered, try to authenticate with available sources
attrs = AuthSource.authenticate(login, password) attrs = AuthSource.authenticate(login, password)
if attrs if attrs
user = new(*attrs) user = new(attrs)
user.login = login user.login = login
user.language = Setting.default_language user.language = Setting.default_language
if user.save if user.save

View File

@ -149,7 +149,7 @@ class AccountTest < ActionController::IntegrationTest
def test_onthefly_registration def test_onthefly_registration
# disable registration # disable registration
Setting.self_registration = '0' Setting.self_registration = '0'
AuthSource.expects(:authenticate).returns([:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66]) AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
post 'account/login', :username => 'foo', :password => 'bar' post 'account/login', :username => 'foo', :password => 'bar'
assert_redirected_to 'my/page' assert_redirected_to 'my/page'
@ -163,7 +163,7 @@ class AccountTest < ActionController::IntegrationTest
def test_onthefly_registration_with_invalid_attributes def test_onthefly_registration_with_invalid_attributes
# disable registration # disable registration
Setting.self_registration = '0' Setting.self_registration = '0'
AuthSource.expects(:authenticate).returns([:login => 'foo', :lastname => 'Smith', :auth_source_id => 66]) AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
post 'account/login', :username => 'foo', :password => 'bar' post 'account/login', :username => 'foo', :password => 'bar'
assert_response :success assert_response :success

View File

@ -43,10 +43,8 @@ class AuthSourceLdapTest < ActiveSupport::TestCase
context 'with a valid LDAP user' do context 'with a valid LDAP user' do
should 'return the user attributes' do should 'return the user attributes' do
response = @auth.authenticate('example1','123456') attributes = @auth.authenticate('example1','123456')
assert response.is_a?(Array), "An array was not returned" assert attributes.is_a?(Hash), "An hash was not returned"
assert response.first.present?, "No user data returned"
attributes = response.first
assert_equal 'Example', attributes[:firstname] assert_equal 'Example', attributes[:firstname]
assert_equal 'One', attributes[:lastname] assert_equal 'One', attributes[:lastname]
assert_equal 'example1@redmine.org', attributes[:mail] assert_equal 'example1@redmine.org', attributes[:mail]