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'


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/activeldap/associations.rb', line 73

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 <<-"end_eval"
    def #{association_id}(objects = true)
      local_key = "#{local_key}"
      local_key = dnattr() if local_key.empty?
      return #{klass}.find_all(:attribute => "#{key}", :value => send(local_key.to_sym), :objects => objects)
    end
  end_eval
end

#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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/activeldap/associations.rb', line 98

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 <<-"end_eval"
    def #{association_id}(objects = true)
      foreign_key = "#{foreign_key}"
      if foreign_key.empty?
        foreign_key = dnattr()
      end
      results = []
      @data["#{key}"].each do |item|
        # This will even yield entries that don't necessarily exist
        #{klass}.find_all(:attribute => foreign_key, :value => item, :objects => objects).each do |match|
          results << match
        end
      end
      return results
    end
  end_eval
end

#ldap_mapping(options = {}) ⇒ Object

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

Raises:

  • (TypeError)


16
17
18
19
20
21
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
# File 'lib/activeldap/associations.rb', line 16

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] || ['top']

  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 << ']'

  # This adds the methods to the local
  # class which can then be inherited, etc
  # which describe the mapping to LDAP.
  klass.class_eval <<-"end_eval"
    class << #{klass}
      # Return the list of required object classes
      def required_classes
        #{classes}
      end

      # Return the full base of the class
      def base
        "#{prefix},\#{super}"
      end

      # Return the expected DN attribute of an object
      def dnattr
        '#{dnattr}'
      end
    end

    # Hide connect
    private_class_method :connect

    # Unhide class methods
    public_class_method :find_all
    public_class_method :find
    public_class_method :new
    public_class_method :dnattr
 end_eval
end