dm-ldap-adapter 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +4 -1
- data/lib/adapters/ldap_adapter.rb +19 -7
- data/lib/ldap/array.rb +9 -0
- data/lib/ldap/ldap_facade.rb +12 -9
- data/lib/ldap/version.rb +1 -1
- data/spec/ldap_adapter_spec.rb +15 -1
- data/spec/multi_repository_spec.rb +43 -0
- data/spec/multi_value_attributes_spec.rb +100 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +6 -6
- data/test.db +0 -0
- metadata +6 -3
- data/lib/ldap/ldap_facade_mock.rb +0 -56
data/Manifest.txt
CHANGED
@@ -11,14 +11,17 @@ lib/adapters/ldap_adapter.rb
|
|
11
11
|
lib/adapters/memory_adapter.rb
|
12
12
|
lib/adapters/simple_adapter.rb
|
13
13
|
lib/dummy_ldap_resource.rb
|
14
|
+
lib/ldap/array.rb
|
14
15
|
lib/ldap/digest.rb
|
15
16
|
lib/ldap/ldap_facade.rb
|
16
|
-
lib/ldap/ldap_facade_mock.rb
|
17
17
|
lib/ldap/version.rb
|
18
18
|
lib/ldap_resource.rb
|
19
19
|
spec/assiociations_ldap_adapter_spec.rb
|
20
20
|
spec/authentication_ldap_adapter_spec.rb
|
21
21
|
spec/ldap_adapter_spec.rb
|
22
|
+
spec/multi_repository_spec.rb
|
23
|
+
spec/multi_value_attributes_spec.rb
|
22
24
|
spec/spec.opts
|
23
25
|
spec/spec_helper.rb
|
26
|
+
test.db
|
24
27
|
test.ldif
|
@@ -126,7 +126,12 @@ module DataMapper
|
|
126
126
|
end
|
127
127
|
key = prop if prop.serial?
|
128
128
|
end
|
129
|
-
|
129
|
+
resource_dup = resource.dup
|
130
|
+
id = ldap.retrieve_next_id(resource.model.treebase,
|
131
|
+
key_properties(resource).field)
|
132
|
+
resource_dup.send("#{key_properties(resource).name}=".to_sym, id)
|
133
|
+
props[key_properties(resource).field.to_sym] = "#{id}"
|
134
|
+
key_value = ldap.create_object(resource.model.dn_prefix(resource_dup),
|
130
135
|
resource.model.treebase,
|
131
136
|
key_properties(resource).field,
|
132
137
|
props, resource.model.multivalue_field)
|
@@ -135,10 +140,12 @@ module DataMapper
|
|
135
140
|
key.set!(resource, key_value.to_i)
|
136
141
|
resource
|
137
142
|
elsif resource.model.multivalue_field
|
138
|
-
multivalue_prop = resource.send(:properties)
|
143
|
+
multivalue_prop = resource.send(:properties).detect do |prop|
|
144
|
+
prop.field.to_sym == resource.model.multivalue_field
|
145
|
+
end
|
139
146
|
update_resource(resource,
|
140
147
|
{ multivalue_prop =>
|
141
|
-
resource.send(
|
148
|
+
resource.send(multivalue_prop.name.to_sym)})
|
142
149
|
else
|
143
150
|
nil
|
144
151
|
end
|
@@ -171,7 +178,7 @@ module DataMapper
|
|
171
178
|
array_actions
|
172
179
|
end
|
173
180
|
else
|
174
|
-
if resource.model.multivalue_field == property.
|
181
|
+
if resource.model.multivalue_field == property.field.to_sym
|
175
182
|
if value.nil?
|
176
183
|
actions << [:delete, field, resource.original_values[property.name].to_s]
|
177
184
|
else
|
@@ -199,10 +206,13 @@ module DataMapper
|
|
199
206
|
# @see SimpleAdapter#delete_resource
|
200
207
|
def delete_resource(resource)
|
201
208
|
if resource.model.multivalue_field
|
209
|
+
multivalue_prop = resource.send(:properties).detect do |prop|
|
210
|
+
prop.field.to_sym == resource.model.multivalue_field
|
211
|
+
end
|
202
212
|
# set the original value so update does the right thing
|
203
|
-
resource.send("#{
|
213
|
+
resource.send("#{multivalue_prop.name}=".to_sym, nil)
|
204
214
|
update_resource(resource,
|
205
|
-
{
|
215
|
+
{ multivalue_prop => nil })
|
206
216
|
else
|
207
217
|
ldap.delete_object(resource.model.dn_prefix(resource),
|
208
218
|
resource.model.treebase)
|
@@ -280,7 +290,9 @@ module DataMapper
|
|
280
290
|
result.collect do |props|
|
281
291
|
query.fields.collect do |f|
|
282
292
|
prop = props[f.field.to_sym]
|
283
|
-
if
|
293
|
+
if f.type == DataMapper::Types::LdapArray
|
294
|
+
prop if prop
|
295
|
+
elsif prop
|
284
296
|
f.primitive == Integer ? prop.first.to_i : prop.first
|
285
297
|
end
|
286
298
|
end
|
data/lib/ldap/array.rb
ADDED
data/lib/ldap/ldap_facade.rb
CHANGED
@@ -21,12 +21,7 @@ module Ldap
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
# @param treebase the treebase of the dn or any search
|
26
|
-
# @param key_field field which carries the integer unique id of the entity
|
27
|
-
# @param props Hash of the ldap attributes of the new ldap object
|
28
|
-
# @return nil in case of an error or the new id of the created object
|
29
|
-
def create_object(dn_prefix, treebase, key_field, props, silence = false)
|
24
|
+
def retrieve_next_id(treebase, key_field)
|
30
25
|
base = "#{treebase},#{@ldap.base}"
|
31
26
|
id_sym = key_field.downcase.to_sym
|
32
27
|
max = 0
|
@@ -36,11 +31,19 @@ module Ldap
|
|
36
31
|
n = entry[id_sym].first.to_i
|
37
32
|
max = n if max < n
|
38
33
|
end
|
39
|
-
|
40
|
-
|
34
|
+
max + 1
|
35
|
+
end
|
36
|
+
|
37
|
+
# @param dn_prefix String the prefix of the dn
|
38
|
+
# @param treebase the treebase of the dn or any search
|
39
|
+
# @param key_field field which carries the integer unique id of the entity
|
40
|
+
# @param props Hash of the ldap attributes of the new ldap object
|
41
|
+
# @return nil in case of an error or the new id of the created object
|
42
|
+
def create_object(dn_prefix, treebase, key_field, props, silence = false)
|
43
|
+
base = "#{treebase},#{@ldap.base}"
|
41
44
|
if @ldap.add( :dn => dn(dn_prefix, treebase),
|
42
45
|
:attributes => props) and @ldap.get_operation_result.code.to_s == "0"
|
43
|
-
|
46
|
+
props[key_field.downcase.to_sym]
|
44
47
|
else
|
45
48
|
unless silence
|
46
49
|
msg = ldap_error("create",
|
data/lib/ldap/version.rb
CHANGED
data/spec/ldap_adapter_spec.rb
CHANGED
@@ -27,6 +27,20 @@ require 'spec_helper'
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
it 'should create an uid' do
|
31
|
+
class User
|
32
|
+
# put the assert here
|
33
|
+
dn_prefix { |user| user.id.should_not == nil; "uid=#{user.login}"}
|
34
|
+
end
|
35
|
+
|
36
|
+
DataMapper.repository(adapter) do
|
37
|
+
id = @user1.id
|
38
|
+
@user1.destroy
|
39
|
+
@user1 = User.create(:login => "black", :name => 'Black', :age => 0)
|
40
|
+
@user1.id.should_not == id
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
30
44
|
it 'should successfully save an object' do
|
31
45
|
DataMapper.repository(adapter) do
|
32
46
|
@user1.new_record?.should be_false
|
@@ -34,7 +48,7 @@ require 'spec_helper'
|
|
34
48
|
end
|
35
49
|
end
|
36
50
|
|
37
|
-
it 'should
|
51
|
+
it 'should raise an error when trying to create an entity with already used key' do
|
38
52
|
DataMapper.repository(adapter) do
|
39
53
|
#p User.first(:login => "black")
|
40
54
|
lambda { User.create(:login => "black", :name => 'Black', :age => 0) }.should raise_error
|
@@ -0,0 +1,43 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__)
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class Order
|
5
|
+
include DataMapper::Resource
|
6
|
+
|
7
|
+
property :id, Serial
|
8
|
+
|
9
|
+
repository(:ldap) do
|
10
|
+
belongs_to :user
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
DataMapper.auto_migrate!(:default)
|
15
|
+
|
16
|
+
describe DataMapper.repository(:ldap).adapter do
|
17
|
+
|
18
|
+
describe 'belongs_to association' do
|
19
|
+
|
20
|
+
before do
|
21
|
+
DataMapper.repository(:ldap) do
|
22
|
+
@user = User.new(:login => "beige", :name => 'Beige')
|
23
|
+
@user.password = "asd123"
|
24
|
+
@user.save
|
25
|
+
end
|
26
|
+
|
27
|
+
@order = Order.create
|
28
|
+
end
|
29
|
+
|
30
|
+
after do
|
31
|
+
DataMapper.repository(:ldap) do
|
32
|
+
@user.destroy
|
33
|
+
end
|
34
|
+
@order.destroy
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should create and load the association' do
|
38
|
+
@order.user = @user
|
39
|
+
@order.save
|
40
|
+
Order.get!(@order.id).user.should == @user
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__)
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class Contact
|
5
|
+
include DataMapper::Resource
|
6
|
+
|
7
|
+
property :id, Serial, :field => "uidnumber"
|
8
|
+
property :login, String, :field => "uid", :unique_index => true
|
9
|
+
property :hashed_password, String, :field => "userpassword", :access => :private
|
10
|
+
property :name, String, :field => "cn"
|
11
|
+
property :mail, LdapArray
|
12
|
+
|
13
|
+
dn_prefix { |contact| "uid=#{contact.login}"}
|
14
|
+
|
15
|
+
treebase "ou=people"
|
16
|
+
|
17
|
+
ldap_properties do |contact|
|
18
|
+
properties = { :objectclass => ["inetOrgPerson", "posixAccount", "shadowAccount"], :loginshell => "/bin/bash", :gidnumber => "10000" }
|
19
|
+
properties[:sn] = "#{contact.name.sub(/.*\ /, "")}"
|
20
|
+
properties[:givenname] = "#{contact.name.sub(/\ .*/, "")}"
|
21
|
+
properties[:homedirectory] = "/home/#{contact.login}"
|
22
|
+
properties
|
23
|
+
end
|
24
|
+
|
25
|
+
def password=(password)
|
26
|
+
attribute_set(:hashed_password, Ldap::Digest.ssha(password, "--#{Time.now}--#{login}--")) if password
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe DataMapper.repository(:ldap).adapter.class do
|
31
|
+
|
32
|
+
describe 'LdapArray' do
|
33
|
+
|
34
|
+
before :each do
|
35
|
+
DataMapper.repository(:ldap) do
|
36
|
+
@contact = Contact.first(:login => "beige") || Contact.new(:login => "beige", :name => 'Beige')
|
37
|
+
@contact.password = "asd123"
|
38
|
+
@contact.save
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
after :each do
|
43
|
+
DataMapper.repository(:ldap) do
|
44
|
+
@contact.destroy
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should add many values to a LdapArray' do
|
49
|
+
DataMapper.repository(:ldap) do
|
50
|
+
@contact.mail.should == []
|
51
|
+
|
52
|
+
@contact.mail << "email1"
|
53
|
+
@contact.save
|
54
|
+
@contact = Contact.get!(@contact.id)
|
55
|
+
@contact.mail.should == ["email1"]
|
56
|
+
|
57
|
+
@contact.mail << "email2"
|
58
|
+
@contact.save
|
59
|
+
@contact = Contact.get!(@contact.id)
|
60
|
+
@contact.mail.should == ["email1", "email2"]
|
61
|
+
|
62
|
+
@contact.mail.delete("email1")
|
63
|
+
@contact.save
|
64
|
+
@contact = Contact.get!(@contact.id)
|
65
|
+
@contact.mail.should == ["email2"]
|
66
|
+
|
67
|
+
@contact.mail.delete("email2")
|
68
|
+
@contact.save
|
69
|
+
@contact = Contact.get!(@contact.id)
|
70
|
+
@contact.mail.should == []
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should get an LdapArray on retrieving collection' do
|
75
|
+
DataMapper.repository(:ldap) do
|
76
|
+
@contact.mail.should == []
|
77
|
+
|
78
|
+
@contact.mail << "email1"
|
79
|
+
@contact.save
|
80
|
+
@contact = Contact.all.detect {|c| c.id = @contact.id}
|
81
|
+
@contact.mail.should == ["email1"]
|
82
|
+
|
83
|
+
@contact.mail << "email2"
|
84
|
+
@contact.save
|
85
|
+
@contact = Contact.all.detect {|c| c.id = @contact.id}
|
86
|
+
@contact.mail.should == ["email1", "email2"]
|
87
|
+
|
88
|
+
@contact.mail.delete("email1")
|
89
|
+
@contact.save
|
90
|
+
@contact = Contact.all.detect {|c| c.id = @contact.id}
|
91
|
+
@contact.mail.should == ["email2"]
|
92
|
+
|
93
|
+
@contact.mail.delete("email2")
|
94
|
+
@contact.save
|
95
|
+
@contact = Contact.all.detect {|c| c.id = @contact.id}
|
96
|
+
@contact.mail.should == []
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/spec.opts
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -37,20 +37,20 @@ class User
|
|
37
37
|
has n, :group_users
|
38
38
|
|
39
39
|
def groups
|
40
|
-
groups = GroupUser.all(:
|
40
|
+
groups = GroupUser.all(:user_id => id).collect{ |gu| gu.group }
|
41
41
|
def groups.user=(user)
|
42
42
|
@user = user
|
43
43
|
end
|
44
44
|
groups.user = self
|
45
45
|
def groups.<<(group)
|
46
46
|
unless member? group
|
47
|
-
GroupUser.create(:
|
47
|
+
GroupUser.create(:user_id => @user.id, :group_id => group.id)
|
48
48
|
super
|
49
49
|
end
|
50
50
|
self
|
51
51
|
end
|
52
52
|
def groups.delete(group)
|
53
|
-
gu = GroupUser.first(:
|
53
|
+
gu = GroupUser.first(:user_id => @user.id, :group_id => group.id)
|
54
54
|
if gu
|
55
55
|
gu.destroy
|
56
56
|
super
|
@@ -58,7 +58,7 @@ class User
|
|
58
58
|
end
|
59
59
|
groups
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
dn_prefix { |user| "uid=#{user.login}"}
|
63
63
|
|
64
64
|
treebase "ou=people"
|
@@ -85,7 +85,7 @@ class Role
|
|
85
85
|
|
86
86
|
treebase "ou=groups"
|
87
87
|
|
88
|
-
ldap_properties {:objectclass => "posixGroup"}
|
88
|
+
ldap_properties {{:objectclass => "posixGroup"}}
|
89
89
|
|
90
90
|
belongs_to :user
|
91
91
|
end
|
@@ -99,7 +99,7 @@ class Group
|
|
99
99
|
|
100
100
|
treebase "ou=groups"
|
101
101
|
|
102
|
-
ldap_properties {:objectclass => "posixGroup"}
|
102
|
+
ldap_properties {{:objectclass => "posixGroup"}}
|
103
103
|
end
|
104
104
|
|
105
105
|
class GroupUser
|
data/test.db
ADDED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-ldap-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mkristian
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-21 00:00:00 +05:30
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -78,16 +78,19 @@ files:
|
|
78
78
|
- lib/adapters/memory_adapter.rb
|
79
79
|
- lib/adapters/simple_adapter.rb
|
80
80
|
- lib/dummy_ldap_resource.rb
|
81
|
+
- lib/ldap/array.rb
|
81
82
|
- lib/ldap/digest.rb
|
82
83
|
- lib/ldap/ldap_facade.rb
|
83
|
-
- lib/ldap/ldap_facade_mock.rb
|
84
84
|
- lib/ldap/version.rb
|
85
85
|
- lib/ldap_resource.rb
|
86
86
|
- spec/assiociations_ldap_adapter_spec.rb
|
87
87
|
- spec/authentication_ldap_adapter_spec.rb
|
88
88
|
- spec/ldap_adapter_spec.rb
|
89
|
+
- spec/multi_repository_spec.rb
|
90
|
+
- spec/multi_value_attributes_spec.rb
|
89
91
|
- spec/spec.opts
|
90
92
|
- spec/spec_helper.rb
|
93
|
+
- test.db
|
91
94
|
- test.ldif
|
92
95
|
has_rdoc: true
|
93
96
|
homepage: http://dm-ldap-adapter.rubyforge.org
|
@@ -1,56 +0,0 @@
|
|
1
|
-
module Ldap
|
2
|
-
class LdapFacade
|
3
|
-
|
4
|
-
def self.open(config)
|
5
|
-
puts "open"
|
6
|
-
p config
|
7
|
-
puts
|
8
|
-
yield "dummy"
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize(uri)
|
12
|
-
puts "new #{self.hash}"
|
13
|
-
p uri
|
14
|
-
puts
|
15
|
-
end
|
16
|
-
|
17
|
-
def create_object(treebase, dn_prefix, key_field, props, silence = false)
|
18
|
-
options = { :dn_prefix => dn_prefix,
|
19
|
-
:treebase => treebase,
|
20
|
-
:key_field => key_field,
|
21
|
-
:properties => props }
|
22
|
-
puts "create #{self.hash}"
|
23
|
-
p options
|
24
|
-
puts
|
25
|
-
@@count ||= 0
|
26
|
-
@@count += 1
|
27
|
-
end
|
28
|
-
|
29
|
-
def read_objects(treebase, key_field, conditions, many = false)
|
30
|
-
options = { :treebase => treebase,
|
31
|
-
:key_field => key_field,
|
32
|
-
:conditions => conditions, :many => many }
|
33
|
-
puts "read #{self.hash}"
|
34
|
-
p options
|
35
|
-
puts
|
36
|
-
[] if many
|
37
|
-
end
|
38
|
-
|
39
|
-
def update_object(treebase, dn_prefix, actions)
|
40
|
-
options = { :dn_prefix => dn_prefix,
|
41
|
-
:treebase => treebase,
|
42
|
-
:actions => actions }
|
43
|
-
puts "update #{self.hash}"
|
44
|
-
p options
|
45
|
-
puts
|
46
|
-
end
|
47
|
-
|
48
|
-
def delete_object(treebase, dn_prefix)
|
49
|
-
options = { :dn_prefix => dn_prefix,
|
50
|
-
:treebase => treebase }
|
51
|
-
puts "delete #{self.hash}"
|
52
|
-
p options
|
53
|
-
puts
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|