active_directory 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -4,6 +4,9 @@ Ruby Integration with Microsoft's Active Directory system based on original code
4
4
 
5
5
  See documentation on ActiveDirectory::Base for more information.
6
6
 
7
+ Caching:
8
+ Queries for membership and group membership are based on the distinguished name of objects. Doing a lot of queries, especially for a Rails app, is a sizable slowdown. To alleviate the problem, I've implemented a very basic cache for queries which search by :distinguishedname. This is diabled by default. All other queries are unaffected.
9
+
7
10
 
8
11
  A code example is worth a thousand words:
9
12
 
@@ -31,4 +34,10 @@ ActiveDirectory::User.find(:all)
31
34
  ActiveDirectory::User.find(:first, :userprincipalname => "john.smith@domain.com")
32
35
 
33
36
  ActiveDirectory::Group.find(:all)
37
+
38
+ #Caching is disabled by default, to enable:
39
+ ActiveDirectory::Base.enable_cache
40
+ ActiveDirectory::Base.disable_cache
41
+ ActiveDirectory::Base.cache?
42
+
34
43
  </pre>
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ require 'psych'
1
2
 
2
3
  begin
3
4
  require 'jeweler'
@@ -16,4 +17,4 @@ begin
16
17
  Jeweler::GemcutterTasks.new
17
18
  rescue LoadError
18
19
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
- end
20
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.0
1
+ 1.5.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{active_directory}
8
- s.version = "1.4.0"
8
+ s.version = "1.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam T Kerr"]
12
- s.date = %q{2011-03-02}
12
+ s.date = %q{2011-03-03}
13
13
  s.description = %q{ActiveDirectory uses Net::LDAP to provide a means of accessing and modifying an Active Directory data store. This is a fork of the activedirectory gem.}
14
14
  s.email = %q{ajrkerr@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
38
38
  ]
