route_authorizer 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e3a0f2ada77c7eb4c8e74711f6ea639ada66ca8
4
- data.tar.gz: 843853d3e80d386fe0838116d1615d358c3940c5
3
+ metadata.gz: ee4e003200b9e3f952427983a096e42ae56337a3
4
+ data.tar.gz: 4c2e08ef064e8565f44dedc540b99a5606b2df77
5
5
  SHA512:
6
- metadata.gz: ccb85cbe5d2ce0cf939cb08220eadc6189b8e1f9f920bc81b242f8dd5f543e5ac59a5282f375688982a24dc7a215d0643f7067cf7724331347fc4bed3795f8c2
7
- data.tar.gz: 0e1f6d4bf1b393b932f609a1594d88e3fb56a4efd68f052ca371bbcea26bf122c40311868860498004edcbd0df28bfb7e5fa35639ec06373f77ede0b0dfc80f4
6
+ metadata.gz: 5ef1bcd4349b97877ce9c00fac2d9697b196d59cee610f5099b8c27aa17841ca8e7ad4d14a6084d2398fc4b482c492ed1a216d0a4ded91fc06c32d53ace1be3a
7
+ data.tar.gz: 8295aded4b8761a0c80ba9b08669891f0176440a7fdcd487638e706dca4654133a41fc2001df08a773393a1a6228983d90524c8e49a3cac3efd3ed23361bb484
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate install Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,7 @@
1
+ class RouteAuthorizer::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def generate_permission
5
+ copy_file "permission.rb", "app/models/permission.rb"
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ class Permission < RouteAuthorizer::Permission
2
+
3
+ # Users can access all actions of HomeController
4
+ # all_roles do
5
+ # permit :home
6
+ # end
7
+
8
+ # Admin can access all controllers and actions
9
+ # role :admin do
10
+ # permit_all
11
+ # end
12
+
13
+ # Staff can access all actions of ProductsController
14
+ # role :staff do
15
+ # permit :products
16
+ # end
17
+
18
+ # Customer can access all actions of OrdersController and just actions Index and Show of ProductsController
19
+ # role :customer do
20
+ # permit :orders
21
+ # permit :products, only: [:index, :show]
22
+ # end
23
+
24
+ end
@@ -14,8 +14,8 @@ private
14
14
  @permission ||= ::Permission.new(current_user.try(:role))
15
15
  end
16
16
 
17
- def permit?(_controller_name, _action_name)
18
- permission.permit?(_controller_name, _action_name)
17
+ def permit?(_controller_path, _action_name)
18
+ permission.permit?(_controller_path, _action_name)
19
19
  end
20
20
 
21
21
  def permit_path?(path)
@@ -24,8 +24,8 @@ private
24
24
  end
25
25
 
26
26
  def authorize_user!
