Class: Acs::Ldap::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/acs/ldap/connector.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connector

Returns a new instance of Connector.



3
4
5
6
7
8
9
10
11
12
# File 'lib/acs/ldap/connector.rb', line 3

def initialize(options = {})
  @host     = options[:host] || '127.0.0.1'
  @port     = options[:port] || 389
  @base     = options[:base] || nil
  @dn       = options[:dn] || nil
  @password = options[:password] || nil
  @tls      = options[:tls] || false

  @connected = false
end

Instance Method Details

#add(dn, attributes) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/acs/ldap/connector.rb', line 61

def add(dn, attributes)
  #debugger
  logger.info "Add dn '#{dn}'  attributes '#{attributes.inspect}'"
  get_connection.add(dn: dn, attributes: attributes)
  result = Acs::Ldap::Result.new(get_connection.get_operation_result)
  logger.info "Add result #{result}"

  result
end

#baseObject



96
97
98
# File 'lib/acs/ldap/connector.rb', line 96

def base
  @base
end

#close_connectionObject



100
101
102
103
104
105
# File 'lib/acs/ldap/connector.rb', line 100

def close_connection
  if @connected
    @ldap = nil
  end
  @connected = false
end

#delete(dn) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/acs/ldap/connector.rb', line 80

def delete(dn)
  logger.info "Delete dn '#{dn}'"
  get_connection.delete(dn: dn)
  result = Acs::Ldap::Result.new(get_connection.get_operation_result)
  logger.info "Delete result #{result}"

  result
end

#delete_all(ou) ⇒ Object



89
90
91
92
93
94
# File 'lib/acs/ldap/connector.rb', line 89

def delete_all(ou)
  logger.info "Delete all ou=#{ou}"
  search({base: "ou=#{ou},#{base}", attributes: 'uid'}).data.each do |entry|
    delete(entry[:dn].first) if entry[:uid].present?
  end
end

#get_connectionObject



107
108
109
110
111
112
113
114
# File 'lib/acs/ldap/connector.rb', line 107

def get_connection
  if @connected
    @ldap
  else
    @ldap = connect
  end
  @ldap
end

#ldap_paramsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/acs/ldap/connector.rb', line 14

def ldap_params
  ldap_params = {
    host: @host,
    port: @port,
    base: @base,
    auth: {
      method: :simple, #other method ?
      username: @dn,
      password: @password
    }
  }

  ldap_params[:encryption] = :simple_tls if @tls

  logger.debug "Connection params: #{ldap_params}"

  ldap_params
end

#search(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/acs/ldap/connector.rb', line 33

def search(options = {})
  base        = options[:base] || nil
  filter      = options[:filter] || nil # instance of Net::LDAP::Filter
  attributes  = options[:attributes] || nil
  logger.info "Search base '#{base}' filter '#{filter}' attributes '#{attributes}'"
  entries = []
  get_connection.search({base: base, filter: filter, attributes: attributes}) do |entry|
    entries << entry
  end
  result = Acs::Ldap::Result.new(get_connection.get_operation_result, entries)
  logger.info "Search result #{result}"
  result
end

#search_by(base, key, value, attributes = nil) ⇒ Object



47
48
49
50
# File 'lib/acs/ldap/connector.rb', line 47

def search_by(base, key, value, attributes = nil)
  filter = Net::LDAP::Filter.eq(key, value.to_s)
  search({base: base, filter: filter, attributes: attributes})
end

#search_one(base, key, value, attributes = nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/acs/ldap/connector.rb', line 52

def search_one(base, key, value, attributes = nil)
  result = search_by(base, key, value, attributes)
  if result.data.count > 0
    result.data[0]
  else
    nil
  end
end

#update(dn, operations) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/acs/ldap/connector.rb', line 71

def update(dn, operations)
  logger.info "Modify dn '#{dn}' operations '#{operations.inspect}'"
  get_connection.modify(dn: dn, operations: operations)
  result = Acs::Ldap::Result.new(get_connection.get_operation_result)
  logger.info "Modify result #{result}"

  result
end