route_authorizer 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d4d097356ade4d4b4a7970ce122a62644a8bde23
4
- data.tar.gz: e69733b53d7502d2ee443cbfe8adb34c50de9925
3
+ metadata.gz: 672e998c468b5c1fb4cf8279b3ce71ce2aae3d8c
4
+ data.tar.gz: 00aa2185e514df67db452d5344209c56437ba283
5
5
  SHA512:
6
- metadata.gz: fb4fcf832f5b96ab5e4b4e8aea623bb0a42a2c4e09c55f55f49c04cfb5b217c7c96a507694d3c0596db9c13f206766b350f4b5d0a9c71e982ee1d0e8b79829ea
7
- data.tar.gz: 0589805cc20769d137ca530fcd5c9e69fc13ab917666c4947d375107663ff6075d3d01c303b0d69f3f41b153581d04344fa97b2ba911b45a1e606992ce5768df
6
+ metadata.gz: b13057a295b5be581734b8e41cd7f44c563435c405170abcf96b360f6c208ec075dff2054433220665c54761e184d36d242d354fda89938bb41ee4710fc332f5
7
+ data.tar.gz: 5b63acbb71f664ca58d6159ce520e49c0f6d50f57cffea8e808a7706a71988042ff29fe4e3d98c2f36ac02403ed228d3b6da3ee1fb7d0b1afd6823b657ac3391
@@ -4,5 +4,6 @@ module RouteAuthorizer
4
4
 
5
5
  require 'route_authorizer/version'
6
6
  require 'route_authorizer/permission'
7
+ require 'route_authorizer/permission_dsl'
7
8
  require 'route_authorizer/authorizer'
8
9
  end
@@ -5,7 +5,7 @@ module RouteAuthorizer::Authorizer
5
5
  class AccessDenied < StandardError; end
6
6
 
7
7
  included do
8
- helper_method :can_redirect_to?, :can_redirect_to_path?
8
+ helper_method :permit?, :permit_path?
9
9
  end
10
10
 
11
11
  private
@@ -14,17 +14,17 @@ private
14
14
  @permission ||= ::Permission.new(current_user.try(:role))
15
15
  end
16
16
 
17
- def can_redirect_to?(_controller_name, _action_name)
18
- permission.redirect_to?(_controller_name, _action_name)
17
+ def permit?(_controller_name, _action_name)
18
+ permission.permit?(_controller_name, _action_name)
19
19
  end
20
20
 
21
- def can_redirect_to_path?(path)
21
+ def permit_path?(path)
22
22
  controller_and_action = Rails.application.routes.recognize_path(path).values[0..1]
23
- can_redirect_to?(*controller_and_action)
23
+ permit?(*controller_and_action)
24
24
  end
25
25
 
26
26
  def authorize_user!
27
- unless can_redirect_to?(controller_name, action_name)
27
+ unless permit?(controller_name, action_name)
28
28
  raise AccessDenied.new("Acess denied to '#{controller_name}##{action_name}'")
29
29
  end
30
30
  end
@@ -4,8 +4,8 @@ module RouteAuthorizer::Permission
4
4
  @role = role.to_s
5
5
  end
6
6
 
