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,7 +3,17 @@ module Gitlab | ||
3 | class Adapter | 3 | class Adapter |
4 | attr_reader :ldap | 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 | encryption = config['method'].to_s == 'ssl' ? :simple_tls : nil | 17 | encryption = config['method'].to_s == 'ssl' ? :simple_tls : nil |
8 | 18 | ||
9 | options = { | 19 | options = { |
@@ -23,8 +33,12 @@ module Gitlab | @@ -23,8 +33,12 @@ module Gitlab | ||
23 | if config['password'] || config['bind_dn'] | 33 | if config['password'] || config['bind_dn'] |
24 | options.merge!(auth_options) | 34 | options.merge!(auth_options) |
25 | end | 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 | end | 42 | end |
29 | 43 | ||
30 | def users(field, value) | 44 | def users(field, value) |
@@ -65,7 +79,7 @@ module Gitlab | @@ -65,7 +79,7 @@ module Gitlab | ||
65 | private | 79 | private |
66 | 80 | ||
67 | def config | 81 | def config |
68 | - @config ||= Gitlab.config.ldap | 82 | + @config ||= self.class.config |
69 | end | 83 | end |
70 | end | 84 | end |
71 | end | 85 | end |