Class: Acs::Ldap::Pusher

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

Instance Method Summary collapse

Constructor Details

#initialize(connector, options = {}) ⇒ Pusher

connector : Acs::Ldap::Connector instance options : directly set a mapper at initialization



5
6
7
8
# File 'lib/acs/ldap/pusher.rb', line 5

def initialize(connector, options = {})
  @connector = connector
  @mapper = options[:mapper] || nil
end

Instance Method Details

#countObject



79
80
81
82
# File 'lib/acs/ldap/pusher.rb', line 79

def count
  filter = Net::LDAP::Filter.pres('uid')
  @connector.search({base: base, filter: filter}).data.count
end

#create(model, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/acs/ldap/pusher.rb', line 27

def create(model, options = {})
  attributes = @mapper.attributes(model).except!(:uid)
  attributes.merge!(objectClass: @mapper.object_class)

  Acs::Ldap.logger.debug "Pusher#create dn '#{dn(model)}' attributes '#{attributes.inspect}'"

  @connector.add(
    dn(model),
    attributes
  )
end

#destroy(model, options = {}) ⇒ Object



59
60
61
# File 'lib/acs/ldap/pusher.rb', line 59

def destroy(model, options = {})
  @connector.delete(dn(model))
end

#exist?(model) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/acs/ldap/pusher.rb', line 67

def exist?(model)
  @connector.search({base: dn(model)}).data.length > 0
end

#find_by(key, value) ⇒ Object



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

def find_by(key, value)
  @connector.search_by(
    base,
    key,
    value
  )
end

#flushObject



63
64
65
# File 'lib/acs/ldap/pusher.rb', line 63

def flush
  @connector.delete_all(ou)
end

#indexObject



22
23
24
25
# File 'lib/acs/ldap/pusher.rb', line 22

def index
  filter = Net::LDAP::Filter.pres('uid')
  @connector.search({base: base, filter: filter})
end

#mapper=(mapper) ⇒ Object



10
11
12
# File 'lib/acs/ldap/pusher.rb', line 10

def mapper=(mapper)
  @mapper = mapper
end

#save(model, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/acs/ldap/pusher.rb', line 14

def save(model, options = {})
  if exist?(model)
    update(model)
  else
    create(model)
  end
end

#update(model, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acs/ldap/pusher.rb', line 39

def update(model, options = {})
  changes = options.fetch(:changes, nil)
  attributes = @mapper.attributes(model).except(:uid)
  operations = []
  attributes.each do |key, value|
    if attributes.nil? or attributes.include?(key)
      if changes.nil?
        operations << [:replace, key.to_s, value]
      else
        operations << [:replace, key.to_s, value] if changes.has_key?(key)
      end
    end
  end

  @connector.update(
    dn(model),
    operations
  )
end