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 104 104: def count(options={}) 105: search(options).size 106: end
# File lib/active_ldap/operations.rb, line 80 80: def exist?(dn, options={}) 81: attr, value, prefix = split_search_value(dn) 82: 83: options_for_leaf = { 84: :attribute => attr, 85: :value => value, 86: :prefix => prefix, 87: :limit => 1, 88: } 89: 90: attribute = attr || ensure_search_attribute 91: options_for_non_leaf = { 92: :attribute => attr, 93: :value => value, 94: :prefix => ["#{attribute}=#{value}", prefix].compact.join(","), 95: :limit => 1, 96: :scope => :base, 97: } 98: 99: !search(options_for_leaf.merge(options)).empty? or 100: !search(options_for_non_leaf.merge(options)).empty? 101: 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: message = _(":ldap_scope search option is deprecated. " \ 50: "Use :scope instead.") 51: ActiveSupport::Deprecation.warn(message) 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 = [] 66: options[:connection].search(search_options) do |dn, attrs| 67: attributes = {} 68: attrs.each do |key, _value| 69: normalized_attr, normalized_value = 70: normalize_attribute_options(key, _value) 71: attributes[normalized_attr] ||= [] 72: attributes[normalized_attr].concat(normalized_value) 73: end 74: values << [dn, attributes] 75: end 76: values = values.collect {|_value| yield(_value)} if block_given? 77: values 78: end
# File lib/active_ldap/operations.rb, line 126 126: def ensure_base(target) 127: [truncate_base(target), base.to_s].reject do |component| 128: component.blank? 129: end.join(',') 130: end
# File lib/active_ldap/operations.rb, line 121 121: def ensure_dn_attribute(target) 122: "#{dn_attribute}=" + 123: target.gsub(/^\s*#{Regexp.escape(dn_attribute)}\s*=\s*/i, '') 124: end
# File lib/active_ldap/operations.rb, line 117 117: def ensure_search_attribute(*candidates) 118: default_search_attribute || "objectClass" 119: end
# File lib/active_ldap/operations.rb, line 113 113: def extract_options_from_args!(args) 114: args.last.is_a?(Hash) ? args.pop : {} 115: end
# File lib/active_ldap/operations.rb, line 167 167: def object_class_filters(classes=nil) 168: expected_classes = (classes || required_classes).collect do |name| 169: Escape.ldap_filter_escape(name) 170: end 171: unexpected_classes = excluded_classes.collect do |name| 172: Escape.ldap_filter_escape(name) 173: end 174: filters = [] 175: unless expected_classes.empty? 176: filters << ["objectClass", "=", *expected_classes] 177: end 178: unless unexpected_classes.empty? 179: filters << [:not, [:or, ["objectClass", "=", *unexpected_classes]]] 180: end 181: filters 182: end
# File lib/active_ldap/operations.rb, line 154 154: def prepare_search_base(components) 155: components.compact.collect do |component| 156: case component 157: when String 158: component 159: when DN 160: component.to_s 161: else 162: DN.new(*component).to_s 163: end 164: end.reject{|x| x.empty?}.join(",") 165: end
# File lib/active_ldap/operations.rb, line 184 184: def split_search_value(value) 185: attr = prefix = nil 186: 187: begin 188: dn = DN.parse(value) 189: attr, value = dn.rdns.first.to_a.first 190: rest = dn.rdns[1..-1] 191: prefix = DN.new(*rest).to_s unless rest.empty? 192: rescue DistinguishedNameInputInvalid 193: return [attr, value, prefix] 194: rescue DistinguishedNameInvalid 195: begin 196: dn = DN.parse("DUMMY=#{value}") 197: _, value = dn.rdns.first.to_a.first 198: rest = dn.rdns[1..-1] 199: prefix = DN.new(*rest).to_s unless rest.empty? 200: rescue DistinguishedNameInvalid 201: end 202: end 203: 204: prefix = nil if prefix == base 205: prefix = truncate_base(prefix) if prefix 206: [attr, value, prefix] 207: end
# File lib/active_ldap/operations.rb, line 132 132: def truncate_base(target) 133: return nil if target.blank? 134: return target if base.nil? 135: 136: parsed_target = nil 137: if target.is_a?(DN) 138: parsed_target = target 139: elsif /,/ =~ target 140: begin 141: parsed_target = DN.parse(target) 142: rescue DistinguishedNameInvalid 143: end 144: end 145: 146: return target if parsed_target.nil? 147: begin 148: (parsed_target - base).to_s 149: rescue ArgumentError 150: target 151: end 152: end