27
- unless permit?(controller_name, action_name)
28
- raise AccessDenied.new("Acess denied to '#{controller_name}##{action_name}'")
27
+ unless permit?(controller_path.to_s.gsub(/\//, "_"), action_name)
28
+ raise AccessDenied.new("Acess denied to '#{controller_path}##{action_name}'")
29
29
  end
30
30
  end
31
31
 
@@ -6,11 +6,11 @@ class RouteAuthorizer::Permission
6
6
  @role = role.to_s
7
7
  end
8
8
 
9
- def permit?(controller_name, action_name)
9
+ def permit?(controller_path, action_name)
10
10
  permit_action? [
11
11
  [:all],
12
- [controller_name.to_sym, :all],
13
- [controller_name.to_sym, action_name.to_sym],
12
+ [controller_path.to_sym, :all],
13
+ [controller_path.to_sym, action_name.to_sym],
14
14
  ]
15
15
  end
16
16
 
@@ -1,3 +1,3 @@
1
1
  module RouteAuthorizer
2
- VERSION = '0.0.5'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: route_authorizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fábio Rodrigues
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-23 00:00:00.000000000 Z
11
+ date: 2015-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -87,22 +87,14 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - ".gitignore"
91
- - ".rspec"
92
- - Gemfile
93
- - LICENSE.txt
94
- - README.md
95
- - Rakefile
90
+ - lib/generators/route_authorizer/install/USAGE
91
+ - lib/generators/route_authorizer/install/install_generator.rb
92
+ - lib/generators/route_authorizer/install/templates/permission.rb
96
93
  - lib/route_authorizer.rb
97
94
  - lib/route_authorizer/authorizer.rb
98
95
  - lib/route_authorizer/permission.rb
99
96
  - lib/route_authorizer/permission_dsl.rb
100
97
  - lib/route_authorizer/version.rb
101
- - route_authorizer.gemspec
102
- - spec/authorizer_spec.rb
103
- - spec/permission_dsl_spec.rb
104
- - spec/permission_spec.rb
105
- - spec/spec_helper.rb
106
98
  homepage: https://github.com/FabioMR/route_authorizer
107
99
  licenses:
108
100
  - MIT
@@ -127,8 +119,4 @@ rubygems_version: 2.4.5
127
119
  signing_key:
128
120
  specification_version: 4
129
121
  summary: Simple routes authorization solution for Rails based on user roles.
130
- test_files:
131
- - spec/authorizer_spec.rb
132
- - spec/permission_dsl_spec.rb
133
- - spec/permission_spec.rb
134
- - spec/spec_helper.rb
122
+ test_files: []
data/.gitignore DELETED
@@ -1,17 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- --color
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
@@ -1,22 +0,0 @@
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 DELETED
@@ -1,29 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- require 'bundler/gem_tasks'
@@ -1,26 +0,0 @@
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 'byebug'
26
- end
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RouteAuthorizer::Authorizer do
4
-
5
- let(:role) { :admin }
6
- let(:current_user) { double('User', role: role) }
7
- let(:permission) { double('permission', permit?: true) }
8
- let(:controller) { ActionController::Base.new }
9
-
10
- before do
11
- allow(Permission).to receive(:new) { permission }
12
-
13
- allow(controller).to receive(:controller_name) { :controller }
14
- allow(controller).to receive(:action_name) { :action }
15
- allow(controller).to receive(:current_user) { current_user }
16
- end
17
-
18
- context 'when has a current user' do
19
- before do
20
- expect(Permission).to receive(:new).with(role)
21
- end
22
-
23
- it 'returns current user role' do
24
- controller.send(:permission)
25
- end
26
- end
27
-
28
- context 'when does not have a current user' do
29
- before do
30
- allow(controller).to receive(:current_user) { nil }
31
- end
32
-
33
- it 'returns no role' do
34
- expect(Permission).to receive(:new).with(nil)
35
- controller.send(:permission)
36
- end
37
- end
38
-
39
- context 'when user has permission' do
40
- before do
41
- expect(permission).to receive(:permit?).with(:controller, :action) { true }
42
- end
43
-
44
- it 'raises no exception' do
45
- expect {controller.send(:authorize_user!)}.not_to raise_error
46
- end
47
- end
48
-
49
- context 'when user does not have permission' do
50
- before do
51
- expect(permission).to receive(:permit?).with(:controller, :action) { false }
52
- end
53
-
54
- it 'raises AccessDenied exception' do
55
- expect {controller.send(:authorize_user!)}.to raise_error(RouteAuthorizer::Authorizer::AccessDenied)
56
- end
57
- end
58
-
59
- it '#permit?' do
60
- expect(permission).to receive(:permit?).with(:other_controller, :other_action)
61
- controller.send(:permit?, :other_controller, :other_action)
62
- end
63
-
64
- it '#permit_path?' do
65
- expect(Rails).to receive_message_chain(:application, :routes, :recognize_path).with('path') { {a: 1, b: 2, c: 3} }
66
- expect(permission).to receive(:permit?).with(1, 2)
67
- controller.send(:permit_path?, 'path')
68
- end
69
-
70
- end
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RouteAuthorizer::PermissionDSL do
4
-
5
- let(:permission_class) { Class.new }
6
- let(:permission) { permission_class.new }
7
-
8
- before do
9
- permission_class.include(RouteAuthorizer::PermissionDSL)
10
- end
11
-
12
- it '.all_roles' do
13
- expect(permission_class).to receive(:role).with(:default).and_yield
14
-
15
- permission_class.send(:all_roles) { :anything }
16
- end
17
-
18
- context '.role' do
19
- it 'with no permission' do
20
- permission_class.send(:role, :admin) {}
21
-
22
- expect(permission.send(:admin)).to eq([])
23
- end
24
-
25
- it 'with all permission' do
26
- permission_class.send(:role, :admin) do
27
- permit_all
28
- end
29
-
30
- expect(permission.send(:admin)).to eq([[:all]])
31
- end
32
-
33
- it 'with controller permission' do
34
- permission_class.send(:role, :admin) do
35
- permit :controller1
36
- permit :controller2
37
- end
38
-
39
- expect(permission.send(:admin)).to eq([
40
- [:controller1, :all],
41
- [:controller2, :all],
42
- ])
43
- end
44
-
45
- it 'with controller and action permissions' do
46
- permission_class.send(:role, :admin) do
47
- permit :controller1, only: [:action1]
48
- permit :controller2, only: [:action1, :action2]
49
- end
50
-
51
- expect(permission.send(:admin)).to eq([
52
- [:controller1, :action1],
53
- [:controller2, :action1],
54
- [:controller2, :action2],
55
- ])
56
- end
57
- end
58
-
59
- end
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RouteAuthorizer::Permission do
4
-
5
- let(:permission_class) { Class.new(RouteAuthorizer::Permission) }
6
- let(:permission) { permission_class.new(@role) }
7
-
8
- before do
9
- @role = :admin
10
- end
11
-
12
- it 'includes DSL' do
13
- expect(permission_class).to include RouteAuthorizer::PermissionDSL
14
- end
15
-
16
- it 'returns no permission for no role' do
17
- @role = nil
18
- expect(permission.send(:role_permissions)).to eq([])
19
- end
20
-
21
- it 'returns no permission by default' do
22
- expect(permission.send(:role_permissions)).to eq([])
23
- end
24
-
25
- it 'returns default permissions' do
26
- allow(permission).to receive(:default).and_return [1]
27
- expect(permission.send(:role_permissions)).to eq [1]
28
- end
29
-
30
- it 'returns role permissions' do
31
- allow(permission).to receive(:admin).and_return [2]
32
- expect(permission.send(:role_permissions)).to eq [2]
33
- end
34
-
35
- it 'returns default and role permissions' do
36
- allow(permission).to receive(:default).and_return [1]
37
- allow(permission).to receive(:admin).and_return [2]
38
- expect(permission.send(:role_permissions)).to eq [1, 2]
39
- end
40
-
41
- it 'permits define permission to all controllers and actions' do
42
- allow(permission).to receive(:admin).and_return [[:all]]
43
- expect(permission.permit? :any, :any).to be_truthy
44
- end
45
-
46
- it 'permits define permission to a specific controller and all actions' do
47
- allow(permission).to receive(:admin).and_return [[:some, :all]]
48
- expect(permission.permit? :some, :any).to be_truthy
49
- expect(permission.permit? :other, :any).to be_falsey
50
- end
51
-
52
- it 'permits define permission to a specific controller and action' do
53
- allow(permission).to receive(:admin).and_return [[:some, :some]]
54
- expect(permission.permit? :some, :some).to be_truthy
55
- expect(permission.permit? :some, :any).to be_falsey
56
- expect(permission.permit? :any, :any).to be_falsey
57
- end
58
-
59
- end
@@ -1,4 +0,0 @@
1
- require 'bundler/setup'
2
- Bundler.require(:default, :development)
3
-
4
- Permission = Class.new(RouteAuthorizer::Permission)