diff --git a/lib/github/ldap/filter.rb b/lib/github/ldap/filter.rb index f11934d..a238642 100644 --- a/lib/github/ldap/filter.rb +++ b/lib/github/ldap/filter.rb @@ -20,26 +20,31 @@ module GitHub # Filter to check group membership. # - # entry: finds groups this Net::LDAP::Entry is a member of (optional) - # uid_attr: specifies the memberUid attribute to match with (optional) + # entry: finds groups this Net::LDAP::Entry is a member of (optional) # # Returns a Net::LDAP::Filter. - def member_filter(entry = nil, uid_attr = @ldap.uid) + def member_filter(entry = nil) if entry - filter = - MEMBERSHIP_NAMES. map {|n| Net::LDAP::Filter.eq(n, entry.dn) }. - reduce(:|) - - if !entry[uid_attr].empty? - filter |= - entry[uid_attr].map { |uid| Net::LDAP::Filter.eq("memberUid", uid) }. - reduce(:|) - end - - filter + MEMBERSHIP_NAMES. + map {|n| Net::LDAP::Filter.eq(n, entry.dn) }.reduce(:|) else - (MEMBERSHIP_NAMES + %w(memberUid)). - map {|n| Net::LDAP::Filter.pres(n)}.reduce(:|) + MEMBERSHIP_NAMES. + map {|n| Net::LDAP::Filter.pres(n) }. reduce(:|) + end + end + + # Filter to check group membership for posixGroups. + # + # Used by Domain#membership when posix_support_enabled? is true. + # + # entry: finds groups this Net::LDAP::Entry is a member of + # uid_attr: specifies the memberUid attribute to match with + # + # Returns a Net::LDAP::Filter or nil if no entry has no UID set. + def posix_member_filter(entry, uid_attr) + if !entry[uid_attr].empty? + entry[uid_attr].map { |uid| Net::LDAP::Filter.eq("memberUid", uid) }. + reduce(:|) end end diff --git a/test/filter_test.rb b/test/filter_test.rb index 0fff2ce..8fc6ba2 100644 --- a/test/filter_test.rb +++ b/test/filter_test.rb @@ -24,18 +24,22 @@ class FilterTest < Minitest::Test end def test_member_present - assert_equal "(|(|(member=*)(uniqueMember=*))(memberUid=*))", @subject.member_filter.to_s + assert_equal "(|(member=*)(uniqueMember=*))", @subject.member_filter.to_s end def test_member_equal - assert_equal "(|(|(member=#{@me})(uniqueMember=#{@me}))(memberUid=#{@uid}))", + assert_equal "(|(member=#{@me})(uniqueMember=#{@me}))", @subject.member_filter(@entry).to_s end - def test_member_without_uid + def test_posix_member_without_uid @entry.uid = nil - assert_equal "(|(member=#{@me})(uniqueMember=#{@me}))", - @subject.member_filter(@entry).to_s + assert_nil @subject.posix_member_filter(@entry, @ldap.uid) + end + + def test_posix_member_equal + assert_equal "(memberUid=#{@uid})", + @subject.posix_member_filter(@entry, @ldap.uid).to_s end def test_groups_reduced