Class: UCB::LDAP::SimpleEntry
- Inherits:
-
Object
- Object
- UCB::LDAP::SimpleEntry
- Defined in:
- lib/ucb_simple_ldap_entry.rb
Class Method Summary collapse
-
.canonical(string_or_symbol) ⇒ Object
Returns the canonical representation of a symbol or string so we can look up attributes in a number of ways.
-
.create(args) ⇒ Object
Creates and returns new entry.
-
.create!(args) ⇒ Object
Same as #create(), but raises DirectoryNotUpdated on failure.
-
.net_ldap ⇒ Object
Returns underlying Net::LDAP instance.
-
.object_classes ⇒ Object
Returns
Array
of object classes making up this type of LDAP entity. - .schema_attribute(attribute_name) ⇒ Object
-
.schema_attributes_array ⇒ Object
Returns an
Array
of Schema::Attribute for the entity. -
.schema_attributes_hash ⇒ Object
Returns as
Hash
whose keys are the canonical attribute names and whose values are the corresponding Schema::Attributes. -
.set_schema_attributes ⇒ Object
Want an array of Schema::Attributes as well as a hash of all possible variations on a name pointing to correct array element.
-
.tree_base ⇒ Object
Can be overridden in #search by passing in a
:base
parm. - .tree_base=(tree_base) ⇒ Object
- .unique_object_class ⇒ Object
Instance Method Summary collapse
- #assigned_attributes ⇒ Object
-
#attributes ⇒ Object
Hash
of attributes returned from underlying NET::LDAP::Entry instance. -
#canonical(string_or_symbol) ⇒ Object
:nodoc:.
-
#dn ⇒ Object
Returns the value of the Distinguished Name attribute.
-
#initialize(dn) ⇒ SimpleEntry
constructor
Returns new instance of UCB::LDAP::Entry.
-
#method_missing(method, *args) ⇒ Object
Used to get/set attribute values.
- #net_ldap ⇒ Object
-
#setter_method?(method) ⇒ Boolean
Returns
true
if method is a “setter”, i.e., ends in “=”. -
#value_getter(method) ⇒ Object
Called by method_missing() to get an attribute value.
-
#value_setter(method, *args) ⇒ Object
Called by method_missing() to set an attribute value.
Constructor Details
#initialize(dn) ⇒ SimpleEntry
Returns new instance of UCB::LDAP::Entry. The argument net_ldap_entry is an instance of Net::LDAP::Entry.
You should not need to create any UCB::LDAP::Entry instances; they are created by calls to UCB::LDAP.search and friends.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ucb_simple_ldap_entry.rb', line 11 def initialize(dn) #:nodoc: # #auth = {:username=>"cn=greedybuddha", :method=>:simple, :password=>"wig0gin"} #ldap = Net::LDAP.new(:host => '0.0.0.0', :port => 1389, :auth => auth) #dn = "cn=Steven Hansen,ou=people,dc=berkeley,dc=edu" # #attr = { # :objectclass => [ # "inetorgperson", # ], # :cn => "Steven Hansen", # :uid => '61065', # :sn => "Hansen", #} # # Don't store Net::LDAP entry in object since it uses the block # initialization method of Hash which can't be marshalled ... this # means it can't be stored in a Rails session. @attributes = {} net_ldap_entry.each do |attr, value| @attributes[canonical(attr)] = value.map{|v| v.dup} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Used to get/set attribute values.
If we can’t make an attribute name out of method, let regular method_missing() handle it.
69 70 71 72 73 |
# File 'lib/ucb_simple_ldap_entry.rb', line 69 def method_missing(method, *args) #:nodoc: setter_method?(method) ? value_setter(method, *args) : value_getter(method) rescue BadAttributeNameException return super end |
Class Method Details
.canonical(string_or_symbol) ⇒ Object
Returns the canonical representation of a symbol or string so we can look up attributes in a number of ways.
161 162 163 |
# File 'lib/ucb_simple_ldap_entry.rb', line 161 def canonical(string_or_symbol) string_or_symbol.to_s.downcase.to_sym end |
.create(args) ⇒ Object
Creates and returns new entry. Returns false
if unsuccessful. Sets :objectclass key of args[:attributes] to object_classes read from schema.
dn = "uid=999999,ou=people,dc=example,dc=com"
attr = {
:uid => "999999",
:mail => "[email protected]"
}
EntrySubClass.create(:dn => dn, :attributes => attr) #=> #<UCB::LDAP::EntrySubClass ..>
Caller is responsible for setting :dn and :attributes correctly, as well as any other validation.
118 119 120 121 122 123 124 |
# File 'lib/ucb_simple_ldap_entry.rb', line 118 def create(args) args[:attributes][:objectclass] = object_classes net_ldap.add(args) or return false # Why is the object being refetched from LDAP here? find_by_dn(args[:dn]) end |
.create!(args) ⇒ Object
Same as #create(), but raises DirectoryNotUpdated on failure.
127 128 129 |
# File 'lib/ucb_simple_ldap_entry.rb', line 127 def create!(args) create(args) || raise(DirectoryNotUpdatedException) end |
.net_ldap ⇒ Object
Returns underlying Net::LDAP instance.
166 167 168 |
# File 'lib/ucb_simple_ldap_entry.rb', line 166 def net_ldap #:nodoc: UCB::LDAP.net_ldap end |
.object_classes ⇒ Object
Returns Array
of object classes making up this type of LDAP entity.
132 133 134 |
# File 'lib/ucb_simple_ldap_entry.rb', line 132 def object_classes @object_classes ||= UCB::LDAP::Schema.schema_hash[entity_name]["objectClasses"] end |
.schema_attribute(attribute_name) ⇒ Object
153 154 155 156 |
# File 'lib/ucb_simple_ldap_entry.rb', line 153 def schema_attribute(attribute_name) schema_attributes_hash[canonical(attribute_name)] || raise(BadAttributeNameException, "'#{attribute_name}' is not a recognized attribute name") end |
.schema_attributes_array ⇒ Object
Returns an Array
of Schema::Attribute for the entity.
141 142 143 144 |
# File 'lib/ucb_simple_ldap_entry.rb', line 141 def schema_attributes_array @schema_attributes_array || set_schema_attributes @schema_attributes_array end |
.schema_attributes_hash ⇒ Object
Returns as Hash
whose keys are the canonical attribute names and whose values are the corresponding Schema::Attributes.
148 149 150 151 |
# File 'lib/ucb_simple_ldap_entry.rb', line 148 def schema_attributes_hash @schema_attributes_hash || set_schema_attributes @schema_attributes_hash end |
.set_schema_attributes ⇒ Object
Want an array of Schema::Attributes as well as a hash of all possible variations on a name pointing to correct array element.
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/ucb_simple_ldap_entry.rb', line 175 def set_schema_attributes @schema_attributes_array = [] @schema_attributes_hash = {} UCB::LDAP::Schema.schema_hash[entity_name]["attributes"].each do |k, v| sa = UCB::LDAP::Schema::Attribute.new(v.merge("name" => k)) @schema_attributes_array << sa [sa.name, sa.aliases].flatten.each do |name| @schema_attributes_hash[canonical(name)] = sa end end rescue raise "Error loading schema attributes for entity_name '#{entity_name}'" end |
.tree_base ⇒ Object
Can be overridden in #search by passing in a :base
parm.
190 191 192 |
# File 'lib/ucb_simple_ldap_entry.rb', line 190 def tree_base @tree_base end |
.tree_base=(tree_base) ⇒ Object
194 195 196 |
# File 'lib/ucb_simple_ldap_entry.rb', line 194 def tree_base=(tree_base) @tree_base = tree_base end |
.unique_object_class ⇒ Object
136 137 138 |
# File 'lib/ucb_simple_ldap_entry.rb', line 136 def unique_object_class @unique_object_class ||= UCB::LDAP::Schema.schema_hash[entity_name]["uniqueObjectClass"] end |
Instance Method Details
#assigned_attributes ⇒ Object
94 95 96 |
# File 'lib/ucb_simple_ldap_entry.rb', line 94 def assigned_attributes @assigned_attributes ||= {} end |
#attributes ⇒ Object
Hash
of attributes returned from underlying NET::LDAP::Entry instance. Hash keys are #canonical attribute names, hash values are attribute values as returned from LDAP, i.e. arrays.
You should most likely be referencing attributes as if they were instance methods rather than directly through this method. See top of this document.
46 47 48 |
# File 'lib/ucb_simple_ldap_entry.rb', line 46 def attributes @attributes end |
#canonical(string_or_symbol) ⇒ Object
:nodoc:
55 56 57 |
# File 'lib/ucb_simple_ldap_entry.rb', line 55 def canonical(string_or_symbol) #:nodoc: self.class.canonical(string_or_symbol) end |
#dn ⇒ Object
Returns the value of the Distinguished Name attribute.
51 52 53 |
# File 'lib/ucb_simple_ldap_entry.rb', line 51 def dn attributes[canonical(:dn)] end |
#net_ldap ⇒ Object
59 60 61 |
# File 'lib/ucb_simple_ldap_entry.rb', line 59 def net_ldap self.class.net_ldap end |
#setter_method?(method) ⇒ Boolean
Returns true
if method is a “setter”, i.e., ends in “=”.
76 77 78 |
# File 'lib/ucb_simple_ldap_entry.rb', line 76 def setter_method?(method) method.to_s[-1, 1] == "=" end |
#value_getter(method) ⇒ Object
Called by method_missing() to get an attribute value.
81 82 83 84 85 |
# File 'lib/ucb_simple_ldap_entry.rb', line 81 def value_getter(method) schema_attribute = self.class.schema_attribute(method) raw_value = attributes[canonical(schema_attribute.name)] schema_attribute.get_value(raw_value) end |
#value_setter(method, *args) ⇒ Object
Called by method_missing() to set an attribute value.
88 89 90 91 92 |
# File 'lib/ucb_simple_ldap_entry.rb', line 88 def value_setter(method, *args) schema_attribute = self.class.schema_attribute(method.to_s.chop) attr_key = canonical(schema_attribute.name) assigned_attributes[attr_key] = schema_attribute.ldap_value(args[0]) end |