39
39
  s.homepage = %q{http://github.com/ajrkerr/active_directory}
40
40
  s.require_paths = ["lib"]
41
- s.rubygems_version = %q{1.5.2}
41
+ s.rubygems_version = %q{1.6.0}
42
42
  s.summary = %q{An interface library for accessing Microsoft's Active Directory.}
43
43
 
44
44
  if s.respond_to? :specification_version then
@@ -239,7 +239,7 @@ module ActiveDirectory
239
239
  }
240
240
 
241
241
  cached_results = find_cached_results(args[1])
242
- return cached_results unless cached_results.nil?
242
+ return cached_results if cached_results or cached_results.nil?
243
243
 
244
244
  options[:in] = [ options[:in].to_s, @@settings[:base] ].delete_if { |part| part.empty? }.join(",")
245
245
 
@@ -258,55 +258,56 @@ module ActiveDirectory
258
258
  end
259
259
  end
260
260
 
261
- ##
262
- # Filters the cache result by the object type we're looking for
263
- #
264
- def self.filter_cache_result(result)
265
- result.delete_if { |entry| !entry.kind_of? self }
266
- end
267
-
268
261
  ##
269
262
  # Searches the cache and returns the result
263
+ # Returns false on failure, nil on wrong object type
270
264
  #
271
265
  def self.find_cached_results(filters)
272
- return nil unless cache?
266
+ return false unless cache?
273
267
 
274
268
  #Check to see if we're only looking for :distinguishedname
275
- if filters.is_a? Hash and filters.keys == [:distinguishedname]
276
- #Find keys we're looking up, convert to array
277
- dns = filters[:distinguishedname]
269
+ return false unless filters.is_a? Hash and filters.keys == [:distinguishedname]
278
270
 
279
- if dns.kind_of? Array
280
- #Check to see if all of the results are in teh cache
281
- return nil unless (dns & @@cache.keys == dns)
271
+ #Find keys we're looking up
272
+ dns = filters[:distinguishedname]
282
273
 
283
- result = []
274
+ if dns.kind_of? Array
275
+ result = []
284
276
 
285
- dns.each { |dn| result << @@cache[dn] }
277
+ dns.each do |dn|
278
+ entry = @@cache[dn]
286
279
 
287
- return filter_cache_result(result) if result.size == dns.size
288
- else
289
- return @@cache[dns] if @@cache.key? dns and @@cache[dns].is_a? self.class
280
+ #If the object isn't in the cache just run the query
281
+ return false if entry.nil?
282
+
283
+ #Only permit objects of the type we're looking for
284
+ result << entry if entry.kind_of? self
290
285
  end
286
+
287
+ return result
288
+ else
289
+ return false unless @@cache.key? dns
290
+ return @@cache[dns] if @@cache[dns].is_a? self
291
291
  end
292
292
  end
293
293
 
294
294
  def self.find_all(options)
295
295
  results = []
296
- @@ldap.search(:filter => options[:filter], :base => options[:in], :return_result => false) do |entry|
297
- ad_entry = new(entry)
298
- @@cache[ad_entry.dn] = ad_entry
299
- results << ad_entry
296
+ ldap_objs = @@ldap.search(:filter => options[:filter], :base => options[:in])
297
+
298
+ ldap_objs.each do |entry|
299
+ ad_obj = new(entry)
300
+ @@cache[entry.dn] = ad_obj unless ad_obj.instance_of? Base
301
+ results << ad_obj
300
302
  end
303
+
301
304
  results
302
305
  end
303
306
 
304
307
  def self.find_first(options)
305
- @@ldap.search(:filter => options[:filter], :base => options[:in], :return_result => false) do |entry|
306
- ad_entry = new(entry)
307
- @@cache[ad_entry.dn] = ad_entry
308
- return ad_entry
309
- end
308
+ ad_obj = new(@@ldap.search(:filter => options[:filter], :base => options[:in]))
309
+ @@cache[ad_obj.dn] = ad_obj unless ad_obj.instance_of? Base
310
+ return ad_obj
310
311
  end
311
312
 
312
313
  def self.method_missing(name, *args) # :nodoc:
@@ -336,7 +337,7 @@ module ActiveDirectory
336
337
 
337
338
  def ==(other) # :nodoc:
338
339
  return false if other.nil?
339
- other.objectGUID == objectGUID
340
+ other[:objectguid] == get_attr(:objectguid)
340
341
  end
341
342
 
342
343
  #
@@ -386,7 +387,7 @@ module ActiveDirectory
386
387
  values = values.collect { |v| v.to_s }
387
388
 
388
389
  current_value = begin
389
- @entry.send(attribute)
390
+ @entry[attribute]
390
391
  rescue NoMethodError
391
392
  nil
392
393
  end
@@ -494,29 +495,73 @@ module ActiveDirectory
494
495
  end
495
496
  end
496
497
 
498
+ ##
499
+ # Pull the class we're in
500
+ # This isn't quite right, as extending the object does funny things to how we
501
+ # lookup objects
502
+ def self.class_name
503
+ @klass ||= (self.name.include?('::') ? self.name[/.*::(.*)/, 1] : self.name)
504
+ end
505
+
497
506
  ##
498
507
  # Grabs the field type depending on the class it is called from
499
508
  # Takes the field name as a parameter
500
509
  def self.get_field_type(name)
501
510
  #Extract class name
502
511
  throw "Invalid field name" if name.nil?
503
- klass = self.name.include?('::') ? self.name[/.*::(.*)/, 1] : self.name
504
- type = ::ActiveDirectory.special_fields[klass.to_sym][name.to_s.downcase.to_sym]
512
+ type = ::ActiveDirectory.special_fields[class_name.to_sym][name.to_s.downcase.to_sym]
505
513
  type.to_s unless type.nil?
506
514
  end
507
515
 
516
+ @types = {}
517
+
508
518
  def self.decode_field(name, value) # :nodoc:
509
519
  type = get_field_type name
510
- return ::ActiveDirectory::FieldType::const_get(type).decode(value) if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
520
+ if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
521
+ return ::ActiveDirectory::FieldType::const_get(type).decode(value)
522
+ end
511
523
  return value
512
524
  end
513
525
 
514
526
  def self.encode_field(name, value) # :nodoc:
515
527
  type = get_field_type name
516
- return ::ActiveDirectory::FieldType::const_get(type).encode(value) if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
528
+ if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
529
+ return ::ActiveDirectory::FieldType::const_get(type).encode(value)
530
+ end
517
531
  return value
518
532
  end
519
533
 
534
+ ##
535
+ # Reads the array of values for the provided attribute. The attribute name
536
+ # is canonicalized prior to reading. Returns an empty array if the
537
+ # attribute does not exist.
538
+ def [](name)
539
+ get_attr(name)
540
+ end
541
+
542
+ ##
543
+ # Ensure that flattening works correctly
544
+ def to_ary
545
+ end
546
+
547
+ def valid_attribute? name
548
+ @attributes.has_key?(name) || @entry.attribute_names.include?(name)
549
+ end
550
+
551
+ def get_attr(name)
552
+ name = name.to_s.downcase
553
+
554
+ return decode_field(name, @attributes[name.to_sym]) if @attributes.has_key?(name.to_sym)
555
+
556
+ if @entry.attribute_names.include? name.to_sym
557
+ value = @entry[name.to_sym]
558
+ value = value.first if value.kind_of?(Array) && value.size == 1
559
+ value = value.to_s if value.nil? || value.size == 1
560
+ value = nil.to_s if value.empty?
561
+ return self.class.decode_field(name, value)
562
+ end
563
+ end
564
+
520
565
  def method_missing(name, args = []) # :nodoc:
521
566
  name = name.to_s.downcase
522
567
 
@@ -525,20 +570,11 @@ module ActiveDirectory
525
570
  @attributes[name.to_sym] = encode_field(name, args)
526
571
  end
527
572
 
528
- return decode_field(name, @attributes[name.to_sym]) if @attributes.has_key?(name.to_sym)
529
-
530
- if @entry
531
- begin
532
- value = @entry.send(name.to_sym)
533
- value = value.first if value.kind_of?(Array) && value.size == 1
534
- value = value.to_s if value.nil? || value.size == 1
535
- return self.class.decode_field(name, value)
536
- rescue NoMethodError => e
537
- return nil
538
- end
573
+ if valid_attribute? name.to_sym
574
+ get_attr(name)
575
+ else
576
+ super
539
577
  end
540
-
541
- super
542
578
  end
543
579
 
544
580
  end
@@ -32,7 +32,7 @@ module ActiveDirectory
32
32
  # Decodes a binary GUID as a hex string
33
33
  #
34
34
  def self.decode(guid)
35
- guid.unpack("H*").to_s
35
+ guid.unpack("H*").first.to_s
36
36
  end
37
37
  end
38
38
  end
@@ -33,7 +33,7 @@ module ActiveDirectory
33
33
  #
34
34
  def self.decode(dn_array)
35
35
  # How to do user or group?
36
- Base.find( :all, :distinguishedname => dn_array)
36
+ Base.find(:all, :distinguishedname => dn_array)
37
37
  end
38
38
  end
39
39
  end
@@ -33,7 +33,7 @@ module ActiveDirectory
33
33
  #
34
34
  def self.decode(dn_array)
35
35
  # How to do user or group?
36
- Group.find( :all, :distinguishedname => dn_array)
36
+ Group.find(:all, :distinguishedname => dn_array)
37
37
  end
38
38
  end
39
39
  end
@@ -33,10 +33,14 @@ module ActiveDirectory
33
33
  #
34
34
  def self.decode(dn_array)
35
35
  # Ensures that the objects are cast correctly
36
- users = [User.find( :all, :distinguishedname => dn_array)].flatten
37
- groups = [Group.find(:all, :distinguishedname => dn_array)].flatten
36
+ users = User.find(:all, :distinguishedname => dn_array)
37
+ groups = Group.find(:all, :distinguishedname => dn_array)
38
38
 
39
- users + groups
39
+ arr = Array.new
40
+ arr << users unless users.nil?
41
+ arr << groups unless groups.nil?
42
+
43
+ return arr.flatten
40
44
  end
41
45
  end
42
46
  end
@@ -33,7 +33,7 @@ module ActiveDirectory
33
33
  #
34
34
  def self.decode(dn_array)
35
35
  # How to do user or group?
36
- User.find( :all, :distinguishedname => dn_array)
36
+ User.find(:all, :distinguishedname => dn_array)
37
37
  end
38
38
  end
39
39
  end
metadata CHANGED
@@ -1,48 +1,36 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: active_directory
3
- version: !ruby/object:Gem::Version
4
- hash: 7
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.5.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 4
9
- - 0
10
- version: 1.4.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Adam T Kerr
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-03-02 00:00:00 -05:00
12
+ date: 2011-03-03 00:00:00.000000000 -05:00
19
13
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: net-ldap
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2152460440 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 25
30
- segments:
31
- - 0
32
- - 1
33
- - 1
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
34
22
  version: 0.1.1
35
23
  type: :runtime
36
- version_requirements: *id001
37
- description: ActiveDirectory uses Net::LDAP to provide a means of accessing and modifying an Active Directory data store. This is a fork of the activedirectory gem.
24
+ prerelease: false
25
+ version_requirements: *2152460440
26
+ description: ActiveDirectory uses Net::LDAP to provide a means of accessing and modifying
27
+ an Active Directory data store. This is a fork of the activedirectory gem.
38
28
  email: ajrkerr@gmail.com
39
29
  executables: []
40
-
41
30
  extensions: []
42
-
43
- extra_rdoc_files:
31
+ extra_rdoc_files:
44
32
  - README.md
45
- files:
33
+ files:
46
34
  - README.md
47
35
  - Rakefile
48
36
  - VERSION
@@ -65,36 +53,26 @@ files:
65
53
  has_rdoc: true
66
54
  homepage: http://github.com/ajrkerr/active_directory
67
55
  licenses: []
68
-
69
56
  post_install_message:
70
57
  rdoc_options: []
71
-
72
- require_paths:
58
+ require_paths:
73
59
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
60
+ required_ruby_version: !ruby/object:Gem::Requirement
75
61
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
- version: "0"
83
- required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
67
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- hash: 3
89
- segments:
90
- - 0
91
- version: "0"
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
92
72
  requirements: []
93
-
94
73
  rubyforge_project:
95
- rubygems_version: 1.5.2
74
+ rubygems_version: 1.6.0
96
75
  signing_key:
97
76
  specification_version: 3
98
77
  summary: An interface library for accessing Microsoft's Active Directory.
99
78
  test_files: []
100
-