ldap-relations 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +1 -0
- data/ldap-relations.gemspec +21 -0
- data/lib/ldap-relations/relation.rb +16 -0
- data/lib/ldap-relations/relation_manager.rb +33 -0
- data/lib/ldap-relations/version.rb +5 -0
- data/lib/ldap-relations.rb +10 -0
- data/spec/.rspec +1 -0
- data/spec/ldap-relations/relation_manager_spec.rb +39 -0
- data/spec/ldap-relations/relation_spec.rb +34 -0
- data/spec/spec_helper.rb +6 -0
- metadata +80 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Felix Clack
|
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,42 @@
|
|
1
|
+
# Ldap::Relations
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/felixclack/ldap-relations.png)](https://travis-ci.org/felixclack/ldap-relations)
|
4
|
+
|
5
|
+
An Arel inspired library to provide a relational algebra for LDAP filters.
|
6
|
+
I'm using it in conjunction with the jruby-ldap library because the
|
7
|
+
ruby ldap libraries just don't cut it in Ruby 1.9.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'ldap-relations'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ldap-relations
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Create a relation manager first...
|
26
|
+
|
27
|
+
manager = Ldap::Relations::RelationManager.new
|
28
|
+
|
29
|
+
manager.relations << Relation.new(sAMAccountName: 'test')
|
30
|
+
manager.relations << Relation.new(objectCategory: 'person')
|
31
|
+
|
32
|
+
manager.to_filter #=> "(&(sAMAccountName=test)(objectCategory=person)"
|
33
|
+
|
34
|
+
Then pass that filter string into the search method on your LDAP connection. Simples.
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ldap-relations/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ldap-relations"
|
8
|
+
gem.version = Ldap::Relations::VERSION
|
9
|
+
gem.authors = ["Felix Clack"]
|
10
|
+
gem.email = ["felixclack@gmail.com"]
|
11
|
+
gem.description = %q{}
|
12
|
+
gem.summary = %q{Abstraction of filters to use with an LDAP library.}
|
13
|
+
gem.homepage = "http://felixclack.github.com/ldap-relations"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Ldap::Relations
|
2
|
+
# This class describes an individual filter that can be combined with others
|
3
|
+
# to be passed to an LDAP server as part of a search.
|
4
|
+
class Relation
|
5
|
+
def initialize params={}
|
6
|
+
@operator = params.fetch :operator, :and
|
7
|
+
@params = params
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :operator, :params
|
11
|
+
|
12
|
+
def to_filter
|
13
|
+
@params.map {|key, value| "#{key}=#{value}" }.join
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Ldap
|
2
|
+
module Relations
|
3
|
+
# Store a list of relations and provide methods
|
4
|
+
# to output the list as an LDAP filter string.
|
5
|
+
class RelationManager
|
6
|
+
RELATION_OPERATOR_MAPPINGS = {
|
7
|
+
or: '|',
|
8
|
+
and: '&',
|
9
|
+
not: '!'
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize *args
|
13
|
+
@relations = []
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_accessor :relations
|
17
|
+
|
18
|
+
# Public: convert the relations to a LDAP-style filter string.
|
19
|
+
#
|
20
|
+
# Returns String
|
21
|
+
def to_filter
|
22
|
+
relation = @relations.shift
|
23
|
+
case @relations.size
|
24
|
+
when 0
|
25
|
+
"(#{relation.to_filter})"
|
26
|
+
when 1
|
27
|
+
"(" << RELATION_OPERATOR_MAPPINGS[@relations.first.operator] <<
|
28
|
+
"(#{relation.to_filter})(#{@relations.first.to_filter}))"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ldap-relations/relation_manager'
|
3
|
+
|
4
|
+
module Ldap::Relations
|
5
|
+
describe RelationManager do
|
6
|
+
let(:relation_manager) { RelationManager.new }
|
7
|
+
let(:relation) { stub 'Relation', to_filter: 'objectCategory=person', operator: :and }
|
8
|
+
let(:or_relation) { stub 'Relation', to_filter: 'sAMAccountName=test', operator: :or }
|
9
|
+
|
10
|
+
describe '#relations' do
|
11
|
+
it 'stores a list of relations' do
|
12
|
+
relation_manager.relations << relation << relation
|
13
|
+
relation_manager.relations.should == [relation, relation]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#to_filter' do
|
18
|
+
subject { relation_manager.to_filter }
|
19
|
+
|
20
|
+
context 'with one relation' do
|
21
|
+
before { relation_manager.relations << relation }
|
22
|
+
|
23
|
+
it { should == "(objectCategory=person)" }
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with 2 relations' do
|
27
|
+
before { relation_manager.relations << relation << relation }
|
28
|
+
|
29
|
+
it { should == "(&(objectCategory=person)(objectCategory=person))" }
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with an AND relation and an OR relation' do
|
33
|
+
before { relation_manager.relations << relation << or_relation }
|
34
|
+
|
35
|
+
it { should == "(|(objectCategory=person)(sAMAccountName=test))"}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ldap-relations/relation'
|
3
|
+
|
4
|
+
module Ldap::Relations
|
5
|
+
describe Relation do
|
6
|
+
let(:relation) { Relation.new sAMAccountName: 'test' }
|
7
|
+
|
8
|
+
subject { relation }
|
9
|
+
|
10
|
+
its(:params) { should == { sAMAccountName: 'test'} }
|
11
|
+
|
12
|
+
describe '#to_filter' do
|
13
|
+
subject { relation.to_filter }
|
14
|
+
|
15
|
+
it { should == 'sAMAccountName=test' }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#operator' do
|
19
|
+
context 'by default' do
|
20
|
+
subject { relation.operator }
|
21
|
+
|
22
|
+
it { should == :and }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when set to :or' do
|
26
|
+
let(:or_relation) { Relation.new sAMAccountName: 'test', operator: :or }
|
27
|
+
|
28
|
+
subject { or_relation.operator }
|
29
|
+
|
30
|
+
it { should == :or }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ldap-relations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Felix Clack
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ''
|
31
|
+
email:
|
32
|
+
- felixclack@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .travis.yml
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- ldap-relations.gemspec
|
44
|
+
- lib/ldap-relations.rb
|
45
|
+
- lib/ldap-relations/relation.rb
|
46
|
+
- lib/ldap-relations/relation_manager.rb
|
47
|
+
- lib/ldap-relations/version.rb
|
48
|
+
- spec/.rspec
|
49
|
+
- spec/ldap-relations/relation_manager_spec.rb
|
50
|
+
- spec/ldap-relations/relation_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
homepage: http://felixclack.github.com/ldap-relations
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.23
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Abstraction of filters to use with an LDAP library.
|
76
|
+
test_files:
|
77
|
+
- spec/.rspec
|
78
|
+
- spec/ldap-relations/relation_manager_spec.rb
|
79
|
+
- spec/ldap-relations/relation_spec.rb
|
80
|
+
- spec/spec_helper.rb
|