7
- def redirect_to?(controller_name, action_name)
8
- redirect_to_action? [
7
+ def permit?(controller_name, action_name)
8
+ permit_action? [
9
9
  [:all],
10
10
  [controller_name.to_sym, :all],
11
11
  [controller_name.to_sym, action_name.to_sym],
@@ -16,7 +16,7 @@ private
16
16
 
17
17
  attr_reader :role
18
18
 
19
- def redirect_to_action?(role_action)
19
+ def permit_action?(role_action)
20
20
  (role_permissions & role_action).any?
21
21
  end
22
22
 
@@ -0,0 +1,35 @@
1
+ module RouteAuthorizer::PermissionDSL
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ include RouteAuthorizer::Permission
6
+
7
+ class_methods do
8
+ def all_roles(&block)
9
+ role(:default, &block)
10
+ end
11
+
12
+ def role(name, &block)
13
+ define_method name do
14
+ @current_role = "@#{name}"
15
+
16
+ instance_variable_set @current_role, []
17
+ instance_eval &block
18
+ instance_variable_get @current_role
19
+ end
20
+ end
21
+ end
22
+
23
+ def permit_all
24
+ instance_variable_get(@current_role) << [:all]
25
+ end
26
+
27
+ def permit(controller, options = {})
28
+ actions = options[:only] || [:all]
29
+
30
+ actions.each do |action|
31
+ instance_variable_get(@current_role) << [controller, action]
32
+ end
33
+ end
34
+
35
+ end
@@ -1,3 +1,3 @@
1
1
  module RouteAuthorizer
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- Permission = Object.new
3
+ Permission = Class.new
4
4
 
5
5
  describe RouteAuthorizer::Authorizer do
6
6
 
7
7
  let(:role) { :admin }
8
8
  let(:current_user) { double('User', role: role) }
9
- let(:permission) { double('permission', redirect_to?: true) }
9
+ let(:permission) { double('permission', permit?: true) }
10
10
  let(:controller) { ActionController::Base.new }
11
11
 
12
12
  before do
@@ -40,7 +40,7 @@ describe RouteAuthorizer::Authorizer do
40
40
 
41
41
  context 'when user has permission' do
42
42
  before do
43
- expect(permission).to receive(:redirect_to?).with(:controller, :action) { true }
43
+ expect(permission).to receive(:permit?).with(:controller, :action) { true }
44
44
  end
45
45
 
46
46
  it 'raises no exception' do
@@ -50,7 +50,7 @@ describe RouteAuthorizer::Authorizer do
50
50
 
51
51
  context 'when user does not have permission' do
52
52
  before do
53
- expect(permission).to receive(:redirect_to?).with(:controller, :action) { false }
53
+ expect(permission).to receive(:permit?).with(:controller, :action) { false }
54
54
  end
55
55
 
56
56
  it 'raises AccessDenied exception' do
@@ -58,15 +58,15 @@ describe RouteAuthorizer::Authorizer do
58
58
  end
59
59
  end
60
60
 
61
- it '#can_redirect_to?' do
62
- expect(permission).to receive(:redirect_to?).with(:other_controller, :other_action)
63
- controller.send(:can_redirect_to?, :other_controller, :other_action)
61
+ it '#permit?' do
62
+ expect(permission).to receive(:permit?).with(:other_controller, :other_action)
63
+ controller.send(:permit?, :other_controller, :other_action)
64
64
  end
65
65
 
66
- it '#can_redirect_to_path?' do
66
+ it '#permit_path?' do
67
67
  expect(Rails).to receive_message_chain(:application, :routes, :recognize_path).with('path') { {a: 1, b: 2, c: 3} }
68
- expect(permission).to receive(:redirect_to?).with(1, 2)
69
- controller.send(:can_redirect_to_path?, 'path')
68
+ expect(permission).to receive(:permit?).with(1, 2)
69
+ controller.send(:permit_path?, 'path')
70
70
  end
71
71
 
72
72
  end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe RouteAuthorizer::PermissionDSL do
4
+
5
+ let(:permission_class) { Class.new }
6
+ let(:permission) { permission_class.new(:admin) }
7
+
8
+ before do
9
+ permission_class.include(RouteAuthorizer::PermissionDSL)
10
+ end
11
+
12
+ it 'includes permission module' do
13
+ expect(permission_class).to include RouteAuthorizer::Permission
14
+ end
15
+
16
+ it '.all_roles' do
17
+ expect(permission_class).to receive(:role).with(:default).and_yield
18
+
19
+ permission_class.send(:all_roles) { :anything }
20
+ end
21
+
22
+ context '.role' do
23
+ it 'with no permission' do
24
+ permission_class.send(:role, :admin) {}
25
+
26
+ expect(permission.send(:admin)).to eq([])
27
+ end
28
+
29
+ it 'with all permission' do
30
+ permission_class.send(:role, :admin) do
31
+ permit_all
32
+ end
33
+
34
+ expect(permission.send(:admin)).to eq([[:all]])
35
+ end
36
+
37
+ it 'with controller permission' do
38
+ permission_class.send(:role, :admin) do
39
+ permit :controller1
40
+ permit :controller2
41
+ end
42
+
43
+ expect(permission.send(:admin)).to eq([
44
+ [:controller1, :all],
45
+ [:controller2, :all],
46
+ ])
47
+ end
48
+
49
+ it 'with controller and action permissions' do
50
+ permission_class.send(:role, :admin) do
51
+ permit :controller1, only: [:action1]
52
+ permit :controller2, only: [:action1, :action2]
53
+ end
54
+
55
+ expect(permission.send(:admin)).to eq([
56
+ [:controller1, :action1],
57
+ [:controller2, :action1],
58
+ [:controller2, :action2],
59
+ ])
60
+ end
61
+ end
62
+
63
+ end
@@ -37,20 +37,20 @@ describe RouteAuthorizer::Permission do
37
37
 
38
38
  it 'permits define permission to all controllers and actions' do
39
39
  allow(permission).to receive(:admin).and_return [[:all]]
40
- expect(permission.redirect_to? :any, :any).to be_truthy
40
+ expect(permission.permit? :any, :any).to be_truthy
41
41
  end
42
42
 
43
43
  it 'permits define permission to a specific controller and all actions' do
44
44
  allow(permission).to receive(:admin).and_return [[:some, :all]]
45
- expect(permission.redirect_to? :some, :any).to be_truthy
46
- expect(permission.redirect_to? :other, :any).to be_falsey
45
+ expect(permission.permit? :some, :any).to be_truthy
46
+ expect(permission.permit? :other, :any).to be_falsey
47
47
  end
48
48
 
49
49
  it 'permits define permission to a specific controller and action' do
50
50
  allow(permission).to receive(:admin).and_return [[:some, :some]]
51
- expect(permission.redirect_to? :some, :some).to be_truthy
52
- expect(permission.redirect_to? :some, :any).to be_falsey
53
- expect(permission.redirect_to? :any, :any).to be_falsey
51
+ expect(permission.permit? :some, :some).to be_truthy
52
+ expect(permission.permit? :some, :any).to be_falsey
53
+ expect(permission.permit? :any, :any).to be_falsey
54
54
  end
55
55
 
56
56
  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.3
4
+ version: 0.0.4
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-11 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -96,9 +96,11 @@ files:
96
96
  - lib/route_authorizer.rb
97
97
  - lib/route_authorizer/authorizer.rb
98
98
  - lib/route_authorizer/permission.rb
99
+ - lib/route_authorizer/permission_dsl.rb
99
100
  - lib/route_authorizer/version.rb
100
101
  - route_authorizer.gemspec
101
102
  - spec/authorizer_spec.rb
103
+ - spec/permission_dsl_spec.rb
102
104
  - spec/permission_spec.rb
103
105
  - spec/spec_helper.rb
104
106
  homepage: https://github.com/FabioMR/route_authorizer
@@ -127,5 +129,6 @@ specification_version: 4
127
129
  summary: Simple routes authorization solution for Rails based on user roles.
128
130
  test_files:
129
131
  - spec/authorizer_spec.rb
132
+ - spec/permission_dsl_spec.rb
130
133
  - spec/permission_spec.rb
131
134
  - spec/spec_helper.rb