route_authorizer 0.0.1

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: c29f1203fb4a70cdc02d3b08dc2342fcdae937db
4
+ data.tar.gz: c3e42e5a4604048c46b0fc18edaee4b8552df0fe
5
+ SHA512:
6
+ metadata.gz: be6bfb7c0c8999d00c3710b7702339eb83eb061e2e3519a65e9bf60c65f7b55a67ba3271cb313aa26981c6c51d813e78785d87d6cd292f78c849e8e84f2f7cce
7
+ data.tar.gz: 304d3e3f5c65e38bf6dac7458d1af3aeb0538e4bd0ecc86f936ad9ae509fc717623bd7226b99cd07a5179033c4206b4f4a93723afe5a17bc3d8ca222c38bdc05
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Fábio Rodrigues
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,29 @@
1
+ # RouteAuthorizer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'route_authorizer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install route_authorizer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/route_authorizer/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,28 @@
1
+ module RouteAuthorizer::Authorizer
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ class AccessDenied < StandardError; end
6
+
7
+ included do
8
+ helper_method :can_redirect_to?, :can_redirect_to_path?
9
+ end
10
+
11
+ private
12
+
13
+ def can_redirect_to?(_controller_name, _action_name)
14
+ Permission.new(current_user.role).redirect_to?(_controller_name, _action_name) if current_user
15
+ end
16
+
17
+ def can_redirect_to_path?(path)
18
+ controller_and_action = Rails.application.routes.recognize_path(path).values[0..1]
19
+ can_redirect_to?(*controller_and_action)
20
+ end
21
+
22
+ def authorize_user!
23
+ unless can_redirect_to?(controller_name, action_name)
24
+ raise AccessDenied.new("Acess denied to '#{controller_name}##{action_name}'") if current_user
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,32 @@
1
+ module RouteAuthorizer::Permission
2
+
3
+ def initialize(role)
4
+ @role = role
5
+ end
6
+
7
+ def redirect_to?(controller_name, action_name)
8
+ redirect_to_action? [
9
+ [:all],
10
+ [controller_name.to_sym, :all],
11
+ [controller_name.to_sym, action_name.to_sym],
12
+ ]
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :role
18
+
19
+ def redirect_to_action?(role_action)
20
+ (role_permissions & role_action).any?
21
+ end
22
+
23
+ def role_permissions
24
+ others_permissions = respond_to?(role, true) ? send(role) : []
25
+ default + others_permissions
26
+ end
27
+
28
+ def default
29
+ []
30
+ end
31
+
32
+ end
@@ -0,0 +1,3 @@
1
+ module RouteAuthorizer
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,7 @@
1
+ module RouteAuthorizer
2
+ require 'active_support'
3
+
4
+ require 'route_authorizer/version'
5
+ require 'route_authorizer/permission'
6
+ require 'route_authorizer/authorizer'
7
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'route_authorizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'route_authorizer'
8
+ spec.version = RouteAuthorizer::VERSION
9
+ spec.authors = ['Fábio Rodrigues']
10
+ spec.email = ['fabio.info@gmail.com']
11
+ spec.summary = 'Simple routes authorization solution for Rails based on user roles.'
12
+ spec.homepage = 'https://github.com/FabioMR/route_authorizer'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'rails', '>= 4.0.0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.5'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'pry-meta'
26
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe RouteAuthorizer::Authorizer do
4
+
5
+ it 'extends active support concern' do
6
+ included_modules = RouteAuthorizer::Authorizer.singleton_class.included_modules
7
+ expect(included_modules).to be_include ActiveSupport::Concern
8
+ end
9
+
10
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe RouteAuthorizer::Permission do
4
+
5
+ let(:permission_class) do
6
+ klass = Class.new
7
+ klass.include(RouteAuthorizer::Permission)
8
+ klass
9
+ end
10
+
11
+ let(:permission) { permission_class.new(:admin) }
12
+
13
+ it 'returns no permission by default' do
14
+ expect(permission.send(:role_permissions)).to eq([])
15
+ end
16
+
17
+ it 'returns default permissions' do
18
+ allow(permission).to receive(:default).and_return [1]
19
+ expect(permission.send(:role_permissions)).to eq [1]
20
+ end
21
+
22
+ it 'returns role permissions' do
23
+ allow(permission).to receive(:admin).and_return [2]
24
+ expect(permission.send(:role_permissions)).to eq [2]
25
+ end
26
+
27
+ it 'returns default and role permissions' do
28
+ allow(permission).to receive(:default).and_return [1]
29
+ allow(permission).to receive(:admin).and_return [2]
30
+ expect(permission.send(:role_permissions)).to eq [1, 2]
31
+ end
32
+
33
+ it 'permits define permission to all controllers and actions' do
34
+ allow(permission).to receive(:admin).and_return [[:all]]
35
+ expect(permission.redirect_to? :any, :any).to be_truthy
36
+ end
37
+
38
+ it 'permits define permission to a specific controller and all actions' do
39
+ allow(permission).to receive(:admin).and_return [[:some, :all]]
40
+ expect(permission.redirect_to? :some, :any).to be_truthy
41
+ expect(permission.redirect_to? :other, :any).to be_falsey
42
+ end
43
+
44
+ it 'permits define permission to a specific controller and action' do
45
+ allow(permission).to receive(:admin).and_return [[:some, :some]]
46
+ expect(permission.redirect_to? :some, :some).to be_truthy
47
+ expect(permission.redirect_to? :some, :any).to be_falsey
48
+ expect(permission.redirect_to? :any, :any).to be_falsey
49
+ end
50
+
51
+ end
@@ -0,0 +1,2 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default, :development)
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: route_authorizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fábio Rodrigues
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
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: pry-meta
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - fabio.info@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/route_authorizer.rb
97
+ - lib/route_authorizer/authorizer.rb
98
+ - lib/route_authorizer/permission.rb
99
+ - lib/route_authorizer/version.rb
100
+ - route_authorizer.gemspec
101
+ - spec/authorizer_spec.rb
102
+ - spec/permission_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: https://github.com/FabioMR/route_authorizer
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.5
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Simple routes authorization solution for Rails based on user roles.
128
+ test_files:
129
+ - spec/authorizer_spec.rb
130
+ - spec/permission_spec.rb
131
+ - spec/spec_helper.rb