cornell_ldap 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/cornell_ldap.gemspec +8 -8
- data/lib/cornell_ldap.rb +84 -48
- data/spec/cornell_ldap_spec.rb +22 -26
- metadata +31 -16
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/aepstein/cornell_ldap"
|
12
12
|
gem.authors = ["Ari Epstein"]
|
13
13
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
-
gem.add_dependency "
|
14
|
+
gem.add_dependency "net-ldap", '>= 0.1.1'
|
15
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
16
|
end
|
17
17
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.3.0
|
data/cornell_ldap.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{cornell_ldap}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ari Epstein"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-07-27}
|
13
13
|
s.description = %q{Using ActiveLdap, this library provides an easy interface for communicating with the Cornell University LDAP directory. Use of this directory is restricted to purposes authorized by the university.}
|
14
14
|
s.email = %q{aepstein607@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,11 +32,11 @@ Gem::Specification.new do |s|
|
|
32
32
|
s.homepage = %q{http://github.com/aepstein/cornell_ldap}
|
33
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
34
34
|
s.require_paths = ["lib"]
|
35
|
-
s.rubygems_version = %q{1.3.
|
35
|
+
s.rubygems_version = %q{1.3.6}
|
36
36
|
s.summary = %q{Toolkit for accessing information about people through the Cornell University LDAP directory.}
|
37
37
|
s.test_files = [
|
38
|
-
"spec/
|
39
|
-
"spec/
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"spec/cornell_ldap_spec.rb"
|
40
40
|
]
|
41
41
|
|
42
42
|
if s.respond_to? :specification_version then
|
@@ -45,14 +45,14 @@ Gem::Specification.new do |s|
|
|
45
45
|
|
46
46
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
47
|
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
48
|
-
s.add_runtime_dependency(%q<
|
48
|
+
s.add_runtime_dependency(%q<net-ldap>, [">= 0.1.1"])
|
49
49
|
else
|
50
50
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
-
s.add_dependency(%q<
|
51
|
+
s.add_dependency(%q<net-ldap>, [">= 0.1.1"])
|
52
52
|
end
|
53
53
|
else
|
54
54
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
-
s.add_dependency(%q<
|
55
|
+
s.add_dependency(%q<net-ldap>, [">= 0.1.1"])
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
data/lib/cornell_ldap.rb
CHANGED
@@ -1,10 +1,74 @@
|
|
1
1
|
require 'rubygems' unless defined? Gem
|
2
|
-
require '
|
2
|
+
require 'net/ldap' unless defined? Net::LDAP
|
3
3
|
|
4
4
|
module CornellLdap
|
5
|
-
class Record <
|
6
|
-
|
7
|
-
|
5
|
+
class Record < Object
|
6
|
+
|
7
|
+
MAPPINGS = {
|
8
|
+
:cornelledutype => :status,
|
9
|
+
:cornelleduwrkngtitle1 => :status_title1,
|
10
|
+
:cornelleduwrkngtitle2 => :status_title2,
|
11
|
+
:cornelleduacadcollege => :college,
|
12
|
+
:cornelleducampusaddress => :campus_address,
|
13
|
+
:cornelledulocaladdress => :local_address,
|
14
|
+
:homepostaladdress => :home_address,
|
15
|
+
:givenname => :first_name,
|
16
|
+
:cornelledumiddlename => :middle_name,
|
17
|
+
:sn => :last_name
|
18
|
+
}
|
19
|
+
|
20
|
+
CONNECTION_PARAMETERS = [ :port, :host, :auth ]
|
21
|
+
|
22
|
+
def self.setup_connection( params )
|
23
|
+
@@connection_params = params
|
24
|
+
con = {}
|
25
|
+
CONNECTION_PARAMETERS.each do |setting|
|
26
|
+
con[setting] = @@connection_params[setting] if params.key? setting
|
27
|
+
end
|
28
|
+
@@connection = Net::LDAP.new( con )
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.connection
|
32
|
+
@@connection
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.find( subject )
|
36
|
+
r = connection.search(
|
37
|
+
:base => 'ou=People,' + @@connection_params[:base], :size => 1,
|
38
|
+
:filter => Net::LDAP::Filter.eq( 'uid', subject ),
|
39
|
+
:scope => Net::LDAP::SearchScope_SingleLevel
|
40
|
+
)
|
41
|
+
return CornellLdap::Record.new( r.first ) if r && r.length > 0
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
|
45
|
+
# Takes an address string and converts it to a hash representation:
|
46
|
+
#
|
47
|
+
# [street] Street address
|
48
|
+
# [city] City
|
49
|
+
# [state] State
|
50
|
+
# [zip] Zip code
|
51
|
+
def self.address_attributes(string_address)
|
52
|
+
return if string_address.nil?
|
53
|
+
array_address = string_address.split(',').map! { |x| x.strip }
|
54
|
+
state = false
|
55
|
+
array_address.each_index { |i| state = i if array_address[i] =~ /^[A-Za-z]{2,2}$/ }
|
56
|
+
return unless state && state > 1
|
57
|
+
attributes = { :street => array_address[0..(state-2)].join(', '),
|
58
|
+
:city => array_address[state - 1],
|
59
|
+
:state => array_address[state] }
|
60
|
+
attributes[:zip] = array_address[state + 1] unless array_address[state + 1].nil?
|
61
|
+
attributes
|
62
|
+
end
|
63
|
+
|
64
|
+
attr_accessor :attributes
|
65
|
+
|
66
|
+
def initialize( record )
|
67
|
+
self.attributes = Hash.new
|
68
|
+
MAPPINGS.each do |ldap, local|
|
69
|
+
self.attributes[local] = record[ldap].first unless record[ldap].nil? || record[ldap].empty?
|
70
|
+
end
|
71
|
+
end
|
8
72
|
|
9
73
|
# Returns a string representation of the person's status with the university
|
10
74
|
# of one of the following types:
|
@@ -15,26 +79,26 @@ module CornellLdap
|
|
15
79
|
# [grad] graduate or professional student
|
16
80
|
# [alumni] alumnus
|
17
81
|
# [temporary] temporary or casual employee
|
18
|
-
#
|
82
|
+
# false status unknown
|
19
83
|
def status
|
20
84
|
return @status unless @status.nil?
|
21
|
-
return unless
|
22
|
-
@status = case
|
85
|
+
return unless attributes.key?( :status )
|
86
|
+
@status = case attributes[:status]
|
23
87
|
when /^staff/ then 'staff'
|
24
88
|
when /^acad/ then
|
25
|
-
if (
|
26
|
-
(
|
89
|
+
if ( attributes.key?(:status_title1) && attributes[:status_title1] =~ /^Prof/ ) ||
|
90
|
+
( attributes.key?(:status_title2) && attributes[:status_title2] =~ /^Prof/ ) then
|
27
91
|
'faculty'
|
28
92
|
else
|
29
93
|
'staff'
|
30
94
|
end
|
31
|
-
when /^student/ then case
|
95
|
+
when /^student/ then case attributes[:college]
|
32
96
|
when 'AS', 'AR', 'AG', 'IL', 'HE', 'EN', 'UN' then 'undergrad'
|
33
97
|
else 'grad'
|
34
98
|
end
|
35
99
|
when /^alumni/ then 'alumni'
|
36
100
|
when /^temp/ then 'temporary'
|
37
|
-
else
|
101
|
+
else false
|
38
102
|
end
|
39
103
|
end
|
40
104
|
|
@@ -45,8 +109,8 @@ module CornellLdap
|
|
45
109
|
unless @campus_address.nil?
|
46
110
|
return @campus_address
|
47
111
|
end
|
48
|
-
@campus_address = false unless
|
49
|
-
@campus_address = { :street =>
|
112
|
+
@campus_address = false unless attributes.key?(:campus_address) &&
|
113
|
+
@campus_address = { :street => attributes[:campus_address].strip,
|
50
114
|
:on_campus => true }
|
51
115
|
return campus_address
|
52
116
|
end
|
@@ -57,8 +121,8 @@ module CornellLdap
|
|
57
121
|
unless @local_address.nil?
|
58
122
|
return @local_address
|
59
123
|
end
|
60
|
-
@local_address = false unless
|
61
|
-
@local_address = Record.address_attributes(
|
124
|
+
@local_address = false unless attributes.key?(:local_address) &&
|
125
|
+
@local_address = Record.address_attributes(attributes[:local_address])
|
62
126
|
return local_address
|
63
127
|
end
|
64
128
|
|
@@ -68,47 +132,19 @@ module CornellLdap
|
|
68
132
|
unless @home_address.nil?
|
69
133
|
return @home_address
|
70
134
|
end
|
71
|
-
@home_address = false unless
|
72
|
-
@home_address = Record.address_attributes(
|
135
|
+
@home_address = false unless attributes.key?(:home_address) &&
|
136
|
+
@home_address = Record.address_attributes(attributes[:home_address])
|
73
137
|
return home_address
|
74
138
|
end
|
75
139
|
|
76
|
-
# Takes an address string and converts it to a hash representation:
|
77
|
-
#
|
78
|
-
# [street] Street address
|
79
|
-
# [city] City
|
80
|
-
# [state] State
|
81
|
-
# [zip] Zip code
|
82
|
-
def self.address_attributes(string_address)
|
83
|
-
return if string_address.nil?
|
84
|
-
array_address = string_address.split(',').map! { |x| x.strip }
|
85
|
-
state = false
|
86
|
-
array_address.each_index { |i| state = i if array_address[i] =~ /^[A-Za-z]{2,2}$/ }
|
87
|
-
return unless state && state > 1
|
88
|
-
attributes = { :street => array_address[0..(state-2)].join(', '),
|
89
|
-
:city => array_address[state - 1],
|
90
|
-
:state => array_address[state] }
|
91
|
-
attributes[:zip] = array_address[state + 1] unless array_address[state + 1].nil?
|
92
|
-
attributes
|
93
|
-
end
|
94
|
-
|
95
140
|
# Returns first name of person
|
96
|
-
def first_name
|
97
|
-
return nil unless attribute_present?('givenName')
|
98
|
-
self.givenName
|
99
|
-
end
|
141
|
+
def first_name; attributes[:first_name]; end
|
100
142
|
|
101
143
|
# Returns middle name of person
|
102
|
-
def middle_name
|
103
|
-
return nil unless attribute_present?('cornelledumiddlename')
|
104
|
-
self.cornelledumiddlename
|
105
|
-
end
|
144
|
+
def middle_name; attributes[:middle_name]; end
|
106
145
|
|
107
146
|
# Returns last name of person
|
108
|
-
def last_name
|
109
|
-
return nil unless attribute_present?('sn')
|
110
|
-
self.sn
|
111
|
-
end
|
147
|
+
def last_name; attributes[:last_name]; end
|
112
148
|
end
|
113
149
|
end
|
114
150
|
|
data/spec/cornell_ldap_spec.rb
CHANGED
@@ -2,9 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
require 'cornell_ldap'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
:base => 'o=Cornell University,c=US'
|
5
|
+
CornellLdap::Record.setup_connection :host => 'directory.cornell.edu',
|
6
|
+
:port => 389, :base => 'o=Cornell University,c=US'
|
8
7
|
|
9
8
|
|
10
9
|
describe "CornellLdap" do
|
@@ -18,7 +17,7 @@ describe "CornellLdap" do
|
|
18
17
|
['student', 'GR', nil, nil, 'grad'],
|
19
18
|
['alumni', nil, nil, nil, 'alumni'],
|
20
19
|
['temp', nil, nil, nil, 'temporary'],
|
21
|
-
['blah', nil, nil, nil,
|
20
|
+
['blah', nil, nil, nil, false]
|
22
21
|
]
|
23
22
|
end
|
24
23
|
|
@@ -27,13 +26,12 @@ describe "CornellLdap" do
|
|
27
26
|
|
28
27
|
it "should correctly guess the status of several types" do
|
29
28
|
@map.each do |scenario|
|
30
|
-
person = mock_person
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
}
|
29
|
+
person = mock_person( {
|
30
|
+
:cornelledutype => [scenario[0]],
|
31
|
+
:cornelleduacadcollege => [scenario[1]],
|
32
|
+
:cornelleduwrkngtitle1 => [scenario[2]],
|
33
|
+
:cornelleduwrkngtitle2 => [scenario[3]]
|
34
|
+
} )
|
37
35
|
person.status.should eql scenario[4]
|
38
36
|
person
|
39
37
|
end
|
@@ -61,13 +59,13 @@ describe "CornellLdap" do
|
|
61
59
|
|
62
60
|
it "should call address_attributes to return local address" do
|
63
61
|
person = mock_person
|
64
|
-
CornellLdap::Record.should_receive(:address_attributes).once.with(person.
|
62
|
+
CornellLdap::Record.should_receive(:address_attributes).once.with(person.attributes[:local_address])
|
65
63
|
person.local_address
|
66
64
|
end
|
67
65
|
|
68
66
|
it "should call address_attributes to return home address" do
|
69
67
|
person = mock_person
|
70
|
-
CornellLdap::Record.should_receive(:address_attributes).once.with(person.
|
68
|
+
CornellLdap::Record.should_receive(:address_attributes).once.with(person.attributes[:home_address])
|
71
69
|
person.home_address
|
72
70
|
end
|
73
71
|
|
@@ -76,19 +74,17 @@ describe "CornellLdap" do
|
|
76
74
|
person.campus_address[:street].should eql '-100 Day Hall'
|
77
75
|
end
|
78
76
|
|
79
|
-
def mock_person
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
}
|
91
|
-
person
|
77
|
+
def mock_person(values = {})
|
78
|
+
CornellLdap::Record.new( {
|
79
|
+
:cornelledutype => ['staff'],
|
80
|
+
:cornelleduacadcollege => ['AS'],
|
81
|
+
:cornelleducampusaddress => ['-100 Day Hall '],
|
82
|
+
:cornelledulocaladdress => ['1 Main St., Apt. 1, Ithaca, NY, 14850'],
|
83
|
+
:homepostaladdress => ['1 Broadway, New York, NY, 00000'],
|
84
|
+
:givenname => ['John'],
|
85
|
+
:cornelledumiddlename => ['A'],
|
86
|
+
:sn => ['Doe']
|
87
|
+
}.merge(values) )
|
92
88
|
end
|
93
89
|
end
|
94
90
|
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cornell_ldap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 1.3.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Ari Epstein
|
@@ -9,29 +14,37 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-07-27 00:00:00 -04:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: rspec
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
23
31
|
version: 1.2.9
|
24
|
-
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
35
|
+
name: net-ldap
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
33
|
-
|
34
|
-
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 1
|
44
|
+
- 1
|
45
|
+
version: 0.1.1
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
35
48
|
description: Using ActiveLdap, this library provides an easy interface for communicating with the Cornell University LDAP directory. Use of this directory is restricted to purposes authorized by the university.
|
36
49
|
email: aepstein607@gmail.com
|
37
50
|
executables: []
|
@@ -66,21 +79,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
79
|
requirements:
|
67
80
|
- - ">="
|
68
81
|
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
69
84
|
version: "0"
|
70
|
-
version:
|
71
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - ">="
|
74
88
|
- !ruby/object:Gem::Version
|
89
|
+
segments:
|
90
|
+
- 0
|
75
91
|
version: "0"
|
76
|
-
version:
|
77
92
|
requirements: []
|
78
93
|
|
79
94
|
rubyforge_project:
|
80
|
-
rubygems_version: 1.3.
|
95
|
+
rubygems_version: 1.3.6
|
81
96
|
signing_key:
|
82
97
|
specification_version: 3
|
83
98
|
summary: Toolkit for accessing information about people through the Cornell University LDAP directory.
|
84
99
|
test_files:
|
85
|
-
- spec/cornell_ldap_spec.rb
|
86
100
|
- spec/spec_helper.rb
|
101
|
+
- spec/cornell_ldap_spec.rb
|