# module ActiveLdap::Operations::Common

### Constants

VALID_SEARCH_OPTIONS

### Public Instance Methods

count(options={}) click to toggle source

    # File lib/active_ldap/operations.rb, line 112
    def count(options={})
      search(options).size
    end

exist?(dn, options={}) click to toggle source

    # File lib/active_ldap/operations.rb, line 88
    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
      options_for_non_leaf = {
        :attribute => attr,
        :value => value,
        :prefix => ["#{attribute}=#{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

Also aliased as: exists?
exists?(dn, options={})
Alias for: exist?
search(options={}) { |_value| ... } click to toggle source

    # File lib/active_ldap/operations.rb, line 29
    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)
      if options.has_key?(:ldap_scope)
        message = _(":ldap_scope search option is deprecated. "                        "Use :scope instead.")
        ActiveSupport::Deprecation.warn(message)
        options[:scope] ||= options[:ldap_scope]
      end
      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,
      }
      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

### Private Instance Methods

ensure_base(target) click to toggle source

    # File lib/active_ldap/operations.rb, line 134
    def ensure_base(target)
      [truncate_base(target), base.to_s].reject do |component|
        component.blank?
      end.join(',')
    end

ensure_dn_attribute(target) click to toggle source

    # File lib/active_ldap/operations.rb, line 129
    def ensure_dn_attribute(target)
      "#{dn_attribute}=" +
        target.gsub(/^\s*#{Regexp.escape(dn_attribute)}\s*=\s*/i, '')
    end

ensure_search_attribute(*candidates) click to toggle source

    # File lib/active_ldap/operations.rb, line 125
    def ensure_search_attribute(*candidates)
      default_search_attribute || "objectClass"
    end

extract_options_from_args!(args) click to toggle source

    # File lib/active_ldap/operations.rb, line 121
    def extract_options_from_args!(args)
      args.last.is_a?(Hash) ? args.pop : {}
    end

object_class_filters(classes=nil) click to toggle source

    # File lib/active_ldap/operations.rb, line 175
    def object_class_filters(classes=nil)
      expected_classes = (classes || required_classes).collect do |name|
        Escape.ldap_filter_escape(name)
      end
      unexpected_classes = excluded_classes.collect do |name|
        Escape.ldap_filter_escape(name)
      end
      filters = []
      unless expected_classes.empty?
        filters << ["objectClass", "=", *expected_classes]
      end
      unless unexpected_classes.empty?
        filters << [:not, [:or, ["objectClass", "=", *unexpected_classes]]]
      end
      filters
    end

prepare_search_base(components) click to toggle source

    # File lib/active_ldap/operations.rb, line 162
    def prepare_search_base(components)
      components.compact.collect do |component|
        case component
        when String
          component
        when DN
          component.to_s
        else
          DN.new(*component).to_s
        end
      end.reject{|x| x.empty?}.join(",")
    end

split_search_value(value) click to toggle source

    # File lib/active_ldap/operations.rb, line 192
    def split_search_value(value)
      attr = prefix = nil

      begin
        dn = DN.parse(value)
        attr, value = dn.rdns.first.to_a.first
        rest = dn.rdns[1..-1]
        prefix = DN.new(*rest).to_s unless rest.empty?
      rescue DistinguishedNameInputInvalid
        return [attr, value, prefix]
      rescue DistinguishedNameInvalid
        begin
          dn = DN.parse("DUMMY=#{value}")
          _, value = dn.rdns.first.to_a.first
          rest = dn.rdns[1..-1]
          prefix = DN.new(*rest).to_s unless rest.empty?
        rescue DistinguishedNameInvalid
        end
      end

      prefix = nil if prefix == base
      prefix = truncate_base(prefix) if prefix
      [attr, value, prefix]
    end

truncate_base(target) click to toggle source

    # File lib/active_ldap/operations.rb, line 140
    def truncate_base(target)
      return nil if target.blank?
      return target if base.nil?

      parsed_target = nil
      if target.is_a?(DN)
        parsed_target = target
      elsif /,/ =~ target
        begin
          parsed_target = DN.parse(target)
        rescue DistinguishedNameInvalid
        end
      end

      return target if parsed_target.nil?
      begin
        (parsed_target - base).to_s
      rescue ArgumentError
        target
      end
    end

validate_search_options(options) click to toggle source

    # File lib/active_ldap/operations.rb, line 117
    def validate_search_options(options)
      options.assert_valid_keys(VALID_SEARCH_OPTIONS)
    end