Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing find_by_dn issue returning multiple results #62

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
5 changes: 1 addition & 4 deletions lib/ldap_fluff/generic_member_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ def find_user(uid)
end

def find_by_dn(dn)
entry, base = dn.split(/(?<!\\),/, 2)
entry_attr, entry_value = entry.split('=', 2)
entry_value = entry_value.gsub('\,', ',')
user = @ldap.search(:filter => name_filter(entry_value, entry_attr), :base => base)
user = @ldap.search(:base => dn, :scope => Net::LDAP::SearchScope_BaseObject)
raise self.class::UIDNotFoundException if (user.nil? || user.empty?)
user
end
Expand Down
9 changes: 6 additions & 3 deletions test/ad_member_services_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_find_missing_group
end

def test_find_by_dn
@ldap.expect(:search, [:result], [:filter => Net::LDAP::Filter.eq('cn', 'Foo Bar'), :base => 'dc=example,dc=com'])
@ldap.expect(:search, [:result], [:base => 'cn=Foo Bar,dc=example,dc=com', :scope => Net::LDAP::SearchScope_BaseObject])
@adms.ldap = @ldap
assert_equal([:result], @adms.find_by_dn('cn=Foo Bar,dc=example,dc=com'))
@ldap.verify
Expand All @@ -135,14 +135,17 @@ def test_find_by_dn_comma_in_cn
# returned by the server in answer to a group membership query with
# backslashes before the commas in the CNs. Such escaped commas should not
# be used when splitting the DN.
@ldap.expect(:search, [:result], [:filter => Net::LDAP::Filter.eq('cn', 'Bar, Foo'), :base => 'dc=example,dc=com'])
#
# Is this still required? DN won't be split anymore.
#
@ldap.expect(:search, [:result], [:base => 'cn=Bar\, Foo,dc=example,dc=com', :scope => Net::LDAP::SearchScope_BaseObject])
@adms.ldap = @ldap
assert_equal([:result], @adms.find_by_dn('cn=Bar\, Foo,dc=example,dc=com'))
@ldap.verify
end

def test_find_by_dn_missing_entry
@ldap.expect(:search, nil, [:filter => Net::LDAP::Filter.eq('cn', 'Foo Bar'), :base => 'dc=example,dc=com'])
@ldap.expect(:search, nil, [:base => 'cn=Foo Bar,dc=example,dc=com', :scope => Net::LDAP::SearchScope_BaseObject])
@adms.ldap = @ldap
assert_raises(LdapFluff::ActiveDirectory::MemberService::UIDNotFoundException) do
@adms.find_by_dn('cn=Foo Bar,dc=example,dc=com')
Expand Down