Module: ActiveLDAP::Associations::ClassMethods

Defined in:
lib/activeldap/associations.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(association_id, options = {}) ⇒ Object

belongs_to

This defines a method for an extension class map its DN key attribute value on to multiple items which reference it by |:foreign_key| in the other LDAP entry covered by class |:class_name|.

Example:

belongs_to :groups, :class_name => Group, :foreign_key => memberUid, :local_key => 'uid'


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/activeldap/associations.rb', line 105

def belongs_to(association_id, options = {})
  klass = options[:class_name] || association_id.to_s
  key = options[:foreign_key]  || association_id.to_s + "_id"
  local_key = options[:local_key] || ''
  class_eval "    def \#{association_id}(objects = nil)\n      objects = @@config[:return_objects] if objects.nil?\n      local_key = \"\#{local_key}\"\n      local_key = dnattr() if local_key.empty?\n      results = []\n      \#{klass}.find_all(:attribute => \"\#{key}\", :value => send(local_key.to_sym), :objects => objects).each do |o|\n        results << o\n      end\n      return results\n    end\n  end_eval\nend\n"

#has_many(association_id, options = {}) ⇒ Object

has_many

This defines a method for an extension class expand an existing multi-element attribute into ActiveLDAP objects. This discards any calls which result in entries that don’t exist in LDAP!

Example:

has_many :members, :class_name => User, :local_key => memberUid, :foreign_key => 'uid'

TODO: def #…=(val) to redefine group membership



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/activeldap/associations.rb', line 135

def has_many(association_id, options = {})
  klass = options[:class_name] || association_id.to_s
  key = options[:local_key]  || association_id.to_s + "_id"
  foreign_key = options[:foreign_key] || ''
  class_eval "    def \#{association_id}(objects = nil)\n      objects = @@config[:return_objects] if objects.nil?\n      foreign_key = \"\#{foreign_key}\"\n      if foreign_key.empty?\n        foreign_key = dnattr()\n      end\n      results = []\n      unless @data[\"\#{key}\"].nil?\n        @data[\"\#{key}\"].each do |item|\n          fkey = \"\"\n          if foreign_key == \"dn\" and not item.empty?\n            fkey = item.split(',')[0].split('=')[0]\n            item = item.split(',')[0].split('=')[1]\n          end\n          # This will even yield entries that don't necessarily exist\n          if foreign_key != \"dn\"\n            fkey = foreign_key\n          end\n          \#{klass}.find_all(:attribute => fkey, :value => item, :objects => objects).each do |match|\n            results << match\n          end\n        end\n      end\n      return results\n    end\n  end_eval\nend\n"

#ldap_mapping(options = {}) ⇒ Object

This class function is used to setup all mappings between the subclass and ldap for use in activeldap

Example:

ldap_mapping :dnattr => 'uid', :prefix => 'ou=People', 
             :classes => ['top', 'posixAccount'], scope => LDAP::LDAP_SCOPE_SUBTREE,
             :parent => String


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/activeldap/associations.rb', line 22

def ldap_mapping(options = {})
  # The immediate ancestor should be the caller....
  klass = self.ancestors[0]

  dnattr = options[:dnattr] || 'cn'
  prefix = options[:prefix] || "ou=#{klass.to_s.split(':').last}"
  classes_array = options[:classes] || nil
  scope = options[:scope] || 'super'
  # When used, instantiates parent objects from the "parent dn". This
  # can be a String or a real ActiveLDAP class. This just adds the helper 
  # Base#parent.
  parent = options[:parent_class] || nil

  classes = 'super'
  unless classes_array.nil?
    raise TypeError, ":classes must be an array" \
      unless classes_array.respond_to? :size
    # Build classes array
    classes = '['
    classes_array.map! {|x| x = "'#{x}'"}
    classes << classes_array.join(', ')
    classes << ']'
  end

  # This adds the methods to the local
  # class which can then be inherited, etc
  # which describe the mapping to LDAP.
  klass.class_eval("    class << self\n      # Return the list of required object classes\n      def required_classes\n        \#{classes}\n      end\n\n      # Return the full base of the class\n      def base\n        if \"\#{prefix}\".empty?\n          return \"\\\#{super}\"\n        else\n          return \"\#{prefix},\\\#{super}\"\n        end\n      end\n\n      # Return the expected DN attribute of an object\n      def dnattr\n        '\#{dnattr}'\n      end\n\n      # Return the expected DN attribute of an object\n      def ldap_scope\n        \#{scope}\n      end\n    end\n    \n    # Hide connect\n    private_class_method :connect\n\n    # Unhide class methods\n    public_class_method :find_all\n    public_class_method :find\n    public_class_method :new\n    public_class_method :dnattr\n end_eval\n\n # Add the parent helper if desired\n if parent\n   klass.class_eval(<<-\"end_eval\")\n     def parent()\n       return \#{parent}.new(@dn.split(',')[1..-1].join(','))\n     end\n   end_eval\n end\nend\n")