From f9f1649feb5db4bfc8a60adda823210547c4b909 Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Fri, 19 Jul 2013 14:41:49 +0300
Subject: [PATCH] Implement ldap group search functionality

---
 Gemfile            |  1 +
 Gemfile.lock       |  1 +
 lib/gitlab/ldap.rb | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+)
 create mode 100644 lib/gitlab/ldap.rb

diff --git a/Gemfile b/Gemfile
index e679f3ee1aa35..e39d673976709 100644
--- a/Gemfile
+++ b/Gemfile
@@ -30,6 +30,7 @@ gem 'gitlab-grack', '~> 1.0.1', require: 'grack'
 
 # LDAP Auth
 gem 'gitlab_omniauth-ldap', '1.0.3', require: "omniauth-ldap"
+gem 'net-ldap'
 
 # Syntax highlighter
 gem "gitlab-pygments.rb", '~> 0.3.2', require: 'pygments.rb'
diff --git a/Gemfile.lock b/Gemfile.lock
index 6eedadc74c6c2..da40c69fc2f13 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -591,6 +591,7 @@ DEPENDENCIES
   minitest (~> 4.7.0)
   modernizr (= 2.6.2)
   mysql2
+  net-ldap
   omniauth (~> 1.1.3)
   omniauth-github
   omniauth-google-oauth2
diff --git a/lib/gitlab/ldap.rb b/lib/gitlab/ldap.rb
new file mode 100644
index 0000000000000..bfc0465c64cd9
--- /dev/null
+++ b/lib/gitlab/ldap.rb
@@ -0,0 +1,39 @@
+module Gitlab
+  class LDAP
+    attr_reader :ldap
+
+    def initialize
+      @ldap = Net::LDAP.new(
+        host: config['host'],
+        port: config['port'],
+        auth: {
+          method: config['method'],
+          username: config['bind_dn'],
+          password: config['password']
+        }
+      )
+    end
+
+    # Get LDAP groups from ou=Groups
+    #
+    # cn - filter groups by name
+    #
+    # Ex.
+    #   groups("dev*") # return all groups start with 'dev'
+    #
+    def groups(cn = "*")
+      options = {
+        base: "ou=Groups,#{config['base']}",
+        filter: Net::LDAP::Filter.eq("cn", cn)
+      }
+
+      ldap.search(options)
+    end
+
+    private
+
+    def config
+      @config ||= Gitlab.config.ldap
+    end
+  end
+end
-- 
GitLab