Module ActiveLDAP::Associations::ClassMethods
In: lib/activeldap/associations.rb
RuntimeError DeleteError AttributeAssignmentError ConnectionError AuthenticationError TimeoutError ConfigurationError ObjectClassError WriteError AttributeEmpty Base lib/activeldap/base.rb ClassMethods Associations Configuration ActiveLDAP Module: ActiveLDAP

Methods

Public Instance methods

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'

[Source]

     # File lib/activeldap/associations.rb, line 107
107:     def belongs_to(association_id, options = {})
108:       klass = options[:class_name] || association_id.to_s
109:       key = options[:foreign_key]  || association_id.to_s + "_id"
110:       local_key = options[:local_key] || ''
111:       class_eval "def \#{association_id}(objects = nil)\nobjects = @@config[:return_objects] if objects.nil?\nlocal_key = \"\#{local_key}\"\nlocal_key = dnattr() if local_key.empty?\nresults = []\n\#{klass}.find_all(:attribute => \"\#{key}\", :value => send(local_key.to_sym), :objects => objects).each do |o|\nresults << o\nend\nreturn results\nend\n"
112:     end

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[ENH]: def #{…}=(val) to redefine group membership

[Source]

     # File lib/activeldap/associations.rb, line 138
138:     def has_many(association_id, options = {})
139:       klass = options[:class_name] || association_id.to_s
140:       key = options[:local_key]  || association_id.to_s + "_id"
141:       foreign_key = options[:foreign_key] || ''
142:       class_eval "def \#{association_id}(objects = nil)\nobjects = @@config[:return_objects] if objects.nil?\nforeign_key = \"\#{foreign_key}\"\nif foreign_key.empty?\nforeign_key = dnattr()\nend\nresults = []\nunless @data[\"\#{key}\"].nil?\n@data[\"\#{key}\"].each do |item|\nfkey = \"\"\nif foreign_key == \"dn\" and not item.empty?\nfkey = item.split(',')[0].split('=')[0]\nitem = item.split(',')[0].split('=')[1]\nend\n# This will even yield entries that don't necessarily exist\nif foreign_key != \"dn\"\nfkey = foreign_key\nend\n\#{klass}.find_all(:attribute => fkey, :value => item, :objects => objects).each do |match|\nresults << match\nend\nend\nend\nreturn results\nend\n"
143:     end

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

[Source]

    # File lib/activeldap/associations.rb, line 22
22:      def ldap_mapping(options = {})
23:        # The immediate ancestor should be the caller....
24:        klass = self.ancestors[0]
25: 
26:        dnattr = options[:dnattr] || 'cn'
27:        prefix = options[:prefix] || "ou=#{klass.to_s.split(':').last}"
28:        classes_array = options[:classes] || nil
29:        scope = options[:scope] || 'super'
30:        # When used, instantiates parent objects from the "parent dn". This
31:        # can be a String or a real ActiveLDAP class. This just adds the helper 
32:        # Base#parent.
33:        parent = options[:parent_class] || nil
34: 
35:        classes = 'super'
36:        unless classes_array.nil?
37:          raise TypeError, ":classes must be an array" \
38:            unless classes_array.respond_to? :size
39:          # Build classes array
40:          classes = '['
41:          classes_array.map! {|x| x = "'#{x}'"}
42:          classes << classes_array.join(', ')
43:          classes << ']'
44:        end
45: 
46:        # This adds the methods to the local
47:        # class which can then be inherited, etc
48:        # which describe the mapping to LDAP.
49:        klass.class_eval("class << self\n# Return the list of required object classes\ndef required_classes\n\#{classes}\nend\n\n# Return the full base of the class\ndef base\nif \"\#{prefix}\".empty?\nreturn \"\\\#{super}\"\nelse\nreturn \"\#{prefix},\\\#{super}\"\nend\nend\n\n# Return the expected DN attribute of an object\ndef dnattr\n'\#{dnattr}'\nend\n\n# Return the expected DN attribute of an object\ndef ldap_scope\n\#{scope}\nend\nend\n\n# Hide connect\nprivate_class_method :connect\n\n# Unhide class methods\npublic_class_method :find_all\npublic_class_method :find\npublic_class_method :new\npublic_class_method :dnattr\n")
50: 
51:       # Add the parent helper if desired
52:       if parent
53:         klass.class_eval("def parent()\nreturn \#{parent}.new(@dn.split(',')[1..-1].join(','))\nend\n")
54:       end
55:      end

[Validate]