Commit 1e9b2733 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Add LDAP::Group and LDAP::Person classes. Create LDAP module. Added method for selecting ldap users

parent 51d5364a
#-------------------------------------------------------------------
#
# The GitLab Enterprise Edition (EE) license
#
# Copyright (c) 2013 GitLab.com
#
# All Rights Reserved. No part of this software may be reproduced without
# prior permission of GitLab.com. By using this software you agree to be
# bound by the GitLab Enterprise Support Subscription Terms.
#
#-------------------------------------------------------------------
module Gitlab
module LDAP
class Adapter
attr_reader :ldap
def initialize
options = {
host: config['host'],
port: config['port'],
}
auth_options = {
auth: {
method: config['method'],
username: config['bind_dn'],
password: config['password']
}
}
if config['password'] || config['bind_dn']
options.merge!(auth_options)
end
@ldap = Net::LDAP.new(options)
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: config['group_base'],
filter: Net::LDAP::Filter.eq("cn", cn)
}
ldap.search(options).map do |entry|
Gitlab::LDAP::Group.new(entry)
end
end
def users(cn = "*")
options = {
base: config['base'],
filter: Net::LDAP::Filter.eq("cn", cn)
}
entries = ldap.search(options).select do |entry|
entry.respond_to? :uid
end
entries.map do |entry|
Gitlab::LDAP::Person.new(entry)
end
end
private
def config
@config ||= Gitlab.config.ldap
end
end
end
end
......@@ -11,50 +11,37 @@
#-------------------------------------------------------------------
module Gitlab
class LDAP
attr_reader :ldap
def initialize
options = {
host: config['host'],
port: config['port'],
}
auth_options = {
auth: {
method: config['method'],
username: config['bind_dn'],
password: config['password']
}
}
if config['password'] || config['bind_dn']
options.merge!(auth_options)
module LDAP
class Group
def initialize(entry)
@entry = entry
end
@ldap = Net::LDAP.new(options)
end
def name
entry.cn.join(" ")
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: config['group_base'],
filter: Net::LDAP::Filter.eq("cn", cn)
}
def path
name.parameterize
end
ldap.search(options)
end
def members
if entry.respond_to? :member
entry.meber
elsif entry.respond_to? :uniquemember
entry.uniquemember
elsif entry.respond_to? :memberof
entry.memberof
else
raise 'Unsupported member attribute'
end
end
private
private
def config
@config ||= Gitlab.config.ldap
def entry
@entry
end
end
end
end
#-------------------------------------------------------------------
#
# The GitLab Enterprise Edition (EE) license
#
# Copyright (c) 2013 GitLab.com
#
# All Rights Reserved. No part of this software may be reproduced without
# prior permission of GitLab.com. By using this software you agree to be
# bound by the GitLab Enterprise Support Subscription Terms.
#
#-------------------------------------------------------------------
module Gitlab
module LDAP
class Person
def initialize(entry)
@entry = entry
end
def name
entry.cn.join(" ")
end
def username
entry.uid.join(" ")
end
private
def entry
@entry
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment