hydra-ldap 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hydra-ldap.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
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,39 @@
1
+ # Hydra::LDAP
2
+
3
+ A gem for managing ldap groups used with hydra
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hydra-ldap'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hydra-ldap
18
+
19
+ ## Usage
20
+
21
+ Create the config file (config/ldap.yml) by running:
22
+
23
+ <pre>rails generate hydra-ldap</pre>
24
+
25
+
26
+ <pre>Hydra::LDAP.create_group(group_code, description, owner, users)</pre>
27
+ <pre>Hydra::LDAP.groups_for_user(user_id)</pre>
28
+ <pre>Hydra::LDAP.groups_owned_by_user(user_id)</pre>
29
+ <pre>Hydra::LDAP.delete_group(group_code)</pre>
30
+ <pre>Hydra::LDAP.add_users_to_group(group_code, users)</pre>
31
+ <pre>Hydra::LDAP.remove_users_from_group(group_code, users)</pre>
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
8
+
9
+ desc "Run specs"
10
+ RSpec::Core::RakeTask.new do |t|
11
+ end
12
+
@@ -0,0 +1,9 @@
1
+ test:
2
+ host: ldap.example.com
3
+ port: 389
4
+ username: cn=Manager,dc=example,dc=com
5
+ password: <%= ENV['LDAP_PASSWORD'] %>
6
+ group_base: ou=groups,dc=example,dc=com
7
+ base: ou=people,dc=example,dc=com
8
+ uid: uid
9
+
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hydra/ldap/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Justin Coyne"]
6
+ gem.email = ["justin.coyne@yourmediashelf.com"]
7
+ gem.description = %q{A gem for managing groups with ldap}
8
+ gem.summary = %q{Create, Read and Update LDAP groups}
9
+ gem.homepage = "https://github.com/projecthydra/hydra-ldap"
10
+
11
+ gem.add_dependency('net-ldap')
12
+
13
+ gem.add_development_dependency('rake')
14
+ gem.add_development_dependency('rspec')
15
+
16
+
17
+ gem.files = `git ls-files`.split($\)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.name = "hydra-ldap"
21
+ gem.require_paths = ["lib"]
22
+ gem.version = Hydra::LDAP::VERSION
23
+ end
@@ -0,0 +1,7 @@
1
+ class ConfigGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_config_file
5
+     copy_file "hydra-ldap.yml", "config/hydra-ldap.yml"
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ development:
2
+ host: dirapps.aset.psu.edu
3
+ port: 389
4
+ # username: cn=admin,dc=example,dc=com
5
+ # password: simple77
6
+ group_base: dc=psu,dc=edu
7
+ # base: dc=example,dc=com
8
+ # uid: uid
9
+ test:
10
+ host: dirapps.aset.psu.edu
11
+ port: 389
12
+ username: cn=admin,dc=example,dc=com
13
+ password: simple77
14
+ group_base: dc=psu,dc=edu
15
+ base: dc=example,dc=com
16
+ uid: uid
17
+ production:
18
+ host: dirapps.aset.psu.edu
19
+ port: 389
20
+ username: cn=admin,dc=example,dc=com
21
+ password: <%= ENV['LDAP_PASSWORD'] %>
22
+ group_base: dc=psu,dc=edu
23
+ base: dc=northwestern,dc=edu
24
+ uid: uid
25
+
26
+
data/lib/hydra-ldap.rb ADDED
@@ -0,0 +1,134 @@
1
+ require "hydra/ldap/version"
2
+ require "net/ldap"
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+ require 'yaml'
6
+
7
+ module Hydra
8
+ module LDAP
9
+
10
+ # Your code goes here...
11
+ class NoUsersError < StandardError; end
12
+ class MissingOwnerError < StandardError; end
13
+ class GroupNotFound < StandardError; end
14
+
15
+ def self.connection
16
+ @ldap_conn ||= Net::LDAP.new(ldap_connection_config)
17
+ end
18
+
19
+ def self.ldap_connection_config
20
+ return @ldap_connection_config if @ldap_connection_config
21
+ @ldap_connection_config = {}
22
+ yml = ldap_config
23
+ @ldap_connection_config[:host] = yml[:host]
24
+ @ldap_connection_config[:port] = yml[:port]
25
+ if yml[:username] && yml[:password]
26
+ @ldap_connection_config[:auth]={:method=>:simple}
27
+ @ldap_connection_config[:auth][:username] = yml[:username]
28
+ @ldap_connection_config[:auth][:password] = yml[:password]
29
+ end
30
+ @ldap_connection_config
31
+ end
32
+
33
+ def self.ldap_config
34
+ root = defined?(Rails) ? Rails.root : '.'
35
+ env = defined?(Rails) ? Rails.env : 'test'
36
+ @ldap_config ||= YAML::load(ERB.new(IO.read(File.join(root, 'config', 'hydra-ldap.yml'))).result)[env].with_indifferent_access
37
+ end
38
+
39
+ def self.group_base
40
+ ldap_config[:group_base]
41
+ end
42
+
43
+ def self.treebase
44
+ ldap_config[:base]
45
+ end
46
+
47
+ def self.dn(code)
48
+ dn = "cn=#{code},#{group_base}"
49
+ end
50
+
51
+ def self.create_group(code, description, owner, users)
52
+ raise NoUsersError, "Unable to persist a group without users" unless users.present?
53
+ raise MissingOwnerError, "Unable to persist a group without owner" unless owner
54
+ attributes = {
55
+ :cn => code,
56
+ :objectclass => "groupofnames",
57
+ :description => description,
58
+ :member=>users.map {|u| "uid=#{u}"},
59
+ :owner=>"uid=#{owner}"
60
+ }
61
+ connection.add(:dn=>dn(code), :attributes=>attributes)
62
+ end
63
+
64
+ def self.delete_group(code)
65
+ Hydra::LDAP.connection.delete(:dn=>dn(code))
66
+ end
67
+
68
+ # same as
69
+ # ldapsearch -h ec2-107-20-53-121.compute-1.amazonaws.com -p 389 -x -b dc=example,dc=com -D "cn=admin,dc=example,dc=com" -W "(&(objectClass=groupofnames)(member=uid=vanessa))" cn
70
+ def self.groups_for_user(uid)
71
+ result = Hydra::LDAP.connection.search(:base=>group_base, :filter=> Net::LDAP::Filter.construct("(&(objectClass=groupofnames)(member=uid=#{uid}))"), :attributes=>['cn'])
72
+ result.map{|r| r[:cn].first}
73
+ end
74
+
75
+ def self.groups_owned_by_user(uid)
76
+ result = Hydra::LDAP.connection.search(:base=>group_base, :filter=> Net::LDAP::Filter.construct("(&(objectClass=groupofnames)(owner=uid=#{uid}))"), :attributes=>['cn'])
77
+ result.map{|r| r[:cn].first}
78
+ end
79
+ def self.title_of_group(group_code)
80
+ result = find_group(group_code)
81
+ result[:description].first
82
+ end
83
+
84
+ def self.users_for_group(group_code)
85
+ result = find_group(group_code)
86
+ result[:member].map { |v| v.sub(/^uid=/, '') }
87
+ end
88
+
89
+ def self.owner_for_group(group_code)
90
+ result = find_group(group_code)
91
+ result[:owner].first.sub(/^uid=/, '')
92
+ end
93
+
94
+ def self.add_users_to_group(group_code, users)
95
+ invalidate_cache(group_code)
96
+ ops = []
97
+ users.each do |u|
98
+ ops << [:add, :member, "uid=#{u}"]
99
+ end
100
+ connection.modify(:dn=>dn(group_code), :operations=>ops)
101
+ end
102
+
103
+ def self.remove_users_from_group(group_code, users)
104
+ invalidate_cache(group_code)
105
+ ops = []
106
+ users.each do |u|
107
+ ops << [:delete, :member, "uid=#{u}"]
108
+ end
109
+ connection.modify(:dn=>dn(group_code), :operations=>ops)
110
+ end
111
+
112
+ def self.invalidate_cache(group_code)
113
+ @cache ||= {}
114
+ @cache[group_code] = nil
115
+ end
116
+
117
+ def self.find_group(group_code)
118
+ @cache ||= {}
119
+ return @cache[group_code] if @cache[group_code]
120
+ result = Hydra::LDAP.connection.search(:base=>group_base, :filter=> Net::LDAP::Filter.construct("(&(objectClass=groupofnames)(cn=#{group_code}))"), :attributes=>['member', 'owner', 'description'])
121
+ val = {}
122
+ raise GroupNotFound, "Can't find group '#{group_code}' in ldap" unless result.first
123
+ result.first.each do |k, v|
124
+ val[k] = v
125
+ end
126
+ #puts "Val is: #{val}"
127
+ @cache[group_code] = val
128
+ end
129
+
130
+ end
131
+ end
132
+
133
+ require 'hydra/ldap/engine' if defined?(Rails)
134
+
@@ -0,0 +1,7 @@
1
+ module Hydra
2
+ module LDAP
3
+ class Engine < Rails::Engine
4
+ end
5
+ end
6
+ end
7
+
@@ -0,0 +1,5 @@
1
+ module Hydra
2
+ module LDAP
3
+ VERSION = "0.0.3"
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Ldap service' do
4
+ before do
5
+ # If this line isn't true, there was a problem creating (probably already exists.
6
+ Hydra::LDAP.create_group('justin1', 'Test Group', 'quentin', ['kacey', 'larry', 'ursula']).should be_true
7
+ end
8
+ after do
9
+ Hydra::LDAP.delete_group('justin1').should be_true
10
+ end
11
+ it "should have description, users, owners of a group" do
12
+ Hydra::LDAP.title_of_group('justin1').should == 'Test Group'
13
+ Hydra::LDAP.users_for_group('justin1').should == ['kacey', 'larry', 'ursula']
14
+ Hydra::LDAP.owner_for_group('justin1').should == 'quentin'
15
+ end
16
+
17
+ describe "#groups_owned_by_user" do
18
+ before do
19
+ Hydra::LDAP.create_group('justin2', 'Test Group', 'quentin', ['kacey', 'larry']).should be_true
20
+ Hydra::LDAP.create_group('justin3', 'Test Group', 'theresa', ['kacey', 'larry']).should be_true
21
+ end
22
+ after do
23
+ Hydra::LDAP.delete_group('justin2').should be_true
24
+ Hydra::LDAP.delete_group('justin3').should be_true
25
+ end
26
+ it "should return the list" do
27
+ Hydra::LDAP.groups_owned_by_user('quentin').should == ['justin1', 'justin2']
28
+ end
29
+ end
30
+ describe "#adding_members" do
31
+ it "should have users and owners of a group" do
32
+ Hydra::LDAP.add_users_to_group('justin1', ['theresa', 'penelope']).should be_true
33
+ Hydra::LDAP.users_for_group('justin1').should == ['kacey', 'larry', 'ursula', 'theresa', 'penelope']
34
+ end
35
+ end
36
+ describe "#removing_members" do
37
+ it "should remove users from the group" do
38
+ Hydra::LDAP.remove_users_from_group('justin1', ['kacey', 'larry']).should be_true
39
+ Hydra::LDAP.users_for_group('justin1').should == ['ursula']
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec/autorun'
5
+ require 'hydra-ldap'
6
+ RSpec.configure do |config|
7
+
8
+ end
9
+
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hydra-ldap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Coyne
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ldap
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A gem for managing groups with ldap
63
+ email:
64
+ - justin.coyne@yourmediashelf.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - config/hydra-ldap.yml
75
+ - hydra-ldap.gemspec
76
+ - lib/generators/hydra-ldap/config_generator.rb
77
+ - lib/generators/hydra-ldap/templates/hydra-ldap.yml
78
+ - lib/hydra-ldap.rb
79
+ - lib/hydra/ldap/engine.rb
80
+ - lib/hydra/ldap/version.rb
81
+ - spec/integration/ldap_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: https://github.com/projecthydra/hydra-ldap
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.24
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Create, Read and Update LDAP groups
107
+ test_files:
108
+ - spec/integration/ldap_spec.rb
109
+ - spec/spec_helper.rb