Class: ActiveLdap::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/active_ldap/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(entries) ⇒ Schema

Returns a new instance of Schema.



3
4
5
6
7
8
# File 'lib/active_ldap/schema.rb', line 3

def initialize(entries)
  @entries = entries
  @schema_info = {}
  @class_attributes_info = {}
  @cache = {}
end

Instance Method Details

#attribute(group, id_or_name, attribute_name) ⇒ Object Also known as: [], attr

attribute

This is just like LDAP::Schema#attribute except that it allows look up in any of the given keys. e.g.

attribute('attributeTypes', 'cn', 'DESC')
attribute('ldapSyntaxes', '1.3.6.1.4.1.1466.115.121.1.5', 'DESC')


25
26
27
28
29
30
# File 'lib/active_ldap/schema.rb', line 25

def attribute(group, id_or_name, attribute_name)
  return [] if attribute_name.empty?
  attribute_name = normalize_attribute_name(attribute_name)
  value = attributes(group, id_or_name)[attribute_name]
  value ? value.dup : []
end

#attribute_aliases(name) ⇒ Object

attribute_aliases

Returns all names from the LDAP schema for the attribute given.



70
71
72
73
74
# File 'lib/active_ldap/schema.rb', line 70

def attribute_aliases(name)
  cache([:attribute_aliases, name]) do
    attribute_type(name, 'NAME')
  end
end

#attributes(group, id_or_name) ⇒ Object



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
# File 'lib/active_ldap/schema.rb', line 34

def attributes(group, id_or_name)
  return {} if group.empty? or id_or_name.empty?

  # Initialize anything that is required
  info, ids, aliases = ensure_schema_info(group)
  id, name = determine_id_or_name(id_or_name, aliases)

  # Check already parsed options first
  return ids[id] if ids.has_key?(id)

  while schema = @entries[group].shift
    next unless /\A\s*\(\s*([\d\.]+)\s*(.*)\s*\)\s*\z/ =~ schema
    schema_id = $1
    rest = $2
    next if ids.has_key?(schema_id)

    attributes = {}
    ids[schema_id] = attributes

    parse_attributes(rest, attributes)
    (attributes["NAME"] || []).each do |v|
      normalized_name = normalize_schema_name(v)
      aliases[normalized_name] = schema_id
      id = schema_id if id.nil? and name == normalized_name
    end

    break if id == schema_id
  end

  ids[id || aliases[name]] || {}
end

#binary?(name) ⇒ Boolean

binary?

Returns true if the given attribute’s syntax is X-NOT-HUMAN-READABLE or X-BINARY-TRANSFER-REQUIRED

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
# File 'lib/active_ldap/schema.rb', line 101

def binary?(name)
  cache([:binary?, name]) do
    # Get syntax OID
    syntax = attribute_type(name, 'SYNTAX')[0]
    !syntax.nil? and
      (ldap_syntax(syntax, 'X-NOT-HUMAN-READABLE') == ["TRUE"] or
       ldap_syntax(syntax, 'X-BINARY-TRANSFER-REQUIRED') == ["TRUE"])
  end
end

#binary_required?(name) ⇒ Boolean

binary_required?

Returns true if the value MUST be transferred in binary

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
# File 'lib/active_ldap/schema.rb', line 114

def binary_required?(name)
  cache([:binary_required?, name]) do
    # Get syntax OID
    syntax = attribute_type(name, 'SYNTAX')[0]
    !syntax.nil? and
      ldap_syntax(syntax, 'X-BINARY-TRANSFER-REQUIRED') == ["TRUE"]
  end
end

#class_attributes(objc) ⇒ Object

class_attributes

Returns an Array of all the valid attributes (but not with full aliases) for the given objectClass



127
128
129
130
131
132
133
134
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
167
168
169
# File 'lib/active_ldap/schema.rb', line 127

def class_attributes(objc)
  cache([:class_attributes, objc]) do
    # First get all the current level attributes
    must = object_class(objc, 'MUST')
    may = object_class(objc, 'MAY')

    # Now add all attributes from the parent object (SUPerclasses)
    # Hopefully an iterative approach will be pretty speedy
    # 1. build complete list of SUPs
    # 2. Add attributes from each
    sups = object_class(objc, 'SUP')
    loop do
      start_size = sups.size
      new_sups = []
      sups.each do |sup|
        new_sups.concat(object_class(sup, 'SUP'))
      end

      sups.concat(new_sups)
      sups.uniq!
      break if sups.size == start_size
    end
    sups.each do |sup|
      must.concat(object_class(sup, 'MUST'))
      may.concat(object_class(sup, 'MAY'))
    end

    # Clean out the dupes.
    must.uniq!
    may.uniq!
    if objc == "inetOrgPerson"
      may.collect! do |name|
        if name == "x500uniqueIdentifier"
          "x500UniqueIdentifier"
        else
          name
        end
      end
    end

    {:must => must, :may => may}
  end
end

#exist_name?(group, name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_ldap/schema.rb', line 14

def exist_name?(group, name)
  alias_map(group).has_key?(normalize_schema_name(name))
end

#names(group) ⇒ Object



10
11
12
# File 'lib/active_ldap/schema.rb', line 10

def names(group)
  alias_map(group).keys
end

#read_only?(name) ⇒ Boolean

read_only?

Returns true if an attribute is read-only NO-USER-MODIFICATION

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/active_ldap/schema.rb', line 80

def read_only?(name)
  cache([:read_only?, name]) do
    attribute_type(name, 'NO-USER-MODIFICATION')[0] == 'TRUE'
  end
end

#single_value?(name) ⇒ Boolean

single_value?

Returns true if an attribute can only have one value defined SINGLE-VALUE

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/active_ldap/schema.rb', line 91

def single_value?(name)
  cache([:single_value?, name]) do
    attribute_type(name, 'SINGLE-VALUE')[0] == 'TRUE'
  end
end