|
| 1 | +require 'net/ldap' |
| 2 | +require 'forwardable' |
| 3 | + |
| 4 | +require 'github/ldap/filter' |
| 5 | +require 'github/ldap/domain' |
| 6 | +require 'github/ldap/group' |
| 7 | +require 'github/ldap/posix_group' |
| 8 | +require 'github/ldap/virtual_group' |
| 9 | +require 'github/ldap/virtual_attributes' |
| 10 | +require 'github/ldap/instrumentation' |
| 11 | +require 'github/ldap/member_search' |
| 12 | +require 'github/ldap/membership_validators' |
| 13 | + |
1 | 14 | module GitHub |
2 | 15 | class Ldap |
3 | | - require 'net/ldap' |
4 | | - require 'forwardable' |
5 | | - require 'github/ldap/filter' |
6 | | - require 'github/ldap/domain' |
7 | | - require 'github/ldap/group' |
8 | | - require 'github/ldap/posix_group' |
9 | | - require 'github/ldap/virtual_group' |
10 | | - require 'github/ldap/virtual_attributes' |
11 | | - require 'github/ldap/instrumentation' |
12 | | - require 'github/ldap/capabilities' |
13 | | - require 'github/ldap/member_search' |
14 | | - require 'github/ldap/membership_validators' |
15 | | - |
16 | 16 | include Instrumentation |
17 | 17 |
|
18 | 18 | extend Forwardable |
19 | 19 |
|
| 20 | + # Internal: The capability required to use ActiveDirectory features. |
| 21 | + # See: http://msdn.microsoft.com/en-us/library/cc223359.aspx. |
| 22 | + ACTIVE_DIRECTORY_V61_R2_OID = "1.2.840.113556.1.4.2080".freeze |
| 23 | + |
20 | 24 | # Utility method to get the last operation result with a human friendly message. |
21 | 25 | # |
22 | 26 | # Returns an OpenStruct with `code` and `message`. |
@@ -91,11 +95,8 @@ def initialize(options = {}) |
91 | 95 | # when a base is not explicitly provided. |
92 | 96 | @search_domains = Array(options[:search_domains]) |
93 | 97 |
|
94 | | - # configure which strategy should be used to validate user membership |
95 | | - configure_membership_validation_strategy(options[:membership_validator]) |
96 | | - |
97 | | - # configure which strategy should be used for member search |
98 | | - configure_member_search_strategy(options[:member_search_strategy]) |
| 98 | + # configure both the membership validator and the member search strategies |
| 99 | + configure_search_strategy(options[:search_strategy]) |
99 | 100 |
|
100 | 101 | # enables instrumenting queries |
101 | 102 | @instrumentation_service = options[:instrumentation_service] |
@@ -242,42 +243,78 @@ def configure_virtual_attributes(attributes) |
242 | 243 | end |
243 | 244 | end |
244 | 245 |
|
245 | | - # Internal: Configure the membership validation strategy. |
| 246 | + # Internal: Configure the member search and membership validation strategies. |
246 | 247 | # |
247 | | - # Used by GitHub::Ldap::MembershipValidators::Detect to force a specific |
248 | | - # strategy (instead of detecting host capabilities and deciding at runtime). |
| 248 | + # TODO: Inline the logic in these two methods here. |
| 249 | + # |
| 250 | + # Returns nothing. |
| 251 | + def configure_search_strategy(strategy = nil) |
| 252 | + # configure which strategy should be used to validate user membership |
| 253 | + configure_membership_validation_strategy(strategy) |
| 254 | + |
| 255 | + # configure which strategy should be used for member search |
| 256 | + configure_member_search_strategy(strategy) |
| 257 | + end |
| 258 | + |
| 259 | + # Internal: Configure the membership validation strategy. |
249 | 260 | # |
250 | | - # If `strategy` is not provided, or doesn't match a known strategy, |
251 | | - # defaults to `:detect`. Otherwise the configured strategy is selected. |
| 261 | + # If no known strategy is provided, detects ActiveDirectory capabilities or |
| 262 | + # falls back to the Recursive strategy by default. |
252 | 263 | # |
253 | | - # Returns the selected membership validator strategy Symbol. |
| 264 | + # Returns the membership validator strategy Class. |
254 | 265 | def configure_membership_validation_strategy(strategy = nil) |
255 | 266 | @membership_validator = |
256 | 267 | case strategy.to_s |
257 | | - when "classic", "recursive", "active_directory" |
258 | | - strategy.to_sym |
| 268 | + when "classic" |
| 269 | + GitHub::Ldap::MembershipValidators::Classic |
| 270 | + when "recursive" |
| 271 | + GitHub::Ldap::MembershipValidators::Recursive |
| 272 | + when "active_directory" |
| 273 | + GitHub::Ldap::MembershipValidators::ActiveDirectory |
259 | 274 | else |
260 | | - :detect |
| 275 | + # fallback to detection, defaulting to recursive strategy |
| 276 | + if active_directory_capability? |
| 277 | + GitHub::Ldap::MembershipValidators::ActiveDirectory |
| 278 | + else |
| 279 | + GitHub::Ldap::MembershipValidators::Recursive |
| 280 | + end |
261 | 281 | end |
262 | 282 | end |
263 | 283 |
|
264 | 284 | # Internal: Configure the member search strategy. |
265 | 285 | # |
266 | | - # Used by GitHub::Ldap::MemberSearch::Detect to force a specific strategy |
267 | | - # (instead of detecting the host capabilities and deciding at runtime). |
268 | 286 | # |
269 | | - # If `strategy` is not provided, or doesn't match a known strategy, |
270 | | - # defaults to `:detect`. Otherwise the configured strategy is selected. |
| 287 | + # If no known strategy is provided, detects ActiveDirectory capabilities or |
| 288 | + # falls back to the Recursive strategy by default. |
271 | 289 | # |
272 | | - # Returns the selected strategy Symbol. |
| 290 | + # Returns the selected strategy Class. |
273 | 291 | def configure_member_search_strategy(strategy = nil) |
274 | 292 | @member_search_strategy = |
275 | | - case strategy.to_s |
276 | | - when "classic", "recursive" |
277 | | - strategy.to_sym |
278 | | - else |
279 | | - :detect |
280 | | - end |
| 293 | + case strategy.to_s |
| 294 | + when "classic" |
| 295 | + GitHub::Ldap::MemberSearch::Classic |
| 296 | + when "recursive" |
| 297 | + GitHub::Ldap::MemberSearch::Recursive |
| 298 | + when "active_directory" |
| 299 | + GitHub::Ldap::MemberSearch::ActiveDirectory |
| 300 | + else |
| 301 | + # fallback to detection, defaulting to recursive strategy |
| 302 | + if active_directory_capability? |
| 303 | + GitHub::Ldap::MemberSearch::ActiveDirectory |
| 304 | + else |
| 305 | + GitHub::Ldap::MemberSearch::Recursive |
| 306 | + end |
| 307 | + end |
| 308 | + end |
| 309 | + |
| 310 | + # Internal: Detect whether the LDAP host is an ActiveDirectory server. |
| 311 | + # |
| 312 | + # See: http://msdn.microsoft.com/en-us/library/cc223359.aspx. |
| 313 | + # |
| 314 | + # Returns true if the host is an ActiveDirectory server, false otherwise. |
| 315 | + def active_directory_capability? |
| 316 | + capabilities[:supportedcapabilities].include?(ACTIVE_DIRECTORY_V61_R2_OID) |
281 | 317 | end |
| 318 | + private :active_directory_capability? |
282 | 319 | end |
283 | 320 | end |
0 commit comments