Commit af53aa9072ae355b6de167b0d20f9b87195131ec
1 parent
5a616649
Exists in
spb-stable
and in
3 other branches
Add Gitlab::LDAP::Adapter.open
This new method is based on Net::LDAP.open, which reuses a single LDAP connection.
Showing
1 changed file
with
17 additions
and
3 deletions
Show diff stats
lib/gitlab/ldap/adapter.rb
... | ... | @@ -3,7 +3,17 @@ module Gitlab |
3 | 3 | class Adapter |
4 | 4 | attr_reader :ldap |
5 | 5 | |
6 | - def initialize | |
6 | + def self.open(&block) | |
7 | + Net::LDAP.open(adapter_options) do |ldap| | |
8 | + block.call(self.new(ldap)) | |
9 | + end | |
10 | + end | |
11 | + | |
12 | + def self.config | |
13 | + Gitlab.config.ldap | |
14 | + end | |
15 | + | |
16 | + def self.adapter_options | |
7 | 17 | encryption = config['method'].to_s == 'ssl' ? :simple_tls : nil |
8 | 18 | |
9 | 19 | options = { |
... | ... | @@ -23,8 +33,12 @@ module Gitlab |
23 | 33 | if config['password'] || config['bind_dn'] |
24 | 34 | options.merge!(auth_options) |
25 | 35 | end |
36 | + options | |
37 | + end | |
38 | + | |
26 | 39 | |
27 | - @ldap = Net::LDAP.new(options) | |
40 | + def initialize(ldap=nil) | |
41 | + @ldap = ldap || Net::LDAP.new(self.class.adapter_options) | |
28 | 42 | end |
29 | 43 | |
30 | 44 | def users(field, value) |
... | ... | @@ -65,7 +79,7 @@ module Gitlab |
65 | 79 | private |
66 | 80 | |
67 | 81 | def config |
68 | - @config ||= Gitlab.config.ldap | |
82 | + @config ||= self.class.config | |
69 | 83 | end |
70 | 84 | end |
71 | 85 | end | ... | ... |