ruby-activeldap 0.5.9 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -82,7 +82,11 @@ module ActiveLDAP
82
82
  def #{association_id}(objects = true)
83
83
  local_key = "#{local_key}"
84
84
  local_key = dnattr() if local_key.empty?
85
- return #{klass}.find_all(:attribute => "#{key}", :value => send(local_key.to_sym), :objects => objects)
85
+ results = []
86
+ #{klass}.find_all(:attribute => "#{key}", :value => send(local_key.to_sym), :objects => objects).each do |o|
87
+ results << o
88
+ end
89
+ return results
86
90
  end
87
91
  end_eval
88
92
  end
@@ -110,18 +114,20 @@ module ActiveLDAP
110
114
  foreign_key = dnattr()
111
115
  end
112
116
  results = []
113
- @data["#{key}"].each do |item|
114
- fkey = ""
115
- if foreign_key == "dn" and not item.empty?
116
- fkey = item.split(',')[0].split('=')[0]
117
- item = item.split(',')[0].split('=')[1]
118
- end
119
- # This will even yield entries that don't necessarily exist
120
- if foreign_key != "dn"
121
- fkey = foreign_key
122
- end
123
- #{klass}.find_all(:attribute => fkey, :value => item, :objects => objects).each do |match|
124
- results << match
117
+ unless @data["#{key}"].nil?
118
+ @data["#{key}"].each do |item|
119
+ fkey = ""
120
+ if foreign_key == "dn" and not item.empty?
121
+ fkey = item.split(',')[0].split('=')[0]
122
+ item = item.split(',')[0].split('=')[1]
123
+ end
124
+ # This will even yield entries that don't necessarily exist
125
+ if foreign_key != "dn"
126
+ fkey = foreign_key
127
+ end
128
+ #{klass}.find_all(:attribute => fkey, :value => item, :objects => objects).each do |match|
129
+ results << match
130
+ end
125
131
  end
126
132
  end
127
133
  return results
@@ -80,6 +80,13 @@ module ActiveLDAP
80
80
  class ObjectClassError < RuntimeError
81
81
  end
82
82
 
83
+ # AttributeAssignmentError
84
+ #
85
+ # An exception raised when there is an issue assigning a value to
86
+ # an attribute
87
+ class AttributeAssignmentError < RuntimeError
88
+ end
89
+
83
90
 
84
91
  # Base
85
92
  #
@@ -408,7 +415,8 @@ module ActiveLDAP
408
415
  tries = 0
409
416
  begin
410
417
  # Get some attributes
411
- @@conn.search(base(), LDAP::LDAP_SCOPE_ONELEVEL, "(#{attr}=#{val})") do |m|
418
+ @@conn.search(base(), LDAP::LDAP_SCOPE_ONELEVEL,
419
+ "(#{attr}=#{val})") do |m|
412
420
  # Extract the dnattr value
413
421
  dnval = m.dn.split(/,/)[0].split(/=/)[1]
414
422
 
@@ -488,7 +496,8 @@ module ActiveLDAP
488
496
  # val can be a dn attribute value, a full DN, or a LDAP::Entry. The use
489
497
  # with a LDAP::Entry is primarily meant for internal use by find and
490
498
  # find_all.
491
- def initialize(val='')
499
+ def initialize(val)
500
+ @exists = false
492
501
  # Try a default connection if none made explicitly
493
502
  unless Base.connection
494
503
  # Use @@config if it has been prepopulated and the conn is down.
@@ -526,17 +535,19 @@ module ActiveLDAP
526
535
  end
527
536
 
528
537
  # Do a search - if it exists, pull all data and parse schema, if not, just set the hierarchical data
529
- if val.empty?
530
- @exists = false
531
- # Setup what should eb authoritative
538
+ if val.class != String or val.empty?
539
+ raise TypeError, 'a dn attribute String must be supplied ' +
540
+ 'on initialization'
541
+ else
542
+ # Create what should be the authoritative DN
532
543
  @dn = "#{dnattr()}=#{val},#{base()}"
533
- send(:apply_objectclass, required_classes())
534
- else # do a search then
544
+
535
545
  # Search for the existing entry
536
546
  tries = 0
537
547
  begin
538
548
  # Get some attributes
539
549
  Base.connection.search(base(), LDAP::LDAP_SCOPE_ONELEVEL, "(#{dnattr()}=#{val})") do |m|
550
+ @exists = true
540
551
  # Save DN
541
552
  @dn = m.dn
542
553
  # Load up data into tmp
