class Wobaduser::Base

Constants

ATTR_MV
ATTR_SV

ATTR_SV is for single valued attributes only. result is a string. ATTR_MV is for multi valued attributes. result is an array. Concrete values should be set in subclasses. See Wobaduser::User for an example.

SearchResult

Attributes

entry[R]
errors[R]

Public Class Methods

filter() click to toggle source
# File lib/wobaduser/base.rb, line 65
def self.filter
  Net::LDAP::Filter.present('objectClass')
end
new(options = {}) click to toggle source

Create an new Wobaduser object not to be intended to call directly, but possible. Better to use Wobaduser::User.new or Wobaduser::Group.new. There are to modes: 1) use Wobaduser::LDAP + LDAP-Filter 2) use a retrieved LDAP entry, i.e. used in Wobaduser::Base.search

:entry

ldap entry

:ldap

instance of Wobaduser::LDAP

:filter

ldap filter (as string, not as Net::LDAP::Filter)

:ldap_options

additional ldap options for search

:entry and (:ldap, :filter) are mutually exclusive

# File lib/wobaduser/base.rb, line 39
def initialize(options = {})
  options.symbolize_keys!
  keys = options.keys
  if keys.include?(:entry) && (keys & [:ldap, :filter, :ldap_options]).any?
    raise ArgumentError, ":entry and one of (:ldap, :filter, :ldap_options) are mutually exclusive!"
  end
  reset_errors
  get_ldap_entry(options)
  unless entry.nil?
    self.class.class_eval do
      generate_single_value_readers
      generate_multi_value_readers
    end
  end
end

Protected Class Methods

build_filter(filter) click to toggle source
# File lib/wobaduser/base.rb, line 139
def self.build_filter(filter)
  unless filter.kind_of? Net::LDAP::Filter
    filter = Net::LDAP::Filter.construct(filter)
  end
  filter & self.filter
end
generate_multi_value_readers() click to toggle source

method generator for multi value attributes defined in ATTR_MV

# File lib/wobaduser/base.rb, line 103
def self.generate_multi_value_readers
  return if ATTR_SV.nil?
  self::ATTR_MV.each_pair do |k, v|
    val, block = Array(v)
    define_method(k) do
      if @entry.attribute_names.include?(val)
        if block.is_a?(Proc)
          finals = @entry.send(val).collect(&block)
        else
          finals = @entry.send(val)
        end
        finals = finals.map{|v| v.is_a?(String) ? v.to_s.force_encoding('UTF-8') : v } if finals.is_a? Array   
        return finals.compact
      else
        return []
      end
    end
  end
end
generate_single_value_readers() click to toggle source

method generator for single value attributes defined in ATTR_SV

# File lib/wobaduser/base.rb, line 78
def self.generate_single_value_readers
  return if ATTR_SV.nil?
  self::ATTR_SV.each_pair do |k, v|
    val, block = Array(v)
    define_method(k) do
      if @entry.attribute_names.include?(val)
        attribute = @entry.send(val)
        attribute = attribute.first if attribute.is_a? Array
        if block.is_a?(Proc)
          final = block[attribute.to_s]
        else
          final = attribute.to_s
        end
        final = final.force_encoding('UTF-8') if final.is_a? String
        return final
      else
        return ''
      end
    end
  end
end
search_ldap_entries(options) click to toggle source
# File lib/wobaduser/base.rb, line 123
def self.search_ldap_entries(options)
  ldap = options.fetch(:ldap)
  if ldap.nil?
    raise "ldap connection not yet available"
  end
  filter = options.fetch(:filter)
  ldap_options = options.fetch(:ldap_options, {}).
                   merge(filter: build_filter(filter))
  entries = ldap.search(ldap_options)
  if ldap.errors.any?
    result = SearchResult.new(success: false, errors: ldap.errors, entries: [])
  else
    result = SearchResult.new(success: true, errors: [], entries: entries)
  end
end

Public Instance Methods

valid?() click to toggle source
# File lib/wobaduser/base.rb, line 69
def valid?
  @entry.kind_of? Net::LDAP::Entry
end

Protected Instance Methods

add_error(message) click to toggle source
# File lib/wobaduser/base.rb, line 148
def add_error(message)
  @errors << message
end
reset_errors() click to toggle source
# File lib/wobaduser/base.rb, line 152
def reset_errors
  @errors = []
end

Private Instance Methods

get_ldap_entry(options) click to toggle source
# File lib/wobaduser/base.rb, line 158
def get_ldap_entry(options)
  if options.keys.include?(:entry)
    @entry = options.fetch(:entry)
    reset_errors
  else
    result = Wobaduser::Base.search_ldap_entries(options)
    if result.success?
      @entry = result.entries.first
    else
      add_error(result.errors.join(", "))
      @entry = nil
    end
  end
end