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,
  :base,
  :offset,
  :use_paged_results,
  :page_size,
]

Instance Method Summary collapse

Instance Method Details

#count(options = {}) ⇒ Object



123
124
125
# File 'lib/active_ldap/operations.rb', line 123

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

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

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_ldap/operations.rb', line 98

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

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

  attribute = attr || ensure_search_attribute
  escaped_value = DN.escape_value(value)
  options_for_non_leaf = {
    :attribute => attr,
    :value => value,
    :prefix => ["#{attribute}=#{escaped_value}", prefix].compact.join(","),
    :limit => 1,
    :scope => :base,
  }

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

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



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_ldap/operations.rb', line 43

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

  value = value.first if value.is_a?(Array) and value.first.size == 1

  _attr = nil
  _prefix = nil
  if attr.nil? or attr == dn_attribute
    _attr, value, _prefix = split_search_value(value)
  end
  attr ||= _attr || ensure_search_attribute
  prefix ||= _prefix
  filter ||= [attr, value]
  filter = [:and, filter, *object_class_filters(classes)]
  _base = options[:base] ? [options[:base]] : [prefix, base]
  _base = prepare_search_base(_base)
  search_options = {
    :base => _base,
    :scope => options[:scope] || scope,
    :filter => filter,
    :limit => options[:limit],
    :attributes => requested_attributes,
    :sort_by => options[:sort_by] || sort_by,
    :order => options[:order] || order,
    :use_paged_results => options[:use_paged_results],
    :page_size => options[:page_size],
  }
  options[:connection] ||= connection
  values = []
  requested_all_attributes_p =
    (requested_attributes.nil? or requested_attributes.include?('*'))
  options[:connection].search(search_options) do |dn, attrs|
    attributes = {}
    attrs.each do |key, _value|
      if requested_all_attributes_p or requested_attributes.include?(key)
        normalized_attribute, normalized_value =
          normalize_attribute_options(key, _value)
        attributes[normalized_attribute] ||= []
        attributes[normalized_attribute].concat(normalized_value)
      else
        next
      end
    end
    values << [dn, attributes]
  end
  values = values.collect {|_value| yield(_value)} if block_given?
  values
end