|
| 1 | +module GitHub |
| 2 | + class Ldap |
| 3 | + module MemberSearch |
| 4 | + # Detects the LDAP host's capabilities and determines the appropriate |
| 5 | + # member search strategy at runtime. |
| 6 | + # |
| 7 | + # Currently detects for ActiveDirectory in-chain membership validation. |
| 8 | + # |
| 9 | + # An explicit strategy can also be defined via |
| 10 | + # `GitHub::Ldap#member_search_strategy=`. |
| 11 | + # |
| 12 | + # See also `GitHub::Ldap#configure_member_search_strategy`. |
| 13 | + class Detect |
| 14 | + # Defines `active_directory_capability?` and necessary helpers. |
| 15 | + include GitHub::Ldap::Capabilities |
| 16 | + |
| 17 | + # Internal: The GitHub::Ldap object to search domains with. |
| 18 | + attr_reader :ldap |
| 19 | + |
| 20 | + # Internal: The Hash of options to pass through to the strategy. |
| 21 | + attr_reader :options |
| 22 | + |
| 23 | + # Public: Instantiate a meta strategy to detect the right strategy |
| 24 | + # to use for the search, and call that strategy, at runtime. |
| 25 | + # |
| 26 | + # - ldap: GitHub::Ldap object |
| 27 | + # - options: Hash of options (passed through) |
| 28 | + def initialize(ldap, options = {}) |
| 29 | + @ldap = ldap |
| 30 | + @options = options |
| 31 | + end |
| 32 | + |
| 33 | + # Public: Performs search for group members via the appropriate search |
| 34 | + # strategy detected/configured. |
| 35 | + # |
| 36 | + # Returns Array of Net::LDAP::Entry objects. |
| 37 | + def perform(entry) |
| 38 | + strategy.perform(entry) |
| 39 | + end |
| 40 | + |
| 41 | + # Internal: Returns the member search strategy object. |
| 42 | + def strategy |
| 43 | + @strategy ||= begin |
| 44 | + strategy = detect_strategy |
| 45 | + strategy.new(ldap, options) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + # Internal: Find the most appropriate search strategy, either by |
| 50 | + # configuration or by detecting the host's capabilities. |
| 51 | + # |
| 52 | + # Returns the strategy class. |
| 53 | + def detect_strategy |
| 54 | + case |
| 55 | + when GitHub::Ldap::MemberSearch::STRATEGIES.key?(strategy_config) |
| 56 | + GitHub::Ldap::MemberSearch::STRATEGIES[strategy_config] |
| 57 | + when active_directory_capability? |
| 58 | + GitHub::Ldap::MemberSearch::STRATEGIES[:active_directory] |
| 59 | + else |
| 60 | + GitHub::Ldap::MemberSearch::STRATEGIES[:recursive] |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + # Internal: Returns the configured member search strategy Symbol. |
| 65 | + def strategy_config |
| 66 | + ldap.member_search_strategy |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments