ruby-activeldap-debug 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,122 @@
1
+
2
+ module ActiveLDAP
3
+ # Associations
4
+ #
5
+ # Associations provides the class methods needed for
6
+ # the extension classes to create methods using
7
+ # belongs_to and has_many
8
+ module Associations
9
+ def self.append_features(base)
10
+ super
11
+ base.extend(ClassMethods)
12
+ end
13
+ module ClassMethods
14
+ # This class function is used to setup all mappings between the subclass
15
+ # and ldap for use in activeldap
16
+ def ldap_mapping(options = {})
17
+ # The immediate ancestor should be the caller....
18
+ klass = self.ancestors[0]
19
+
20
+ dnattr = options[:dnattr] || 'cn'
21
+ prefix = options[:prefix] || "ou=#{klass.to_s.split(':').last}"
22
+ classes_array = options[:classes] || ['top']
23
+
24
+ raise TypeError, ":classes must be an array" \
25
+ unless classes_array.respond_to? :size
26
+ # Build classes array
27
+ classes = '['
28
+ classes_array.map! {|x| x = "'#{x}'"}
29
+ classes << classes_array.join(', ')
30
+ classes << ']'
31
+
32
+ # This adds the methods to the local
33
+ # class which can then be inherited, etc
34
+ # which describe the mapping to LDAP.
35
+ klass.class_eval <<-"end_eval"
36
+ class << #{klass}
37
+ # Return the list of required object classes
38
+ def required_classes
39
+ #{classes}
40
+ end
41
+
42
+ # Return the full base of the class
43
+ def base
44
+ "#{prefix},\#{super}"
45
+ end
46
+
47
+ # Return the expected DN attribute of an object
48
+ def dnattr
49
+ '#{dnattr}'
50
+ end
51
+ end
52
+
53
+ # Hide connect
54
+ private_class_method :connect
55
+
56
+ # Unhide class methods
57
+ public_class_method :find_all
58
+ public_class_method :find
59
+ public_class_method :new
60
+ public_class_method :dnattr
61
+ end_eval
62
+ end
63
+
64
+ # belongs_to
65
+ #
66
+ # This defines a method for an extension class map its DN key
67
+ # attribute value on to multiple items which reference it by
68
+ # |:foreign_key| in the other LDAP entry covered by class |:class_name|.
69
+ #
70
+ # Example:
71
+ # belongs_to :groups, :class_name => Group, :foreign_key => memberUid, :local_key => 'uid'
72
+ #
73
+ def belongs_to(association_id, options = {})
74
+ klass = options[:class_name] || association_id.to_s
75
+ key = options[:foreign_key] || association_id.to_s + "_id"
76
+ local_key = options[:local_key] || ''
77
+ class_eval <<-"end_eval"
78
+ def #{association_id}(objects = true)
79
+ local_key = "#{local_key}"
80
+ local_key = dnattr() if local_key.empty?
81
+ return #{klass}.find_all(:attribute => "#{key}", :value => send(local_key.to_sym), :objects => objects)
82
+ end
83
+ end_eval
84
+ end
85
+
86
+
87
+ # has_many
88
+ #
89
+ # This defines a method for an extension class expand an
90
+ # existing multi-element attribute into ActiveLDAP objects.
91
+ # This discards any calls which result in entries that
92
+ # don't exist in LDAP!
93
+ #
94
+ # Example:
95
+ # has_many :members, :class_name => User, :local_key => memberUid, :foreign_key => 'uid'
96
+ #
97
+ # TODO[ENH]: def #{...}=(val) to redefine group membership
98
+ def has_many(association_id, options = {})
99
+ klass = options[:class_name] || association_id.to_s
100
+ key = options[:local_key] || association_id.to_s + "_id"
101
+ foreign_key = options[:foreign_key] || ''
102
+ class_eval <<-"end_eval"
103
+ def #{association_id}(objects = true)
104
+ foreign_key = "#{foreign_key}"
105
+ if foreign_key.empty?
106
+ foreign_key = dnattr()
107
+ end
108
+ results = []
109
+ @data["#{key}"].each do |item|
110
+ # This will even yield entries that don't necessarily exist
111
+ #{klass}.find_all(:attribute => foreign_key, :value => item, :objects => objects).each do |match|
112
+ results << match
113
+ end
114
+ end
115
+ return results
116
+ end
117
+ end_eval
118
+ end
119
+
120
+ end
121
+ end
122
+ end