Module: ActiveLdap::Operations::Common

Defined in:
lib/active_ldap/operations.rb

Constant Summary collapse

VALID_SEARCH_OPTIONS =
[:attribute, :value, :filter, :prefix,
:classes, :scope, :limit, :attributes,
:sort_by, :order, :connection]

Instance Method Summary collapse

Instance Method Details

#count(options = {}) ⇒ Object



100
101
102
# File 'lib/active_ldap/operations.rb', line 100

def count(options={})
  search(options).size
end

#exist?(dn, options = {}) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_ldap/operations.rb', line 78

def exist?(dn, options={})
  attr, value, prefix = split_search_value(dn)

  options_for_leaf = {
    :attribute => attr,
    :value => value,
    :prefix => prefix,
  }

  attribute = attr || dn_attribute || "objectClass"
  options_for_non_leaf = {
    :attribute => attr,
    :value => value,
    :prefix => ["#{attribute}=#{value}", prefix].compact.join(","),
    :scope => :base,
  }

  !search(options_for_leaf.merge(options)).empty? or
    !search(options_for_non_leaf.merge(options)).empty?
end

#search(options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_ldap/operations.rb', line 27

def search(options={}, &block)
  validate_search_options(options)
  attr = options[:attribute]
  value = options[:value] || '*'
  filter = options[:filter]
  prefix = options[:prefix]
  classes = options[:classes]

  value = value.first if value.is_a?(Array) and value.first.size == 1
  if filter.nil? and !value.is_a?(String)
    message = _("Search value must be a String: %s") % value.inspect
    raise ArgumentError, message
  end

  _attr, value, _prefix = split_search_value(value)
  attr ||= _attr || dn_attribute || "objectClass"
  prefix ||= _prefix
  filter ||= [attr, value]
  filter = [:and, filter, *object_class_filters(classes)]
  _base = [prefix, base].compact.reject{|x| x.empty?}.join(",")
  if options.has_key?(:ldap_scope)
    logger.warning do
      _(":ldap_scope search option is deprecated. Use :scope instead.")
    end
    options[:scope] ||= options[:ldap_scope]
  end
  search_options = {
    :base => _base,
    :scope => options[:scope] || scope,
    :filter => filter,
    :limit => options[:limit],
    :attributes => options[:attributes],
    :sort_by => options[:sort_by],
    :order => options[:order],
  }

  conn = options[:connection] || connection
  conn.search(search_options) do |dn, attrs|
    attributes = {}
    attrs.each do |key, value|
      normalized_attr, normalized_value =
        normalize_attribute_options(key, value)
      attributes[normalized_attr] ||= []
      attributes[normalized_attr].concat(normalized_value)
    end
    value = [dn, attributes]
    value = yield(value) if block_given?
    value
  end
end