@@ -556,43 +567,41 @@ module ActiveLDAP
556
567
  end
557
568
  end
558
569
  end
559
- @exists = true
560
- # Populate schema data
561
- send(:apply_objectclass, @ldap_data['objectClass'])
562
-
563
- # Populate real data now that we have the schema with aliases
564
- @ldap_data.each do |pair|
565
- send(:attribute_method=, pair[0], pair[1].dup)
566
- end
567
570
  rescue RuntimeError => detail
568
571
  #todo# check for 'no message' when retrying
569
572
  # the connection may have gone stale. let's reconnect and retry.
570
- if tries > @@config[:retries]
571
- @exists = false
572
- # Create what should be the authoritative DN
573
- @dn = "#{dnattr()}=#{val},#{base()}"
574
- send(:apply_objectclass, required_classes())
575
-
576
- # Setup dn attribute (later rdn too!)
577
- attr_sym = "#{dnattr()}=".to_sym
578
-
579
- send(attr_sym, val)
573
+ if tries <= @@config[:retries]
574
+ tries += 1
575
+ # reconnect and rebind.
576
+ do_connect()
577
+ retry
578
+ else
579
+ @@logger.error('new: unable to search for entry')
580
+ raise detail
580
581
  end
581
- tries += 1
582
- # reconnect and rebind.
583
- do_connect()
584
- retry
585
582
  rescue LDAP::ResultError
586
- @exists = false
587
- # Create what should be the authoritative DN
588
- @dn = "#{dnattr()}=#{val},#{base()}"
589
- send(:apply_objectclass, required_classes())
583
+ end
584
+ end
590
585
 
591
- # Setup dn attribute (later rdn too!)
592
- attr_sym = "#{dnattr()}=".to_sym
586
+ # Do the actual object setup work.
587
+ if @exists
588
+ # Populate schema data
589
+ send(:apply_objectclass, @ldap_data['objectClass'])
590
+
591
+ # Populate real data now that we have the schema with aliases
592
+ @ldap_data.each do |pair|
593
+ real_attr = @attr_methods[pair[0]]
594
+
595
+ @data[real_attr] = pair[1].dup
593
596
 
594
- send(attr_sym, val)
595
597
  end
598
+ else
599
+ send(:apply_objectclass, required_classes())
600
+
601
+ # Setup dn attribute (later rdn too!)
602
+ real_dnattr = @attr_methods[dnattr()]
603
+ @data[real_dnattr] = val
604
+
596
605
  end
597
606
  end # initialize
598
607
 
@@ -961,7 +970,10 @@ module ActiveLDAP
961
970
 
962
971
  # Populate real data now that we have the schema with aliases
963
972
  @ldap_data.each do |pair|
964
- send(:attribute_method=, pair[0], pair[1].dup)
973
+ real_attr = @attr_methods[pair[0]]
974
+
975
+ @data[real_attr] = pair[1].dup
976
+
965
977
  end
966
978
  end # import
967
979
 
@@ -1291,6 +1303,11 @@ module ActiveLDAP
1291
1303
  attr = @attr_methods[method]
1292
1304
 
1293
1305
 
1306
+ # Check if it is the DN attribute
1307
+ if dnattr() == attr
1308
+ raise AttributeAssignmentError, 'cannot modify the DN attribute value'
1309
+ end
1310
+
1294
1311
  # Assign the value
1295
1312
  @data[attr] = value
1296
1313
 
@@ -8,7 +8,7 @@ module ActiveLDAP
8
8
  module Configuration
9
9
  @@host = "127.0.0.1"
10
10
  @@port = 389
11
- @@bind_format = "uid=%s,ou=People,dc=localdomain"
11
+ @@bind_format = "cn=%s,dc=localdomain"
12
12
 
13
13
  # Make the return value the string that is your LDAP base
14
14
  def Base.base
data/lib/activeldap.rb CHANGED
@@ -909,7 +909,7 @@ require 'activeldap/schema2'
909
909
 
910
910
 
911
911
  module ActiveLDAP
912
- VERSION = "0.5.9"
912
+ VERSION = "0.6.0"
913
913
  end
914
914
 
915
915
  ActiveLDAP::Base.class_eval do
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.4
3
3
  specification_version: 1
4
4
  name: ruby-activeldap
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.9
7
- date: 2005-11-01
6
+ version: 0.6.0
7
+ date: 2006-01-09
8
8
  summary: Ruby/ActiveLDAP is a object-oriented API to LDAP
9
9
  require_paths:
10
10
  - lib