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'


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/activeldap/associations.rb', line 77

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?
      results = []
      #{klass}.find_all(:attribute => "#{key}", :value => send(local_key.to_sym), :objects => objects).each do |o|
        results << o
      end
      return results
    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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/activeldap/associations.rb', line 106

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 = []
      unless @data["#{key}"].nil?
        @data["#{key}"].each do |item|
          fkey = ""
          if foreign_key == "dn" and not item.empty?
            fkey = item.split(',')[0].split('=')[0]
            item = item.split(',')[0].split('=')[1]
          end
          # This will even yield entries that don't necessarily exist
          if foreign_key != "dn"
            fkey = foreign_key
          end
          #{klass}.find_all(:attribute => fkey, :value => item, :objects => objects).each do |match|
            results << match
          end
        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
63
64
65
66
# 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 << self
      # Return the list of required object classes
      def required_classes
        #{classes}
      end

      # Return the full base of the class
      def base
        if "#{prefix}".empty?
          return "\#{super}"
        else
          return "#{prefix},\#{super}"
        end
      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