Sha256: 6e6a32558cdb8088d2efc1793eef591c562bc6ac86a59cee9a47f856a6f64eda

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

class Acs::Ldap::Pusher

  # connector : Acs::Ldap::Connector instance
  # options[:mapper] : directly set a mapper at initialization
  def initialize(connector, options = {})
    @connector = connector
    @mapper = options[:mapper] || nil
  end

  def mapper=(mapper)
    @mapper = mapper
  end

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

  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

  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

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

  def flush
    @connector.delete_all(ou)
  end

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

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

  def count
    count = 0
    @connector.search({base: base}).data.each do |entry|
      count += 1 if entry[:uid].present?
    end
    count
  end

protected

  def ou
    @mapper.ou
  end

  def base
    "ou=#{ou},#{@connector.base}"
  end

  def dn(model)
    "uid=#{@mapper.uid(model)},#{base}"
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
acs-ldap-0.2.4 lib/acs/ldap/pusher.rb
acs-ldap-0.2.3 lib/acs/ldap/pusher.rb
acs-ldap-0.2.2 lib/acs/ldap/pusher.rb
acs-ldap-0.2.1 lib/acs/ldap/pusher.rb