tcs-ldap-permission 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2025fb81cee1f349807d52ca15b280a46cb1176
4
+ data.tar.gz: 22e2e6c8d17d5c273cc300abd0e7993994b63264
5
+ SHA512:
6
+ metadata.gz: 463819ac04e04f5a5703e0fea5b4fbade157a1e0f5bf27a40116df6599b5507917c0dba5ceb3bfa0689f20f716dc0209e9e17826d3696f5466ccc21c3d4e1d3f
7
+ data.tar.gz: 16fef793d951f59c0d42fc8749a1c11acb38e7d406673d41a567412519ac1e68dc927131498d1fb315c81ba1d59f2a90f64ab159d3136ff16add0765738716ac
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # Ignore RubyMine
11
+ /.idea
12
+
13
+ # Ignore mac files
14
+ .DS_Store
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ inherit_gem:
2
+ tcs-rubocop:
3
+ - default.yml
4
+
5
+ AllCops:
6
+ Exclude:
7
+ - bin/*
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
+
6
+ # Specify your gem's dependencies in tcs-ldap-permission.gemspec
7
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tcs-ldap-permission (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ minitest (5.11.3)
11
+ parallel (1.12.1)
12
+ parser (2.5.0.3)
13
+ ast (~> 2.4.0)
14
+ powerpack (0.1.1)
15
+ rainbow (3.0.0)
16
+ rake (10.5.0)
17
+ rubocop (0.52.1)
18
+ parallel (~> 1.10)
19
+ parser (>= 2.4.0.2, < 3.0)
20
+ powerpack (~> 0.1)
21
+ rainbow (>= 2.2.2, < 4.0)
22
+ ruby-progressbar (~> 1.7)
23
+ unicode-display_width (~> 1.0, >= 1.0.1)
24
+ ruby-progressbar (1.9.0)
25
+ tcs-rubocop (0.3.0)
26
+ rubocop (= 0.52.1)
27
+ unicode-display_width (1.3.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.16)
34
+ minitest (~> 5.0)
35
+ rake (~> 10.0)
36
+ tcs-ldap-permission!
37
+ tcs-rubocop
38
+
39
+ BUNDLED WITH
40
+ 1.16.0
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Tcs::Ldap::Permission Gem
2
+ An easy way to map LDAP groups to application roles
3
+
4
+ ## Installation
5
+
6
+ This supports Rails 4.2 and above.
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'tcs-ldap-permission'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install tcs-ldap-permission
21
+
22
+ ## Usage
23
+
24
+ Add a `config/ldap_perimissions.yml` configuration file, e.g.
25
+ ```yaml
26
+ <%= Rails.env %>:
27
+ groups:
28
+ PAYROLL_LINK_KRONOS SEC:
29
+ - manage_job_codes
30
+ IS Directors:
31
+ - superuser
32
+ users:
33
+ jonny@appleseed.com:
34
+ - approve_job_codes
35
+ ```
36
+
37
+ In your `User` class, include the `Tcs::Ldap::Permission` module
38
+
39
+ ```ruby
40
+ class User
41
+ include Tcs::Ldap::Permission
42
+ ...
43
+ ```
44
+
45
+ Now you can check authorization in your controller
46
+ ```ruby
47
+ before_action :authorize!
48
+ ...
49
+ def authorize!
50
+ unless current_user.can?(:manage_job_codes)
51
+ flash[:error] = I18n.t "devise.failure.unauthorized"
52
+ redirect_to(root_path)
53
+ end
54
+ end
55
+
56
+ ```
57
+
58
+ ### Configuration
59
+
60
+ The `config/ldap_perimissions.yml` file specifies what actions are available to a group or user.
61
+ It is preferable to only use groups so that the `config/ldap_perimissions.yml` doesn't
62
+ need to be updated every time a person moves into or out of a role.
63
+ In situations where it doesn't make sense to create a group, you can specify individual
64
+ users by their login.
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies.
69
+ Then, run `rake test` to run the tests.
70
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
71
+
72
+ To install this gem onto your local machine, run `bundle exec rake install`.
73
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
74
+ which will create a git tag for the version, push git commits and tags,
75
+ and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/the-container-store/Tcs-Ldap-Permission-Gem.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tcs/ldap/permission"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ # models an LDAP group specifier, like
3
+ # CN=pbadmins,OU=Security Groups,OU=Group Accounts,DC=containerstore,DC=com
4
+ # as an array of two-element arrays, with lowest-level nodes being first
5
+ # in the array
6
+ # [
7
+ # ['CN', 'pbadmins'],
8
+ # ['OU', 'Security Groups'],
9
+ # ['OU', 'Group Accounts'],
10
+ # ['DC', 'containerstore'],
11
+ # ['DC', 'com']
12
+ # ]
13
+ class Tcs::Ldap::Group
14
+ def initialize(group_string)
15
+ @original_string = group_string
16
+ # CN=pb\, admins,OU=Security Groups\=good,OU=Group Accounts,DC=containerstore,DC=com
17
+ string_with_escaped_commas_replaced = group_string.gsub(/\\,/, ";;")
18
+ # CN=pb$$ admins,OU=Security Groups\=good,OU=Group Accounts,DC=containerstore,DC=com
19
+ nodes_with_commas_replaced = string_with_escaped_commas_replaced.split(",")
20
+ # ['CN=pb$$ admins', 'OU=Security Groups\=good', 'OU=Group Accounts', 'DC=containerstore', 'DC=com']
21
+ node_strings = nodes_with_commas_replaced.collect { |n| n.gsub(/;;/, ",") }
22
+ # ['CN=pb, admins', 'OU=Security Groups\=good', 'OU=Group Accounts', 'DC=containerstore', 'DC=com']
23
+ node_strings_with_equals_replaced = node_strings.collect { |n| n.gsub(/\\=/, ";;") }
24
+ # ['CN=pb, admins', 'OU=Security Groups$$good', 'OU=Group Accounts', 'DC=containerstore', 'DC=com']
25
+ nodes_with_equals_replaced = node_strings_with_equals_replaced.collect { |n| n.split("=") }
26
+ # [['CN', 'pb, admins'], ['OU', 'Security Groups$$good'],
27
+ # ['OU', 'Group Accounts'], ['DC', 'containerstore'], ['DC', 'com']]
28
+ @nodes = nodes_with_equals_replaced.collect { |n| [n.first, n.last.gsub(/;;/, "=")] }
29
+ end
30
+
31
+ def cn
32
+ @nodes.first.last
33
+ end
34
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+ require "tcs/ldap/permission/version"
3
+ require "tcs/ldap/group"
4
+
5
+ module Tcs
6
+ module Ldap
7
+ module Permission
8
+ module ClassMethods
9
+ def ldap_permissions
10
+ @ldap_permissions ||= read_ldap_permissions_config
11
+ end
12
+
13
+ def configured_ldap_groups
14
+ ldap_permissions[:groups].keys
15
+ end
16
+
17
+ private
18
+ def read_ldap_permissions_config
19
+ Rails.application.config_for(:ldap_permissions).symbolize_keys
20
+ end
21
+ end
22
+
23
+ def self.included(klass)
24
+ klass.extend(ClassMethods)
25
+ end
26
+
27
+ def groups
28
+ @groups ||= fetch_groups
29
+ end
30
+
31
+ def authorized_actions
32
+ @authorized_actions ||= determine_authorized_actions
33
+ end
34
+
35
+ def can?(action)
36
+ superuser? || authorized_actions.include?(action.to_sym)
37
+ end
38
+
39
+ def superuser?
40
+ authorized_actions.include?(:superuser)
41
+ end
42
+
43
+ private
44
+
45
+ def fetch_groups
46
+ ldap_array = ldap(:memberof)
47
+ return [] if ldap_array.nil?
48
+ groups = ldap_array.collect { |g| Tcs::Ldap::Group.new(g) }
49
+ groups.collect(&:cn) & self.class.configured_ldap_groups
50
+ end
51
+
52
+ def ldap(attr)
53
+ Devise::LDAP::Adapter.get_ldap_param(username, attr.to_s)
54
+ end
55
+
56
+ def determine_authorized_actions
57
+ actions = groups.map {|group| self.class.ldap_permissions.dig(:groups, group)}
58
+ actions << user_specific_actions
59
+ actions.flatten.compact.uniq.map(&:to_sym)
60
+ end
61
+
62
+ def user_specific_actions
63
+ self.class.ldap_permissions.dig(:users, username)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ module Tcs
3
+ module Ldap
4
+ module Permission
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "tcs/ldap/permission/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "tcs-ldap-permission"
9
+ spec.version = Tcs::Ldap::Permission::VERSION
10
+ spec.authors = ["Ed Wagner"]
11
+ spec.email = ["ewwagner@containerstore.com"]
12
+
13
+ spec.summary = %q{An easy way to map LDAP groups to application roles}
14
+ spec.homepage = "https://github.com/the-container-store/Tcs-Ldap-Permission-Gem"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "tcs-rubocop"
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tcs-ldap-permission
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ed Wagner
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-13 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tcs-rubocop
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
+ description:
70
+ email:
71
+ - ewwagner@containerstore.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/tcs/ldap/group.rb
85
+ - lib/tcs/ldap/permission.rb
86
+ - lib/tcs/ldap/permission/version.rb
87
+ - tcs-ldap-permission.gemspec
88
+ homepage: https://github.com/the-container-store/Tcs-Ldap-Permission-Gem
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.14
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: An easy way to map LDAP groups to application roles
111
+ test_files: []