From 60741b3e1c025f50e93e6f5847716ed930b319a0 2012-03-01 16:26:10 From: Jean-Philippe Lang Date: 2012-03-01 16:26:10 Subject: [PATCH] Adds an optional LDAP filter (#1060). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9044 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/auth_source_ldap.rb b/app/models/auth_source_ldap.rb index 5c6d28c..bf8d704 100644 --- a/app/models/auth_source_ldap.rb +++ b/app/models/auth_source_ldap.rb @@ -21,9 +21,10 @@ require 'net/ldap' class AuthSourceLdap < AuthSource validates_presence_of :host, :port, :attr_login validates_length_of :name, :host, :maximum => 60, :allow_nil => true - validates_length_of :account, :account_password, :base_dn, :maximum => 255, :allow_nil => true + validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true validates_numericality_of :port, :only_integer => true + validate :validate_filter before_validation :strip_ldap_attributes @@ -58,6 +59,20 @@ class AuthSourceLdap < AuthSource private + def ldap_filter + if filter.present? + Net::LDAP::Filter.construct(filter) + end + rescue Net::LDAP::LdapError + nil + end + + def validate_filter + if filter.present? && ldap_filter.nil? + errors.add(:filter, :invalid) + end + end + def strip_ldap_attributes [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? @@ -107,8 +122,13 @@ class AuthSourceLdap < AuthSource object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) attrs = {} + search_filter = object_filter & login_filter + if f = ldap_filter + search_filter = search_filter & f + end + ldap_con.search( :base => self.base_dn, - :filter => object_filter & login_filter, + :filter => search_filter, :attributes=> search_attributes) do |entry| if onthefly_register? diff --git a/app/views/ldap_auth_sources/_form.html.erb b/app/views/ldap_auth_sources/_form.html.erb index 9ffffaf..3ddf43a 100644 --- a/app/views/ldap_auth_sources/_form.html.erb +++ b/app/views/ldap_auth_sources/_form.html.erb @@ -23,6 +23,9 @@

<%= text_field 'auth_source', 'base_dn', :size => 60 %>

+

+<%= text_field 'auth_source', 'filter', :size => 60 %>

+

<%= check_box 'auth_source', 'onthefly_register' %>

diff --git a/config/locales/ar.yml b/config/locales/ar.yml index 2e262f8..4234d84 100644 --- a/config/locales/ar.yml +++ b/config/locales/ar.yml @@ -1023,3 +1023,4 @@ ar: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/bg.yml b/config/locales/bg.yml index 6ce7b73..53c3cca 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -1021,3 +1021,4 @@ bg: description_date_range_interval: Изберете диапазон чрез задаване на начална и крайна дати description_date_from: Въведете начална дата description_date_to: Въведете крайна дата + field_ldap_filter: LDAP filter diff --git a/config/locales/bs.yml b/config/locales/bs.yml index 147b687..0854340 100644 --- a/config/locales/bs.yml +++ b/config/locales/bs.yml @@ -1037,3 +1037,4 @@ bs: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/ca.yml b/config/locales/ca.yml index 86e997c..c0d554a 100644 --- a/config/locales/ca.yml +++ b/config/locales/ca.yml @@ -1025,3 +1025,4 @@ ca: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/cs.yml b/config/locales/cs.yml index f2e08f4..e5c1a51 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -1026,3 +1026,4 @@ cs: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/da.yml b/config/locales/da.yml index 3865749..9cc3a3e 100644 --- a/config/locales/da.yml +++ b/config/locales/da.yml @@ -1040,3 +1040,4 @@ da: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/de.yml b/config/locales/de.yml index aef5457..9dc0296 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1043,3 +1043,4 @@ de: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/el.yml b/config/locales/el.yml index 50537d5..92d8e8c 100644 --- a/config/locales/el.yml +++ b/config/locales/el.yml @@ -1023,3 +1023,4 @@ el: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index 2cbd770..2136b93 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -1025,3 +1025,4 @@ en-GB: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/en.yml b/config/locales/en.yml index 2ca8582..21e12ba 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -321,6 +321,7 @@ en: field_cvs_module: Module field_repository_is_default: Main repository field_multiple: Multiple values + field_ldap_filter: LDAP filter setting_app_title: Application title setting_app_subtitle: Application subtitle diff --git a/config/locales/es.yml b/config/locales/es.yml index 19531ca..a30ffd8 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -1060,3 +1060,4 @@ es: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/et.yml b/config/locales/et.yml index 9ffe12e..d77d9c0 100644 --- a/config/locales/et.yml +++ b/config/locales/et.yml @@ -1039,3 +1039,4 @@ et: label_export_options: "%{export_format} ekspordi valikud" label_completed_versions: "Lõpetatud versioonid" error_attachment_too_big: "Seda faili ei saa üles laadida, kuna ületab maksimumsuurust (%{max_size})" + field_ldap_filter: LDAP filter diff --git a/config/locales/eu.yml b/config/locales/eu.yml index a85e7cd..8a02f2f 100644 --- a/config/locales/eu.yml +++ b/config/locales/eu.yml @@ -1026,3 +1026,4 @@ eu: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/fa.yml b/config/locales/fa.yml index bd10913..efac7a1 100644 --- a/config/locales/fa.yml +++ b/config/locales/fa.yml @@ -1025,3 +1025,4 @@ fa: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/fi.yml b/config/locales/fi.yml index 2da4687..afafc51 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -1044,3 +1044,4 @@ fi: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 2a149a0..8fd18c3 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -320,6 +320,7 @@ fr: field_commit_logs_encoding: Encodage des messages de commit field_repository_is_default: Dépôt principal field_multiple: Valeurs multiples + field_ldap_filter: Filtre LDAP setting_app_title: Titre de l'application setting_app_subtitle: Sous-titre de l'application diff --git a/config/locales/gl.yml b/config/locales/gl.yml index 17b583a..8e8d30e 100644 --- a/config/locales/gl.yml +++ b/config/locales/gl.yml @@ -1034,3 +1034,4 @@ gl: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/he.yml b/config/locales/he.yml index 0a56713..af31a05 100644 --- a/config/locales/he.yml +++ b/config/locales/he.yml @@ -1028,3 +1028,4 @@ he: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/hr.yml b/config/locales/hr.yml index 462615d..3ce6f30 100644 --- a/config/locales/hr.yml +++ b/config/locales/hr.yml @@ -1029,3 +1029,4 @@ hr: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/hu.yml b/config/locales/hu.yml index 363575d..b4cb98b 100644 --- a/config/locales/hu.yml +++ b/config/locales/hu.yml @@ -1042,3 +1042,4 @@ notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/id.yml b/config/locales/id.yml index e6ab8d1..6dd2c81 100644 --- a/config/locales/id.yml +++ b/config/locales/id.yml @@ -1029,3 +1029,4 @@ id: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/it.yml b/config/locales/it.yml index d415e66..ae45142 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -1024,3 +1024,4 @@ it: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/ja.yml b/config/locales/ja.yml index f6635cb..1d7ebe2 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -1053,3 +1053,4 @@ ja: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/ko.yml b/config/locales/ko.yml index 44ac0f3..7400962 100644 --- a/config/locales/ko.yml +++ b/config/locales/ko.yml @@ -1073,3 +1073,4 @@ ko: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/lt.yml b/config/locales/lt.yml index 921e437..ec76878 100644 --- a/config/locales/lt.yml +++ b/config/locales/lt.yml @@ -1083,3 +1083,4 @@ lt: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/lv.yml b/config/locales/lv.yml index 05a1cc7..cef9174 100644 --- a/config/locales/lv.yml +++ b/config/locales/lv.yml @@ -1017,3 +1017,4 @@ lv: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/mk.yml b/config/locales/mk.yml index 6f2fb2a..ea32c97 100644 --- a/config/locales/mk.yml +++ b/config/locales/mk.yml @@ -1023,3 +1023,4 @@ mk: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/mn.yml b/config/locales/mn.yml index c4f3ca7..8ce7c21 100644 --- a/config/locales/mn.yml +++ b/config/locales/mn.yml @@ -1023,3 +1023,4 @@ mn: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 4db1f74..00bcf33 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -1005,3 +1005,4 @@ nl: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/no.yml b/config/locales/no.yml index 18689cb..027d66b 100644 --- a/config/locales/no.yml +++ b/config/locales/no.yml @@ -1013,3 +1013,4 @@ notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/pl.yml b/config/locales/pl.yml index 4aa4181..c7a4b23 100644 --- a/config/locales/pl.yml +++ b/config/locales/pl.yml @@ -1040,3 +1040,4 @@ pl: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index b26d133..8752072 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -1046,3 +1046,4 @@ pt-BR: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/pt.yml b/config/locales/pt.yml index ecbca94..775fe55 100644 --- a/config/locales/pt.yml +++ b/config/locales/pt.yml @@ -1028,3 +1028,4 @@ pt: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/ro.yml b/config/locales/ro.yml index 6b243c6..2ac3066 100644 --- a/config/locales/ro.yml +++ b/config/locales/ro.yml @@ -1020,3 +1020,4 @@ ro: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/ru.yml b/config/locales/ru.yml index f927abc..a4bbdc4 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -1138,3 +1138,4 @@ ru: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/sk.yml b/config/locales/sk.yml index d8c71c6..1cfe719 100644 --- a/config/locales/sk.yml +++ b/config/locales/sk.yml @@ -1023,3 +1023,4 @@ sk: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/sl.yml b/config/locales/sl.yml index 0016ace..0fc5785 100644 --- a/config/locales/sl.yml +++ b/config/locales/sl.yml @@ -1023,3 +1023,4 @@ sl: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/sr-YU.yml b/config/locales/sr-YU.yml index a98aaed..ea4e68c 100644 --- a/config/locales/sr-YU.yml +++ b/config/locales/sr-YU.yml @@ -1023,3 +1023,4 @@ sr-YU: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/sr.yml b/config/locales/sr.yml index 7c5b0f2..2c8d3bc 100644 --- a/config/locales/sr.yml +++ b/config/locales/sr.yml @@ -1024,3 +1024,4 @@ sr: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/sv.yml b/config/locales/sv.yml index e4a9dd8..c39e482 100644 --- a/config/locales/sv.yml +++ b/config/locales/sv.yml @@ -1064,3 +1064,4 @@ sv: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/th.yml b/config/locales/th.yml index 26e468c..905a57f 100644 --- a/config/locales/th.yml +++ b/config/locales/th.yml @@ -1020,3 +1020,4 @@ th: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/tr.yml b/config/locales/tr.yml index d73663a..383f1fd 100644 --- a/config/locales/tr.yml +++ b/config/locales/tr.yml @@ -1042,3 +1042,4 @@ tr: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/uk.yml b/config/locales/uk.yml index 86d1d9e..6c16c71 100644 --- a/config/locales/uk.yml +++ b/config/locales/uk.yml @@ -1020,3 +1020,4 @@ uk: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/vi.yml b/config/locales/vi.yml index 0d2068b..9419ef2 100644 --- a/config/locales/vi.yml +++ b/config/locales/vi.yml @@ -1074,3 +1074,4 @@ vi: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/config/locales/zh-TW.yml b/config/locales/zh-TW.yml index 8c373ad..7b67bf2 100644 --- a/config/locales/zh-TW.yml +++ b/config/locales/zh-TW.yml @@ -1103,3 +1103,4 @@ zero: 0 問題 one: 1 問題 other: "%{count} 問題清單" + field_ldap_filter: LDAP filter diff --git a/config/locales/zh.yml b/config/locales/zh.yml index 9da84db..7be3096 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -1025,3 +1025,4 @@ zh: notice_issue_update_conflict: The issue has been updated by an other user while you were editing it. text_issue_conflict_resolution_cancel: Discard all my changes and redisplay %{link} permission_manage_related_issues: Manage related issues + field_ldap_filter: LDAP filter diff --git a/db/migrate/20120301153455_add_auth_sources_filter.rb b/db/migrate/20120301153455_add_auth_sources_filter.rb new file mode 100644 index 0000000..617b3b7 --- /dev/null +++ b/db/migrate/20120301153455_add_auth_sources_filter.rb @@ -0,0 +1,9 @@ +class AddAuthSourcesFilter < ActiveRecord::Migration + def self.up + add_column :auth_sources, :filter, :string + end + + def self.down + remove_column :auth_sources, :filter + end +end diff --git a/test/unit/auth_source_ldap_test.rb b/test/unit/auth_source_ldap_test.rb index 5dc532f..55e1479 100644 --- a/test/unit/auth_source_ldap_test.rb +++ b/test/unit/auth_source_ldap_test.rb @@ -18,6 +18,7 @@ require File.expand_path('../../test_helper', __FILE__) class AuthSourceLdapTest < ActiveSupport::TestCase + include Redmine::I18n fixtures :auth_sources def setup @@ -44,6 +45,18 @@ class AuthSourceLdapTest < ActiveSupport::TestCase assert_equal 389, a.port end + def test_filter_should_be_validated + set_language_if_valid 'en' + + a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :attr_login => 'sn') + a.filter = "(mail=*@redmine.org" + assert !a.valid? + assert_equal "is invalid", a.errors[:filter].to_s + + a.filter = "(mail=*@redmine.org)" + assert a.valid? + end + if ldap_configured? context '#authenticate' do setup do @@ -83,6 +96,23 @@ class AuthSourceLdapTest < ActiveSupport::TestCase end end + context 'without filter' do + should 'return any user' do + assert @auth.authenticate('example1','123456') + assert @auth.authenticate('edavis', '123456') + end + end + + context 'with filter' do + setup do + @auth.filter = "(mail=*@redmine.org)" + end + + should 'return user who matches the filter only' do + assert @auth.authenticate('example1','123456') + assert_nil @auth.authenticate('edavis', '123456') + end + end end else puts '(Test LDAP server not configured)'