acs-ldap 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +15 -0
- data/acs-ldap.gemspec +28 -0
- data/lib/acs/ldap/connector.rb +141 -0
- data/lib/acs/ldap/logger.rb +77 -0
- data/lib/acs/ldap/model.rb +81 -0
- data/lib/acs/ldap/result.rb +42 -0
- data/lib/acs/ldap/version.rb +5 -0
- data/lib/acs/ldap.rb +28 -0
- data/log/.keep +0 -0
- data/log/acs_ldap.log +0 -0
- data/spec/acs/ldap/connector_spec.rb +58 -0
- data/spec/acs/ldap/model_spec.rb +86 -0
- data/spec/acs/ldap_spec.rb +7 -0
- data/spec/spec_helper.rb +3 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 87192732f0e5cc5b001b0e80b18887dbb0179c62
|
4
|
+
data.tar.gz: ae9fb51329987aa1ff619c54db70fb65e6bb49f8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 64435288cabd7ab71f7d751e0538c490237a510816f795e79b311086dbba8aa186e0953a25fea8fd17682ccfa982ac866d5f6353494a4f5d14df66d4f534873d
|
7
|
+
data.tar.gz: 84a22a7ae34ab3408bd744f659e88d968390caf0ccb973b53be7b839048d4bab99ac41a229e16f21b19f65d331a370afd0470a76f4ac045df2666cd33c0b3bb5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Terranova David
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Acs::Ldap
|
2
|
+
|
3
|
+
ActiveRecord to LDAP adapter
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'acs-ldap'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install acs-ldap
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/acs-ldap/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new do |t|
|
5
|
+
t.libs << 'spec'
|
6
|
+
t.pattern = "spec/*_spec.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Run tests"
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
desc "Open an irb session preloaded with this library"
|
13
|
+
task :console do
|
14
|
+
sh "irb -rubygems -I lib -r acs/ldap"
|
15
|
+
end
|
data/acs-ldap.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'acs/ldap/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "acs-ldap"
|
8
|
+
s.version = Acs::Ldap::VERSION
|
9
|
+
s.authors = ["Terranova David"]
|
10
|
+
s.email = ["dterranova@adhara-cybersecurity.com"]
|
11
|
+
s.summary = %q{ActiveRecord to LDAP adapter}
|
12
|
+
s.description = %q{ActiveRecord to LDAP adapter}
|
13
|
+
s.homepage = ""
|
14
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
s.files = `git ls-files -z`.split("\x0")
|
17
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
s.add_development_dependency "rake", "~> 10.0"
|
23
|
+
s.add_development_dependency "rspec"
|
24
|
+
s.add_development_dependency "debugger2"
|
25
|
+
|
26
|
+
s.add_dependency 'net-ldap'
|
27
|
+
s.add_dependency 'rails'
|
28
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
class Acs::Ldap::Connector
|
2
|
+
|
3
|
+
def initialize(options = {})
|
4
|
+
@host = options[:host] || '127.0.0.1'
|
5
|
+
@port = options[:port] || 389
|
6
|
+
@base = options[:base] || nil
|
7
|
+
@dn = options[:dn] || nil
|
8
|
+
@password = options[:password] || nil
|
9
|
+
@tls = options[:tls] || false
|
10
|
+
|
11
|
+
@connected = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def ldap_params
|
15
|
+
ldap_params = {
|
16
|
+
host: @host,
|
17
|
+
port: @port,
|
18
|
+
base: @base,
|
19
|
+
auth: {
|
20
|
+
method: :simple, #other method ?
|
21
|
+
username: @dn,
|
22
|
+
password: @password
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
ldap_params[:encryption] = :simple_tls if @tls
|
27
|
+
|
28
|
+
logger.debug "Connection params: #{ldap_params}"
|
29
|
+
|
30
|
+
ldap_params
|
31
|
+
end
|
32
|
+
|
33
|
+
def search(options = {})
|
34
|
+
base = options[:base] || nil
|
35
|
+
filter = options[:filter] || nil # instance of Net::LDAP::Filter
|
36
|
+
attributes = options[:attributes] || nil
|
37
|
+
logger.info "Search base '#{base}' filter '#{filter}' attributes '#{attributes}'"
|
38
|
+
entries = []
|
39
|
+
get_connection.search({base: base, filter: filter, attributes: attributes}) do |entry|
|
40
|
+
entries << entry
|
41
|
+
end
|
42
|
+
result = Acs::Ldap::Result.new(get_connection.get_operation_result, entries)
|
43
|
+
logger.info "Search result #{result}"
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
def search_by(base, key, value, attributes = nil)
|
48
|
+
filter = Net::LDAP::Filter.eq(key, value.to_s)
|
49
|
+
search({base: base, filter: filter, attributes: attributes})
|
50
|
+
end
|
51
|
+
|
52
|
+
def search_one(base, key, value, attributes = nil)
|
53
|
+
result = search_by(base, key, value, attributes)
|
54
|
+
if result.data.count > 0
|
55
|
+
result.data[0]
|
56
|
+
else
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def add(dn, attributes)
|
62
|
+
#debugger
|
63
|
+
logger.info "Add dn '#{dn}' attributes '#{attributes.inspect}'"
|
64
|
+
get_connection.add(dn: dn, attributes: attributes)
|
65
|
+
result = Acs::Ldap::Result.new(get_connection.get_operation_result)
|
66
|
+
logger.info "Add result #{result}"
|
67
|
+
|
68
|
+
result
|
69
|
+
end
|
70
|
+
|
71
|
+
def update(dn, operations)
|
72
|
+
logger.info "Modify dn '#{dn}' operations '#{operations.inspect}'"
|
73
|
+
get_connection.modify(dn: dn, operations: operations)
|
74
|
+
result = Acs::Ldap::Result.new(get_connection.get_operation_result)
|
75
|
+
logger.info "Modify result #{result}"
|
76
|
+
|
77
|
+
result
|
78
|
+
end
|
79
|
+
|
80
|
+
def delete(dn)
|
81
|
+
logger.info "Delete dn '#{dn}'"
|
82
|
+
get_connection.delete(dn: dn)
|
83
|
+
result = Acs::Ldap::Result.new(get_connection.get_operation_result)
|
84
|
+
logger.info "Delete result #{result}"
|
85
|
+
|
86
|
+
result
|
87
|
+
end
|
88
|
+
|
89
|
+
def delete_all(ou)
|
90
|
+
logger.info "Delete all ou=#{ou}"
|
91
|
+
search({base: "ou=#{ou},#{base}", attributes: 'uid'}).data.each do |entry|
|
92
|
+
delete(entry[:dn].first) if entry[:uid].present?
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def base
|
97
|
+
@base
|
98
|
+
end
|
99
|
+
|
100
|
+
def close_connection
|
101
|
+
if @connected
|
102
|
+
@ldap = nil
|
103
|
+
end
|
104
|
+
@connected = false
|
105
|
+
end
|
106
|
+
|
107
|
+
def get_connection
|
108
|
+
if @connected
|
109
|
+
@ldap
|
110
|
+
else
|
111
|
+
connect
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
protected
|
116
|
+
|
117
|
+
# get_connection should be used
|
118
|
+
def connect
|
119
|
+
logger.debug "LDAP connect"
|
120
|
+
if ! @connected
|
121
|
+
logger.debug "Binding to ldap..."
|
122
|
+
@ldap = Net::LDAP.new(ldap_params)
|
123
|
+
if @ldap.bind
|
124
|
+
logger.debug "Connection succeed"
|
125
|
+
@connected = true
|
126
|
+
else
|
127
|
+
@connected = false
|
128
|
+
logger.debug "Connection failed"
|
129
|
+
end
|
130
|
+
@ldap
|
131
|
+
else
|
132
|
+
@logger.debug "LDAP already connected"
|
133
|
+
nil
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def logger
|
138
|
+
Acs::Ldap.logger
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
class Acs::Ldap::Logger < ::Logger
|
2
|
+
|
3
|
+
def self.error(message)
|
4
|
+
build.error(message)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.info(message)
|
8
|
+
build.info(message)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.debug(message)
|
12
|
+
build.debug(message)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.read_latest
|
16
|
+
path = Rails.root.join("log", file_name)
|
17
|
+
self.build unless File.exist?(path)
|
18
|
+
tail_output, _ = Manager::Popen.popen(%W(tail -n 2000 #{path}))
|
19
|
+
tail_output.split("\n")
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.read_latest_for filename
|
23
|
+
path = Rails.root.join("log", filename)
|
24
|
+
tail_output, _ = Manager::Popen.popen(%W(tail -n 2000 #{path}))
|
25
|
+
tail_output.split("\n")
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.build
|
29
|
+
new(Rails.root.join("log", file_name))
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.file_name
|
33
|
+
file_name_noext + '.log'
|
34
|
+
end
|
35
|
+
|
36
|
+
def format_message(severity, timestamp, progname, msg)
|
37
|
+
"#{severity} : #{timestamp.strftime("%y-%m-%d %H:%M:%S:%L %z")} : #{msg}\n"
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.archive
|
41
|
+
%x(gzip -c #{file_path} > #{targz_file_path})
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.clear
|
45
|
+
%x(echo > #{file_path})
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.size
|
49
|
+
File.new(file_path).size
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.targz_file_path
|
53
|
+
targz_file_name = "#{file_name}-" + %x(date "+%Y%m%d_%H%M%S").gsub("\n", '') + ".gz"
|
54
|
+
Rails.root.join("log", targz_file_name).to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.file_path
|
58
|
+
if Rails.root.present?
|
59
|
+
Rails.root.join("log", file_name).to_s
|
60
|
+
else
|
61
|
+
[Dir.pwd, "log", file_name].join("/").to_s
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.build
|
66
|
+
# File.delete(file_path)
|
67
|
+
self.new(file_path)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
##########
|
72
|
+
|
73
|
+
def self.file_name
|
74
|
+
"acs_ldap.log"
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
class Acs::Ldap::Model
|
2
|
+
|
3
|
+
def initialize(connector, options = {})
|
4
|
+
@connector = connector
|
5
|
+
@id = options[:id] || :id
|
6
|
+
end
|
7
|
+
|
8
|
+
def ou
|
9
|
+
@ou
|
10
|
+
end
|
11
|
+
|
12
|
+
def base
|
13
|
+
"ou=#{ou},#{@connector.base}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def dn(model)
|
17
|
+
"uid=#{model.send @id},#{base}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_by(key, value)
|
21
|
+
@connector.search_by(
|
22
|
+
base,
|
23
|
+
key,
|
24
|
+
value
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
def create(model)
|
29
|
+
attributes = attributes(model).except!(:uid)
|
30
|
+
attributes.merge!(objectClass: object_class)
|
31
|
+
|
32
|
+
@connector.add(
|
33
|
+
dn(model),
|
34
|
+
#base,
|
35
|
+
attributes
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
def update(model, attributes = nil)
|
40
|
+
operations = []
|
41
|
+
update_attributes = []
|
42
|
+
update_attributes << attributes
|
43
|
+
update_attributes.flatten
|
44
|
+
attributes(model).each do |sym, value|
|
45
|
+
if attributes == nil || update_attributes.include?(sym)
|
46
|
+
operations << [:replace, sym.to_s, value] unless sym.to_s == "uid"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
@connector.update(
|
50
|
+
dn(model),
|
51
|
+
operations
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def destroy(model)
|
56
|
+
@connector.delete(dn(model))
|
57
|
+
end
|
58
|
+
|
59
|
+
def flush
|
60
|
+
@connector.delete_all(ou)
|
61
|
+
end
|
62
|
+
|
63
|
+
def count
|
64
|
+
count = 0
|
65
|
+
@connector.search({base: base}).data.each do |entry|
|
66
|
+
count += 1 if entry[:uid].present?
|
67
|
+
end
|
68
|
+
count
|
69
|
+
end
|
70
|
+
|
71
|
+
def exist?(model)
|
72
|
+
@connector.search({base: dn(model)}).data.length > 0
|
73
|
+
end
|
74
|
+
|
75
|
+
protected
|
76
|
+
|
77
|
+
def logger
|
78
|
+
Acs::Ldap.logger
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Acs::Ldap::Result
|
2
|
+
def initialize(result, data = nil, log = false)
|
3
|
+
@code = result.code
|
4
|
+
@dn = result.matched_dn
|
5
|
+
@message = result.message
|
6
|
+
@data = data
|
7
|
+
logger.info to_s if log
|
8
|
+
end
|
9
|
+
|
10
|
+
def success?
|
11
|
+
@code == 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def code
|
15
|
+
@code
|
16
|
+
end
|
17
|
+
|
18
|
+
def dn
|
19
|
+
@dn
|
20
|
+
end
|
21
|
+
|
22
|
+
def message
|
23
|
+
@message
|
24
|
+
end
|
25
|
+
|
26
|
+
def data=(data)
|
27
|
+
@data = data
|
28
|
+
end
|
29
|
+
|
30
|
+
def data
|
31
|
+
@data
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
result = success? ? 'SUCCESS' : 'ERROR'
|
36
|
+
"#{result} return code:#{@code}, matched_dn: #{@dn}, message:#{@message}, data:#{@data.inspect}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def logger
|
40
|
+
Acs::Ldap.logger
|
41
|
+
end
|
42
|
+
end
|
data/lib/acs/ldap.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'net/ldap'
|
3
|
+
require "acs/ldap/version"
|
4
|
+
|
5
|
+
require "acs/ldap/logger"
|
6
|
+
require "acs/ldap/result"
|
7
|
+
require "acs/ldap/connector"
|
8
|
+
require "acs/ldap/model"
|
9
|
+
|
10
|
+
module Acs
|
11
|
+
module Ldap
|
12
|
+
|
13
|
+
def self.logger
|
14
|
+
@logger || Acs::Ldap::Logger
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.logger=(logger)
|
18
|
+
@logger = logger
|
19
|
+
end
|
20
|
+
|
21
|
+
class Railtie < ::Rails::Railtie
|
22
|
+
initializer :acs_ldap do |app|
|
23
|
+
Acs::Ldap.logger = Rails.logger
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/log/.keep
ADDED
File without changes
|
data/log/acs_ldap.log
ADDED
File without changes
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Acs::Ldap::Connector, order: :defined do
|
4
|
+
before(:context) do
|
5
|
+
@connector = Acs::Ldap::Connector.new({host: '192.168.59.103', port: 49389, base: "dc=adharacs,dc=lan", dn: "cn=admin,dc=adharacs,dc=lan", password: "admin"})
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should be possible to create a connector" do
|
9
|
+
expect(@connector).not_to be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should be possible to create a connection" do
|
13
|
+
expect(@connector.get_connection()).not_to be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be possible to search without specs" do
|
17
|
+
expect(@connector.search()).not_to be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be possible to add a user" do
|
21
|
+
result = @connector.add(
|
22
|
+
"uid=1,ou=people,dc=adharacs,dc=lan",
|
23
|
+
{
|
24
|
+
sn: "john.doe",
|
25
|
+
cn: "John Doe",
|
26
|
+
givenName: "John Doe",
|
27
|
+
mail: "john.doe@adharacs.lan",
|
28
|
+
userPassword: "{SSHA}+MBMtUqzkOeH8hI1KVnl+djdqzw0YmU5M2Y5MmQyOTgxMDU1",
|
29
|
+
objectClass: [
|
30
|
+
"organizationalPerson",
|
31
|
+
"person",
|
32
|
+
"top",
|
33
|
+
"extensibleObject"
|
34
|
+
]
|
35
|
+
}
|
36
|
+
)
|
37
|
+
expect(result.success?).to eq true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be possible to find a user" do
|
41
|
+
result = @connector.search_by(
|
42
|
+
"ou=people,dc=adharacs,dc=lan",
|
43
|
+
'mail',
|
44
|
+
'john.doe@adharacs.lan',
|
45
|
+
'mail'
|
46
|
+
)
|
47
|
+
expect(result.success?).to eq true
|
48
|
+
expect(result.data.length).to eq 1
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be possible to remove a user" do
|
52
|
+
result = @connector.delete(
|
53
|
+
"uid=1,ou=people,dc=adharacs,dc=lan"
|
54
|
+
)
|
55
|
+
expect(result.success?).to eq true
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Acs::Ldap::Model, order: :defined do
|
4
|
+
before(:context) do
|
5
|
+
@connector = Acs::Ldap::Connector.new({host: '192.168.59.103', port: 49389, base: "dc=adharacs,dc=lan", dn: "cn=admin,dc=adharacs,dc=lan", password: "admin"})
|
6
|
+
@user_model = UserModel.new(@connector)
|
7
|
+
@user = User.new({uid: 2, sn: "dark.vador", cn: "dark.vador", givenName: "Dark Vador", userPassword: "{SSHA}+MBMtUqzkOeH8hI1KVnl+djdqzw0YmU5M2Y5MmQyOTgxMDU1", mail: "dvador@adharacs.lan"})
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be possible to flush an OU" do
|
11
|
+
@user_model.flush
|
12
|
+
expect(@user_model.count).to eq 0
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should be possible to create a User" do
|
16
|
+
expect(@user_model.create(@user).success?).to eq true
|
17
|
+
expect(@user_model.count).to eq 1
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be possible to update a User" do
|
21
|
+
@user.givenName = "Vador Dark"
|
22
|
+
expect(@user_model.update(@user, :givenName).success?).to eq true
|
23
|
+
expect(@user_model.find_by('uid', 2).data[0][:givenName]).to eq ["Vador Dark"]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be possible to remove a User" do
|
27
|
+
expect(@user_model.destroy(@user).success?).to eq true
|
28
|
+
expect(@user_model.count).to eq 0
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be possible to check if a User exists" do
|
32
|
+
@user_model.flush
|
33
|
+
expect(@user_model.exist?(@user)).to eq false
|
34
|
+
@user_model.create(@user)
|
35
|
+
expect(@user_model.exist?(@user)).to eq true
|
36
|
+
@user_model.flush
|
37
|
+
end
|
38
|
+
|
39
|
+
class UserModel < Acs::Ldap::Model
|
40
|
+
def initialize(connector, ou = nil)
|
41
|
+
@ou = ou || 'people'
|
42
|
+
super(connector, {id: :uid})
|
43
|
+
end
|
44
|
+
|
45
|
+
def attributes(user)
|
46
|
+
{
|
47
|
+
uid: user.id,
|
48
|
+
sn: user.sn,
|
49
|
+
cn: user.cn,
|
50
|
+
givenName: user.givenName,
|
51
|
+
mail: user.mail,
|
52
|
+
userPassword: user.userPassword
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def object_class
|
57
|
+
[
|
58
|
+
"organizationalPerson",
|
59
|
+
"person",
|
60
|
+
"top",
|
61
|
+
"extensibleObject"
|
62
|
+
]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class User
|
67
|
+
|
68
|
+
def initialize(options = {})
|
69
|
+
@options = options
|
70
|
+
end
|
71
|
+
|
72
|
+
def method_missing(method_sym, *args, &block)
|
73
|
+
if args.length > 0
|
74
|
+
@options[method_sym.to_s.gsub(/=/,'').to_sym] = args[0]
|
75
|
+
else
|
76
|
+
@options[method_sym] || nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def to_s
|
81
|
+
@options.inspect
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acs-ldap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Terranova David
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: debugger2
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: net-ldap
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: ActiveRecord to LDAP adapter
|
98
|
+
email:
|
99
|
+
- dterranova@adhara-cybersecurity.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- acs-ldap.gemspec
|
110
|
+
- lib/acs/ldap.rb
|
111
|
+
- lib/acs/ldap/connector.rb
|
112
|
+
- lib/acs/ldap/logger.rb
|
113
|
+
- lib/acs/ldap/model.rb
|
114
|
+
- lib/acs/ldap/result.rb
|
115
|
+
- lib/acs/ldap/version.rb
|
116
|
+
- log/.keep
|
117
|
+
- log/acs_ldap.log
|
118
|
+
- spec/acs/ldap/connector_spec.rb
|
119
|
+
- spec/acs/ldap/model_spec.rb
|
120
|
+
- spec/acs/ldap_spec.rb
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
homepage: ''
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.4.5
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: ActiveRecord to LDAP adapter
|
146
|
+
test_files:
|
147
|
+
- spec/acs/ldap/connector_spec.rb
|
148
|
+
- spec/acs/ldap/model_spec.rb
|
149
|
+
- spec/acs/ldap_spec.rb
|
150
|
+
- spec/spec_helper.rb
|