ucb_ldap 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +0 -1
- data/Rakefile +2 -2
- data/lib/ucb_ldap.rb +49 -19
- data/lib/ucb_ldap_entry.rb +36 -56
- data/ucb_ldap.gemspec +7 -7
- data/version.yml +1 -1
- metadata +5 -4
- data/lib/ucb_simple_ldap_entry.rb +0 -201
data/Manifest
CHANGED
data/Rakefile
CHANGED
@@ -3,14 +3,14 @@ require 'rake'
|
|
3
3
|
require 'echoe'
|
4
4
|
require 'hanna/rdoctask'
|
5
5
|
|
6
|
-
Echoe.new('ucb_ldap', '1.3.
|
6
|
+
Echoe.new('ucb_ldap', '1.3.2') do |p|
|
7
7
|
p.description = "Convenience classes for interacing with UCB's LDAP directory"
|
8
8
|
p.url = "http://ucbrb.rubyforge.org/ucb_ldap"
|
9
9
|
p.author = "Steven Hansen, Steve Downey, Lucas Rockwell"
|
10
10
|
p.email = "runner@berkeley.edu"
|
11
11
|
p.ignore_pattern = ["svn_user.yml", "tasks/ucb_ldap.rake", "spec/**/**", "test/**/**"]
|
12
12
|
p.project = "ucbrb"
|
13
|
-
p.runtime_dependencies = "ruby-net-ldap >=0.0.4"
|
13
|
+
p.runtime_dependencies = ["ruby-net-ldap >= 0.0.4"]
|
14
14
|
p.rdoc_options = "-o doc --inline-source -T hanna lib/*.rb"
|
15
15
|
p.rdoc_pattern = ["README", "lib/**/**"]
|
16
16
|
end
|
data/lib/ucb_ldap.rb
CHANGED
@@ -21,7 +21,6 @@ require 'ucb_ldap_service'
|
|
21
21
|
|
22
22
|
|
23
23
|
module UCB #:nodoc:
|
24
|
-
|
25
24
|
# =UCB::LDAP
|
26
25
|
#
|
27
26
|
# <b>If you are doing searches that don't require a privileged bind
|
@@ -35,12 +34,10 @@ module UCB #:nodoc:
|
|
35
34
|
# and other methods of UCB::LDAP::Entry and its sub-classes.
|
36
35
|
#
|
37
36
|
module LDAP
|
38
|
-
|
39
|
-
|
37
|
+
|
40
38
|
HOST_PRODUCTION = 'ldap.berkeley.edu'
|
41
39
|
HOST_TEST = 'ldap-test.berkeley.edu'
|
42
40
|
|
43
|
-
|
44
41
|
# class methods
|
45
42
|
class << self
|
46
43
|
|
@@ -54,12 +51,12 @@ module UCB #:nodoc:
|
|
54
51
|
end
|
55
52
|
|
56
53
|
# Removes current bind (username, password).
|
57
|
-
def clear_authentication
|
54
|
+
def clear_authentication
|
58
55
|
authenticate(nil, nil)
|
59
56
|
end
|
60
57
|
|
61
58
|
# Returns LDAP host used for lookups. Default is HOST_PRODUCTION.
|
62
|
-
def host
|
59
|
+
def host
|
63
60
|
@host || HOST_PRODUCTION
|
64
61
|
end
|
65
62
|
|
@@ -85,15 +82,15 @@ module UCB #:nodoc:
|
|
85
82
|
#
|
86
83
|
# Note: callers should not cache the results of this call unless they
|
87
84
|
# are prepared to handle timed-out connections (which this method does).
|
88
|
-
def net_ldap
|
89
|
-
@net_ldap
|
85
|
+
def net_ldap
|
86
|
+
connection_open? ? @net_ldap : new_net_ldap
|
90
87
|
end
|
91
88
|
|
92
|
-
def password
|
89
|
+
def password #:nodoc:
|
93
90
|
@password
|
94
91
|
end
|
95
92
|
|
96
|
-
def username
|
93
|
+
def username #:nodoc:
|
97
94
|
@username
|
98
95
|
end
|
99
96
|
|
@@ -140,30 +137,63 @@ module UCB #:nodoc:
|
|
140
137
|
private unless $TESTING
|
141
138
|
|
142
139
|
# The value of the :auth parameter for Net::LDAP.new().
|
143
|
-
def authentication_information
|
140
|
+
def authentication_information
|
144
141
|
password.nil? ?
|
145
142
|
{:method => :anonymous} :
|
146
143
|
{:method => :simple, :username => username, :password => password}
|
147
144
|
end
|
148
145
|
|
149
|
-
# Returns
|
150
|
-
#
|
151
|
-
|
152
|
-
|
153
|
-
|
146
|
+
# Returns +true+ if we have a Net::LDAP instance with an open
|
147
|
+
# connection.
|
148
|
+
def connection_open?
|
149
|
+
@net_ldap.nil? ? false : ldap_ping
|
150
|
+
rescue
|
151
|
+
false
|
152
|
+
end
|
153
|
+
|
154
|
+
# Returns +true+ if connection simple search works.
|
155
|
+
def ldap_ping
|
156
|
+
search_attrs = {
|
157
|
+
:base => "",
|
158
|
+
:scope => Net::LDAP::SearchScope_BaseObject,
|
159
|
+
:attributes => [1.1]
|
160
|
+
}
|
161
|
+
result = false
|
162
|
+
@net_ldap.search(search_attrs){result = true}
|
163
|
+
result
|
164
|
+
end
|
165
|
+
|
166
|
+
# Returns new Net::LDAP instance. Also
|
167
|
+
# reaches into the Net::LDAP to set the @open_connection instance
|
168
|
+
# variable.
|
169
|
+
#
|
170
|
+
# Warning: this seems to be contrary to the Net::LDAP author's
|
171
|
+
# intent and may break with future versions of Net::LDAP.
|
154
172
|
def new_net_ldap()
|
155
173
|
@net_ldap = Net::LDAP.new(
|
156
174
|
:host => host,
|
157
175
|
:auth => authentication_information,
|
158
176
|
:port => 636,
|
159
177
|
:encryption => {:method =>:simple_tls}
|
160
|
-
|
161
|
-
|
178
|
+
)
|
179
|
+
@net_ldap.instance_variable_set(:@open_connection, new_net_ldap_connection)
|
180
|
+
@net_ldap.bind || raise(BindFailedException)
|
162
181
|
@net_ldap
|
163
182
|
end
|
164
183
|
|
184
|
+
# Return a new Net::LDAP::Connection
|
185
|
+
def new_net_ldap_connection
|
186
|
+
Net::LDAP::Connection.new(
|
187
|
+
:host => host,
|
188
|
+
:port => 636,
|
189
|
+
:encryption => {:method => :simple_tls}
|
190
|
+
)
|
191
|
+
rescue Net::LDAP::LdapError
|
192
|
+
raise UCB::LDAP::ConnectionFailedException
|
193
|
+
end
|
194
|
+
|
165
195
|
# Used for testing
|
166
|
-
def clear_instance_variables
|
196
|
+
def clear_instance_variables
|
167
197
|
@host = nil
|
168
198
|
@net_ldap = nil
|
169
199
|
@username = nil
|
data/lib/ucb_ldap_entry.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
module UCB
|
3
2
|
module LDAP
|
4
3
|
# = UCB::LDAP::Entry
|
@@ -32,7 +31,7 @@ module UCB
|
|
32
31
|
#
|
33
32
|
# Entry subclasses may have convenience
|
34
33
|
# methods that return scalars even though the schema defines
|
35
|
-
# the unerlying attribute as multi-valued becuase in practice
|
34
|
+
# the unerlying attribute as multi-valued becuase in practice the are single-valued.
|
36
35
|
#
|
37
36
|
# === Attribute Types
|
38
37
|
#
|
@@ -82,31 +81,14 @@ module UCB
|
|
82
81
|
#
|
83
82
|
# You should not need to create any UCB::LDAP::Entry instances;
|
84
83
|
# they are created by calls to UCB::LDAP.search and friends.
|
85
|
-
def initialize(
|
86
|
-
@new_record = true
|
87
|
-
@attributes = {}
|
88
|
-
@tainted_attributes = {}
|
89
|
-
end
|
90
|
-
|
91
|
-
def new_record?
|
92
|
-
@new_record
|
93
|
-
end
|
94
|
-
|
95
|
-
# Hydrates (populates) the object with values from the ldap resultset.
|
96
|
-
def self.hydrate(net_ldap_entry)
|
97
|
-
new_ldap_entry = self.new
|
98
|
-
new_ldap_entry.instance_variable_set(:@new_record, false)
|
84
|
+
def initialize(net_ldap_entry) #:nodoc:
|
99
85
|
# Don't store Net::LDAP entry in object since it uses the block
|
100
86
|
# initialization method of Hash which can't be marshalled ... this
|
101
87
|
# means it can't be stored in a Rails session.
|
88
|
+
@attributes = {}
|
102
89
|
net_ldap_entry.each do |attr, value|
|
103
|
-
|
90
|
+
@attributes[canonical(attr)] = value.map{|v| v.dup}
|
104
91
|
end
|
105
|
-
new_ldap_entry
|
106
|
-
end
|
107
|
-
|
108
|
-
def tainted_attributes
|
109
|
-
@tainted_attributes
|
110
92
|
end
|
111
93
|
|
112
94
|
# <tt>Hash</tt> of attributes returned from underlying NET::LDAP::Entry
|
@@ -120,7 +102,7 @@ module UCB
|
|
120
102
|
@attributes
|
121
103
|
end
|
122
104
|
|
123
|
-
#
|
105
|
+
# Returns the value of the <em>Distinguished Name</em> attribute.
|
124
106
|
def dn
|
125
107
|
attributes[canonical(:dn)]
|
126
108
|
end
|
@@ -129,13 +111,13 @@ module UCB
|
|
129
111
|
self.class.canonical(string_or_symbol)
|
130
112
|
end
|
131
113
|
|
132
|
-
#
|
114
|
+
# Update an existing entry. Returns entry if successful else false.
|
133
115
|
#
|
134
116
|
# attrs = {:attr1 => "new_v1", :attr2 => "new_v2"}
|
135
117
|
# entry.update_attributes(attrs)
|
136
118
|
#
|
137
119
|
def update_attributes(attrs)
|
138
|
-
attrs.each
|
120
|
+
attrs.each{|k, v| self.send("#{k}=", v)}
|
139
121
|
if modify()
|
140
122
|
@attributes = self.class.find_by_dn(dn).attributes.dup
|
141
123
|
return true
|
@@ -143,19 +125,19 @@ module UCB
|
|
143
125
|
false
|
144
126
|
end
|
145
127
|
|
146
|
-
#
|
128
|
+
# Same as #update_attributes(), but raises DirectoryNotUpdated on failure.
|
147
129
|
def update_attributes!(attrs)
|
148
|
-
update_attributes(attrs) || raise(
|
130
|
+
update_attributes(attrs) || raise(DirectoryNotUpdatedException)
|
149
131
|
end
|
150
132
|
|
151
|
-
#
|
133
|
+
# Delete entry. Returns +true+ on sucess, +false+ on failure.
|
152
134
|
def delete
|
153
135
|
net_ldap.delete(:dn => dn)
|
154
136
|
end
|
155
137
|
|
156
|
-
#
|
138
|
+
# Same as #delete() except raises DirectoryNotUpdated on failure.
|
157
139
|
def delete!
|
158
|
-
delete || raise(
|
140
|
+
delete || raise(DirectoryNotUpdatedException)
|
159
141
|
end
|
160
142
|
|
161
143
|
def net_ldap
|
@@ -163,11 +145,11 @@ module UCB
|
|
163
145
|
end
|
164
146
|
|
165
147
|
|
166
|
-
private unless $
|
148
|
+
private unless $TESTING
|
167
149
|
|
168
|
-
#
|
150
|
+
# Used to get/set attribute values.
|
169
151
|
#
|
170
|
-
#
|
152
|
+
# If we can't make an attribute name out of method, let
|
171
153
|
# regular method_missing() handle it.
|
172
154
|
def method_missing(method, *args) #:nodoc:
|
173
155
|
setter_method?(method) ? value_setter(method, *args) : value_getter(method)
|
@@ -175,29 +157,33 @@ module UCB
|
|
175
157
|
return super
|
176
158
|
end
|
177
159
|
|
178
|
-
#
|
160
|
+
# Returns +true+ if _method_ is a "setter", i.e., ends in "=".
|
179
161
|
def setter_method?(method)
|
180
162
|
method.to_s[-1, 1] == "="
|
181
163
|
end
|
182
164
|
|
183
|
-
#
|
165
|
+
# Called by method_missing() to get an attribute value.
|
184
166
|
def value_getter(method)
|
185
167
|
schema_attribute = self.class.schema_attribute(method)
|
186
168
|
raw_value = attributes[canonical(schema_attribute.name)]
|
187
169
|
schema_attribute.get_value(raw_value)
|
188
170
|
end
|
189
171
|
|
190
|
-
#
|
172
|
+
# Called by method_missing() to set an attribute value.
|
191
173
|
def value_setter(method, *args)
|
192
174
|
schema_attribute = self.class.schema_attribute(method.to_s.chop)
|
193
175
|
attr_key = canonical(schema_attribute.name)
|
194
|
-
|
176
|
+
assigned_attributes[attr_key] = schema_attribute.ldap_value(args[0])
|
177
|
+
end
|
178
|
+
|
179
|
+
def assigned_attributes
|
180
|
+
@assigned_attributes ||= {}
|
195
181
|
end
|
196
182
|
|
197
183
|
def modify_operations
|
198
184
|
ops = []
|
199
|
-
|
200
|
-
value =
|
185
|
+
assigned_attributes.keys.sort_by{|k| k.to_s}.each do |key|
|
186
|
+
value = assigned_attributes[key]
|
201
187
|
op = value.nil? ? :delete : :replace
|
202
188
|
ops << [op, key, value]
|
203
189
|
end
|
@@ -205,20 +191,20 @@ module UCB
|
|
205
191
|
end
|
206
192
|
|
207
193
|
def modify()
|
208
|
-
if
|
209
|
-
@
|
194
|
+
if UCB::LDAP.net_ldap.modify(:dn => dn, :operations => modify_operations)
|
195
|
+
@assigned_attributes = nil
|
210
196
|
return true
|
211
197
|
end
|
212
198
|
false
|
213
199
|
end
|
214
200
|
|
215
|
-
#
|
201
|
+
# Class methods
|
216
202
|
class << self
|
217
203
|
|
218
204
|
public
|
219
205
|
|
220
|
-
#
|
221
|
-
#
|
206
|
+
# Creates and returns new entry. Returns +false+ if unsuccessful.
|
207
|
+
# Sets :objectclass key of <em>args[:attributes]</em> to
|
222
208
|
# object_classes read from schema.
|
223
209
|
#
|
224
210
|
# dn = "uid=999999,ou=people,dc=example,dc=com"
|
@@ -227,24 +213,18 @@ module UCB
|
|
227
213
|
# :mail => "gsmith@example.com"
|
228
214
|
# }
|
229
215
|
#
|
230
|
-
#
|
216
|
+
# EntrySubClass.create(:dn => dn, :attributes => attr) #=> #<UCB::LDAP::EntrySubClass ..>
|
231
217
|
#
|
232
|
-
#
|
218
|
+
# Caller is responsible for setting :dn and :attributes correctly,
|
233
219
|
# as well as any other validation.
|
234
220
|
#
|
235
221
|
def create(args)
|
236
222
|
args[:attributes][:objectclass] = object_classes
|
237
223
|
net_ldap.add(args) or return false
|
238
|
-
|
239
|
-
# why is the object being refetched from ldap here?
|
240
224
|
find_by_dn(args[:dn])
|
241
225
|
end
|
242
|
-
|
243
|
-
|
244
|
-
schema_attributes_hash.delete_if {|key, value| value["required"] == false }.keys
|
245
|
-
end
|
246
|
-
|
247
|
-
# returns entry whose distinguised name is _dn_.
|
226
|
+
|
227
|
+
# Returns entry whose distinguised name is _dn_.
|
248
228
|
def find_by_dn(dn)
|
249
229
|
search(
|
250
230
|
:base => dn,
|
@@ -341,7 +321,7 @@ module UCB
|
|
341
321
|
|
342
322
|
results = []
|
343
323
|
net_ldap.search(args) do |entry|
|
344
|
-
results <<
|
324
|
+
results << new(entry)
|
345
325
|
end
|
346
326
|
results
|
347
327
|
end
|
@@ -363,7 +343,7 @@ module UCB
|
|
363
343
|
def entity_name
|
364
344
|
@entity_name
|
365
345
|
end
|
366
|
-
|
346
|
+
|
367
347
|
# Want an array of Schema::Attributes as well as a hash
|
368
348
|
# of all possible variations on a name pointing to correct array element.
|
369
349
|
def set_schema_attributes
|
data/ucb_ldap.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{ucb_ldap}
|
5
|
-
s.version = "1.3.
|
5
|
+
s.version = "1.3.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Steven Hansen, Steve Downey, Lucas Rockwell"]
|
9
|
-
s.date = %q{
|
9
|
+
s.date = %q{2009-02-09}
|
10
10
|
s.description = %q{Convenience classes for interacing with UCB's LDAP directory}
|
11
11
|
s.email = %q{runner@berkeley.edu}
|
12
|
-
s.extra_rdoc_files = ["README", "lib/person/adv_con_person.rb", "lib/person/affiliation_methods.rb", "lib/person/generic_attributes.rb", "lib/ucb_ldap.rb", "lib/ucb_ldap_address.rb", "lib/ucb_ldap_affiliation.rb", "lib/ucb_ldap_entry.rb", "lib/ucb_ldap_exceptions.rb", "lib/ucb_ldap_namespace.rb", "lib/ucb_ldap_org.rb", "lib/ucb_ldap_person.rb", "lib/ucb_ldap_person_job_appointment.rb", "lib/ucb_ldap_schema.rb", "lib/ucb_ldap_schema_attribute.rb", "lib/ucb_ldap_service.rb", "lib/ucb_ldap_student_term.rb"
|
13
|
-
s.files = ["CHANGELOG", "Manifest", "README", "Rakefile", "TODO", "init.rb", "lib/person/adv_con_person.rb", "lib/person/affiliation_methods.rb", "lib/person/generic_attributes.rb", "lib/ucb_ldap.rb", "lib/ucb_ldap_address.rb", "lib/ucb_ldap_affiliation.rb", "lib/ucb_ldap_entry.rb", "lib/ucb_ldap_exceptions.rb", "lib/ucb_ldap_namespace.rb", "lib/ucb_ldap_org.rb", "lib/ucb_ldap_person.rb", "lib/ucb_ldap_person_job_appointment.rb", "lib/ucb_ldap_schema.rb", "lib/ucb_ldap_schema_attribute.rb", "lib/ucb_ldap_service.rb", "lib/ucb_ldap_student_term.rb", "
|
12
|
+
s.extra_rdoc_files = ["README", "lib/person/adv_con_person.rb", "lib/person/affiliation_methods.rb", "lib/person/generic_attributes.rb", "lib/ucb_ldap.rb", "lib/ucb_ldap_address.rb", "lib/ucb_ldap_affiliation.rb", "lib/ucb_ldap_entry.rb", "lib/ucb_ldap_exceptions.rb", "lib/ucb_ldap_namespace.rb", "lib/ucb_ldap_org.rb", "lib/ucb_ldap_person.rb", "lib/ucb_ldap_person_job_appointment.rb", "lib/ucb_ldap_schema.rb", "lib/ucb_ldap_schema_attribute.rb", "lib/ucb_ldap_service.rb", "lib/ucb_ldap_student_term.rb"]
|
13
|
+
s.files = ["CHANGELOG", "Manifest", "README", "Rakefile", "TODO", "init.rb", "lib/person/adv_con_person.rb", "lib/person/affiliation_methods.rb", "lib/person/generic_attributes.rb", "lib/ucb_ldap.rb", "lib/ucb_ldap_address.rb", "lib/ucb_ldap_affiliation.rb", "lib/ucb_ldap_entry.rb", "lib/ucb_ldap_exceptions.rb", "lib/ucb_ldap_namespace.rb", "lib/ucb_ldap_org.rb", "lib/ucb_ldap_person.rb", "lib/ucb_ldap_person_job_appointment.rb", "lib/ucb_ldap_schema.rb", "lib/ucb_ldap_schema_attribute.rb", "lib/ucb_ldap_service.rb", "lib/ucb_ldap_student_term.rb", "schema/schema.yml", "version.yml", "ucb_ldap.gemspec"]
|
14
14
|
s.has_rdoc = true
|
15
15
|
s.homepage = %q{http://ucbrb.rubyforge.org/ucb_ldap}
|
16
16
|
s.rdoc_options = ["-o doc --inline-source -T hanna lib/*.rb"]
|
@@ -24,14 +24,14 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.specification_version = 2
|
25
25
|
|
26
26
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
-
s.add_runtime_dependency(%q<ruby-net-ldap>, [">= 0.0.4"])
|
27
|
+
s.add_runtime_dependency(%q<ruby-net-ldap>, [">= 0", "= 0.0.4"])
|
28
28
|
s.add_development_dependency(%q<echoe>, [">= 0"])
|
29
29
|
else
|
30
|
-
s.add_dependency(%q<ruby-net-ldap>, [">= 0.0.4"])
|
30
|
+
s.add_dependency(%q<ruby-net-ldap>, [">= 0", "= 0.0.4"])
|
31
31
|
s.add_dependency(%q<echoe>, [">= 0"])
|
32
32
|
end
|
33
33
|
else
|
34
|
-
s.add_dependency(%q<ruby-net-ldap>, [">= 0.0.4"])
|
34
|
+
s.add_dependency(%q<ruby-net-ldap>, [">= 0", "= 0.0.4"])
|
35
35
|
s.add_dependency(%q<echoe>, [">= 0"])
|
36
36
|
end
|
37
37
|
end
|
data/version.yml
CHANGED
@@ -1 +1 @@
|
|
1
|
-
version: 'rel-1.3.
|
1
|
+
version: 'rel-1.3.2'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ucb_ldap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Hansen, Steve Downey, Lucas Rockwell
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-02-09 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -19,6 +19,9 @@ dependencies:
|
|
19
19
|
version_requirements: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
- - "="
|
22
25
|
- !ruby/object:Gem::Version
|
23
26
|
version: 0.0.4
|
24
27
|
version:
|
@@ -56,7 +59,6 @@ extra_rdoc_files:
|
|
56
59
|
- lib/ucb_ldap_schema_attribute.rb
|
57
60
|
- lib/ucb_ldap_service.rb
|
58
61
|
- lib/ucb_ldap_student_term.rb
|
59
|
-
- lib/ucb_simple_ldap_entry.rb
|
60
62
|
files:
|
61
63
|
- CHANGELOG
|
62
64
|
- Manifest
|
@@ -80,7 +82,6 @@ files:
|
|
80
82
|
- lib/ucb_ldap_schema_attribute.rb
|
81
83
|
- lib/ucb_ldap_service.rb
|
82
84
|
- lib/ucb_ldap_student_term.rb
|
83
|
-
- lib/ucb_simple_ldap_entry.rb
|
84
85
|
- schema/schema.yml
|
85
86
|
- version.yml
|
86
87
|
- ucb_ldap.gemspec
|
@@ -1,201 +0,0 @@
|
|
1
|
-
|
2
|
-
module UCB
|
3
|
-
module LDAP
|
4
|
-
class SimpleEntry
|
5
|
-
|
6
|
-
# Returns new instance of UCB::LDAP::Entry. The argument
|
7
|
-
# net_ldap_entry is an instance of Net::LDAP::Entry.
|
8
|
-
#
|
9
|
-
# You should not need to create any UCB::LDAP::Entry instances;
|
10
|
-
# they are created by calls to UCB::LDAP.search and friends.
|
11
|
-
def initialize(dn) #:nodoc:
|
12
|
-
|
13
|
-
#
|
14
|
-
#auth = {:username=>"cn=greedybuddha", :method=>:simple, :password=>"wig0gin"}
|
15
|
-
#ldap = Net::LDAP.new(:host => '0.0.0.0', :port => 1389, :auth => auth)
|
16
|
-
#dn = "cn=Steven Hansen,ou=people,dc=berkeley,dc=edu"
|
17
|
-
#
|
18
|
-
#attr = {
|
19
|
-
# :objectclass => [
|
20
|
-
# "inetorgperson",
|
21
|
-
# ],
|
22
|
-
# :cn => "Steven Hansen",
|
23
|
-
# :uid => '61065',
|
24
|
-
# :sn => "Hansen",
|
25
|
-
#}
|
26
|
-
#
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# Don't store Net::LDAP entry in object since it uses the block
|
31
|
-
# initialization method of Hash which can't be marshalled ... this
|
32
|
-
# means it can't be stored in a Rails session.
|
33
|
-
@attributes = {}
|
34
|
-
net_ldap_entry.each do |attr, value|
|
35
|
-
@attributes[canonical(attr)] = value.map{|v| v.dup}
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# <tt>Hash</tt> of attributes returned from underlying NET::LDAP::Entry
|
40
|
-
# instance. Hash keys are #canonical attribute names, hash values are attribute
|
41
|
-
# values <em>as returned from LDAP</em>, i.e. arrays.
|
42
|
-
#
|
43
|
-
# You should most likely be referencing attributes as if they were
|
44
|
-
# instance methods rather than directly through this method. See top of
|
45
|
-
# this document.
|
46
|
-
def attributes
|
47
|
-
@attributes
|
48
|
-
end
|
49
|
-
|
50
|
-
# Returns the value of the <em>Distinguished Name</em> attribute.
|
51
|
-
def dn
|
52
|
-
attributes[canonical(:dn)]
|
53
|
-
end
|
54
|
-
|
55
|
-
def canonical(string_or_symbol) #:nodoc:
|
56
|
-
self.class.canonical(string_or_symbol)
|
57
|
-
end
|
58
|
-
|
59
|
-
def net_ldap
|
60
|
-
self.class.net_ldap
|
61
|
-
end
|
62
|
-
|
63
|
-
private unless $TESTING
|
64
|
-
|
65
|
-
# Used to get/set attribute values.
|
66
|
-
#
|
67
|
-
# If we can't make an attribute name out of method, let
|
68
|
-
# regular method_missing() handle it.
|
69
|
-
def method_missing(method, *args) #:nodoc:
|
70
|
-
setter_method?(method) ? value_setter(method, *args) : value_getter(method)
|
71
|
-
rescue BadAttributeNameException
|
72
|
-
return super
|
73
|
-
end
|
74
|
-
|
75
|
-
# Returns +true+ if _method_ is a "setter", i.e., ends in "=".
|
76
|
-
def setter_method?(method)
|
77
|
-
method.to_s[-1, 1] == "="
|
78
|
-
end
|
79
|
-
|
80
|
-
# Called by method_missing() to get an attribute value.
|
81
|
-
def value_getter(method)
|
82
|
-
schema_attribute = self.class.schema_attribute(method)
|
83
|
-
raw_value = attributes[canonical(schema_attribute.name)]
|
84
|
-
schema_attribute.get_value(raw_value)
|
85
|
-
end
|
86
|
-
|
87
|
-
# Called by method_missing() to set an attribute value.
|
88
|
-
def value_setter(method, *args)
|
89
|
-
schema_attribute = self.class.schema_attribute(method.to_s.chop)
|
90
|
-
attr_key = canonical(schema_attribute.name)
|
91
|
-
assigned_attributes[attr_key] = schema_attribute.ldap_value(args[0])
|
92
|
-
end
|
93
|
-
|
94
|
-
def assigned_attributes
|
95
|
-
@assigned_attributes ||= {}
|
96
|
-
end
|
97
|
-
|
98
|
-
# Class methods
|
99
|
-
class << self
|
100
|
-
|
101
|
-
public
|
102
|
-
|
103
|
-
# Creates and returns new entry. Returns +false+ if unsuccessful.
|
104
|
-
# Sets :objectclass key of <em>args[:attributes]</em> to
|
105
|
-
# object_classes read from schema.
|
106
|
-
#
|
107
|
-
# dn = "uid=999999,ou=people,dc=example,dc=com"
|
108
|
-
# attr = {
|
109
|
-
# :uid => "999999",
|
110
|
-
# :mail => "gsmith@example.com"
|
111
|
-
# }
|
112
|
-
#
|
113
|
-
# EntrySubClass.create(:dn => dn, :attributes => attr) #=> #<UCB::LDAP::EntrySubClass ..>
|
114
|
-
#
|
115
|
-
# Caller is responsible for setting :dn and :attributes correctly,
|
116
|
-
# as well as any other validation.
|
117
|
-
#
|
118
|
-
def create(args)
|
119
|
-
args[:attributes][:objectclass] = object_classes
|
120
|
-
net_ldap.add(args) or return false
|
121
|
-
|
122
|
-
# Why is the object being refetched from LDAP here?
|
123
|
-
find_by_dn(args[:dn])
|
124
|
-
end
|
125
|
-
|
126
|
-
# Same as #create(), but raises DirectoryNotUpdated on failure.
|
127
|
-
def create!(args)
|
128
|
-
create(args) || raise(DirectoryNotUpdatedException)
|
129
|
-
end
|
130
|
-
|
131
|
-
# Returns +Array+ of object classes making up this type of LDAP entity.
|
132
|
-
def object_classes
|
133
|
-
@object_classes ||= UCB::LDAP::Schema.schema_hash[entity_name]["objectClasses"]
|
134
|
-
end
|
135
|
-
|
136
|
-
def unique_object_class
|
137
|
-
@unique_object_class ||= UCB::LDAP::Schema.schema_hash[entity_name]["uniqueObjectClass"]
|
138
|
-
end
|
139
|
-
|
140
|
-
# Returns an +Array+ of Schema::Attribute for the entity.
|
141
|
-
def schema_attributes_array
|
142
|
-
@schema_attributes_array || set_schema_attributes
|
143
|
-
@schema_attributes_array
|
144
|
-
end
|
145
|
-
|
146
|
-
# Returns as +Hash+ whose keys are the canonical attribute names
|
147
|
-
# and whose values are the corresponding Schema::Attributes.
|
148
|
-
def schema_attributes_hash
|
149
|
-
@schema_attributes_hash || set_schema_attributes
|
150
|
-
@schema_attributes_hash
|
151
|
-
end
|
152
|
-
|
153
|
-
def schema_attribute(attribute_name)
|
154
|
-
schema_attributes_hash[canonical(attribute_name)] ||
|
155
|
-
raise(BadAttributeNameException, "'#{attribute_name}' is not a recognized attribute name")
|
156
|
-
end
|
157
|
-
|
158
|
-
|
159
|
-
# Returns the canonical representation of a symbol or string so
|
160
|
-
# we can look up attributes in a number of ways.
|
161
|
-
def canonical(string_or_symbol)
|
162
|
-
string_or_symbol.to_s.downcase.to_sym
|
163
|
-
end
|
164
|
-
|
165
|
-
# Returns underlying Net::LDAP instance.
|
166
|
-
def net_ldap #:nodoc:
|
167
|
-
UCB::LDAP.net_ldap
|
168
|
-
end
|
169
|
-
|
170
|
-
private unless $TESTING
|
171
|
-
|
172
|
-
|
173
|
-
# Want an array of Schema::Attributes as well as a hash
|
174
|
-
# of all possible variations on a name pointing to correct array element.
|
175
|
-
def set_schema_attributes
|
176
|
-
@schema_attributes_array = []
|
177
|
-
@schema_attributes_hash = {}
|
178
|
-
UCB::LDAP::Schema.schema_hash[entity_name]["attributes"].each do |k, v|
|
179
|
-
sa = UCB::LDAP::Schema::Attribute.new(v.merge("name" => k))
|
180
|
-
@schema_attributes_array << sa
|
181
|
-
[sa.name, sa.aliases].flatten.each do |name|
|
182
|
-
@schema_attributes_hash[canonical(name)] = sa
|
183
|
-
end
|
184
|
-
end
|
185
|
-
rescue
|
186
|
-
raise "Error loading schema attributes for entity_name '#{entity_name}'"
|
187
|
-
end
|
188
|
-
|
189
|
-
# Can be overridden in #search by passing in a <tt>:base</tt> parm.
|
190
|
-
def tree_base
|
191
|
-
@tree_base
|
192
|
-
end
|
193
|
-
|
194
|
-
def tree_base=(tree_base)
|
195
|
-
@tree_base = tree_base
|
196
|
-
end
|
197
|
-
|
198
|
-
end # end of class methods
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|