Module | ActiveLdap::Operations::Common |
In: |
lib/active_ldap/operations.rb
|
VALID_SEARCH_OPTIONS | = | [:attribute, :value, :filter, :prefix, :classes, :scope, :limit, :attributes, :sort_by, :order, :connection, :base] |
# File lib/active_ldap/operations.rb, line 101 101: def count(options={}) 102: search(options).size 103: end
# File lib/active_ldap/operations.rb, line 79 79: def exist?(dn, options={}) 80: attr, value, prefix = split_search_value(dn) 81: 82: options_for_leaf = { 83: :attribute => attr, 84: :value => value, 85: :prefix => prefix, 86: } 87: 88: attribute = attr || ensure_search_attribute 89: options_for_non_leaf = { 90: :attribute => attr, 91: :value => value, 92: :prefix => ["#{attribute}=#{value}", prefix].compact.join(","), 93: :scope => :base, 94: } 95: 96: !search(options_for_leaf.merge(options)).empty? or 97: !search(options_for_non_leaf.merge(options)).empty? 98: end
# File lib/active_ldap/operations.rb, line 27 27: def search(options={}, &block) 28: validate_search_options(options) 29: attr = options[:attribute] 30: value = options[:value] || '*' 31: filter = options[:filter] 32: prefix = options[:prefix] 33: classes = options[:classes] 34: 35: value = value.first if value.is_a?(Array) and value.first.size == 1 36: 37: _attr = nil 38: _prefix = nil 39: if attr.nil? or attr == dn_attribute 40: _attr, value, _prefix = split_search_value(value) 41: end 42: attr ||= _attr || ensure_search_attribute 43: prefix ||= _prefix 44: filter ||= [attr, value] 45: filter = [:and, filter, *object_class_filters(classes)] 46: _base = options[:base] ? [options[:base]] : [prefix, base] 47: _base = prepare_search_base(_base) 48: if options.has_key?(:ldap_scope) 49: logger.warning do 50: _(":ldap_scope search option is deprecated. Use :scope instead.") 51: end 52: options[:scope] ||= options[:ldap_scope] 53: end 54: search_options = { 55: :base => _base, 56: :scope => options[:scope] || scope, 57: :filter => filter, 58: :limit => options[:limit], 59: :attributes => options[:attributes], 60: :sort_by => options[:sort_by] || sort_by, 61: :order => options[:order] || order, 62: } 63: 64: options[:connection] ||= connection 65: values = options[:connection].search(search_options) do |dn, attrs| 66: attributes = {} 67: attrs.each do |key, _value| 68: normalized_attr, normalized_value = 69: normalize_attribute_options(key, _value) 70: attributes[normalized_attr] ||= [] 71: attributes[normalized_attr].concat(normalized_value) 72: end 73: [dn, attributes] 74: end 75: values = values.collect {|_value| yield(_value)} if block_given? 76: values 77: end
# File lib/active_ldap/operations.rb, line 123 123: def ensure_base(target) 124: [truncate_base(target), base].reject do |component| 125: component.blank? 126: end.join(',') 127: end
# File lib/active_ldap/operations.rb, line 118 118: def ensure_dn_attribute(target) 119: "#{dn_attribute}=" + 120: target.gsub(/^\s*#{Regexp.escape(dn_attribute)}\s*=\s*/i, '') 121: end
# File lib/active_ldap/operations.rb, line 114 114: def ensure_search_attribute(*candidates) 115: default_search_attribute || "objectClass" 116: end
# File lib/active_ldap/operations.rb, line 110 110: def extract_options_from_args!(args) 111: args.last.is_a?(Hash) ? args.pop : {} 112: end
# File lib/active_ldap/operations.rb, line 151 151: def object_class_filters(classes=nil) 152: expected_classes = (classes || required_classes).collect do |name| 153: Escape.ldap_filter_escape(name) 154: end 155: unexpected_classes = excluded_classes.collect do |name| 156: Escape.ldap_filter_escape(name) 157: end 158: filters = [] 159: unless expected_classes.empty? 160: filters << ["objectClass", "=", *expected_classes] 161: end 162: unless unexpected_classes.empty? 163: filters << [:not, [:or, ["objectClass", "=", *unexpected_classes]]] 164: end 165: filters 166: end
# File lib/active_ldap/operations.rb, line 141 141: def prepare_search_base(components) 142: components.compact.collect do |component| 143: if component.is_a?(String) 144: component 145: else 146: DN.new(*component).to_s 147: end 148: end.reject{|x| x.empty?}.join(",") 149: end
# File lib/active_ldap/operations.rb, line 168 168: def split_search_value(value) 169: attr = prefix = nil 170: 171: begin 172: dn = DN.parse(value) 173: attr, value = dn.rdns.first.to_a.first 174: rest = dn.rdns[1..-1] 175: prefix = DN.new(*rest).to_s unless rest.empty? 176: rescue DistinguishedNameInputInvalid 177: return [attr, value, prefix] 178: rescue DistinguishedNameInvalid 179: begin 180: dn = DN.parse("DUMMY=#{value}") 181: _, value = dn.rdns.first.to_a.first 182: rest = dn.rdns[1..-1] 183: prefix = DN.new(*rest).to_s unless rest.empty? 184: rescue DistinguishedNameInvalid 185: end 186: end 187: 188: prefix = nil if prefix == base 189: prefix = truncate_base(prefix) if prefix 190: [attr, value, prefix] 191: end
# File lib/active_ldap/operations.rb, line 129 129: def truncate_base(target) 130: if /,/ =~ target 131: begin 132: (DN.parse(target) - DN.parse(base)).to_s 133: rescue DistinguishedNameInvalid, ArgumentError 134: target 135: end 136: else 137: target 138: end 139: end