|
| 1 | +module GitHub |
| 2 | + class Ldap |
| 3 | + module MemberSearch |
| 4 | + # Look up group members using the ActiveDirectory "in chain" matching rule. |
| 5 | + # |
| 6 | + # The 1.2.840.113556.1.4.1941 matching rule (LDAP_MATCHING_RULE_IN_CHAIN) |
| 7 | + # "walks the chain of ancestry in objects all the way to the root until |
| 8 | + # it finds a match". |
| 9 | + # Source: http://msdn.microsoft.com/en-us/library/aa746475(v=vs.85).aspx |
| 10 | + # |
| 11 | + # This means we have an efficient method of searching for group members, |
| 12 | + # even in nested groups, performed on the server side. |
| 13 | + class ActiveDirectory < Base |
| 14 | + OID = "1.2.840.113556.1.4.1941" |
| 15 | + |
| 16 | + # Internal: The default attributes to query for. |
| 17 | + # NOTE: We technically don't need any by default, but if we left this |
| 18 | + # empty, we'd be querying for *all* attributes which is less ideal. |
| 19 | + DEFAULT_ATTRS = %w(objectClass) |
| 20 | + |
| 21 | + # Internal: The attributes to search for. |
| 22 | + attr_reader :attrs |
| 23 | + |
| 24 | + # Public: Instantiate new search strategy. |
| 25 | + # |
| 26 | + # - ldap: GitHub::Ldap object |
| 27 | + # - options: Hash of options |
| 28 | + # |
| 29 | + # NOTE: This overrides default behavior to configure attrs`. |
| 30 | + def initialize(ldap, options = {}) |
| 31 | + super |
| 32 | + @attrs = Array(options[:attrs]).concat DEFAULT_ATTRS |
| 33 | + end |
| 34 | + |
| 35 | + # Public: Performs search for group members, including groups and |
| 36 | + # members of subgroups, using ActiveDirectory's "in chain" matching |
| 37 | + # rule. |
| 38 | + # |
| 39 | + # Returns Array of Net::LDAP::Entry objects. |
| 40 | + def perform(group) |
| 41 | + filter = member_of_in_chain_filter(group) |
| 42 | + |
| 43 | + # search for all members of the group, including subgroups, by |
| 44 | + # searching "in chain". |
| 45 | + domains.each_with_object([]) do |domain, members| |
| 46 | + members.concat domain.search(filter: filter, attributes: attrs) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + # Internal: Constructs a member filter using the "in chain" |
| 51 | + # extended matching rule afforded by ActiveDirectory. |
| 52 | + # |
| 53 | + # Returns a Net::LDAP::Filter object. |
| 54 | + def member_of_in_chain_filter(entry) |
| 55 | + Net::LDAP::Filter.ex("memberOf:#{OID}", entry.dn) |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | +end |
0 commit comments