cannie 0.0.4 → 0.0.5
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 +4 -4
- data/lib/cannie/controller_extensions.rb +2 -2
- data/lib/cannie/version.rb +1 -1
- data/spec/cannie/controller_extensions_spec.rb +43 -43
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb9f9ff798ce6036fb378e643d66ab7a6cc678e8
|
4
|
+
data.tar.gz: 4ae746f74792940b996abfd21842078b8fe794a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7ce6ab4dfdfecca9c648bba8b28d19b8438cc15dc956098567853e54bf9d4cf4a8efece057537f6e30838a2db73d4014e2621fafaf55aa92ee12f975fc68a50
|
7
|
+
data.tar.gz: 65bd5926da7baf5da5f5329ce4a2c466dbf38236d55bfc0b64ba351abd37971604f3603d21a935e2bc3e93a5cbcc7d72e90230087d62bd70c179c4d3d1a8f335
|
@@ -19,8 +19,8 @@ module Cannie
|
|
19
19
|
def check_permissions(options={})
|
20
20
|
after_action(options.slice(:only, :except)) do |controller|
|
21
21
|
next if controller.permitted?
|
22
|
-
next if options[:if] && !controller.instance_eval(options[:if])
|
23
|
-
next if options[:unless] && controller.instance_eval(options[:unless])
|
22
|
+
next if options[:if] && !controller.instance_eval(&options[:if])
|
23
|
+
next if options[:unless] && controller.instance_eval(&options[:unless])
|
24
24
|
raise CheckPermissionsNotPerformed, 'Action failed the check_permissions because it does not calls permit! method. Add skip_check_permissions to bypass this check.'
|
25
25
|
end
|
26
26
|
end
|
data/lib/cannie/version.rb
CHANGED
@@ -1,20 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class TestController < ActionController::Base
|
4
|
-
def action
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
3
|
describe Cannie::ControllerExtensions do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
4
|
+
let(:klass) {
|
5
|
+
Class.new(ActionController::Base) do
|
6
|
+
def action; end
|
7
|
+
end
|
8
|
+
}
|
14
9
|
|
15
|
-
|
16
|
-
subject.class._process_action_callbacks.select{|f| f.kind == :after}.map(&:raw_filter)
|
17
|
-
end
|
10
|
+
subject { klass.new }
|
18
11
|
|
19
12
|
let(:permissions) do
|
20
13
|
Class.new do
|
@@ -30,66 +23,51 @@ describe Cannie::ControllerExtensions do
|
|
30
23
|
|
31
24
|
describe '.check_permissions' do
|
32
25
|
describe 'without conditions' do
|
33
|
-
before
|
34
|
-
TestController.class_eval do
|
35
|
-
check_permissions
|
36
|
-
end
|
37
|
-
end
|
26
|
+
before { klass.check_permissions }
|
38
27
|
|
39
28
|
it 'raises exception if controller.permitted? evaluates to false' do
|
40
|
-
expect {
|
29
|
+
expect { subject.run_callbacks(:process_action) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
|
41
30
|
end
|
42
31
|
|
43
32
|
it 'does not raise exception if controller.permitted? evaluates to true' do
|
44
33
|
subject.stub(:permitted?).and_return(true)
|
45
|
-
expect {
|
34
|
+
expect { subject.run_callbacks(:process_action) }.not_to raise_error
|
46
35
|
end
|
47
36
|
end
|
48
37
|
|
49
38
|
describe 'with if condition' do
|
50
|
-
before
|
51
|
-
TestController.class_eval do
|
52
|
-
check_permissions if: ->{ self.var == true }
|
53
|
-
end
|
54
|
-
end
|
39
|
+
before { klass.check_permissions if: :condition? }
|
55
40
|
|
56
41
|
it 'raises exception if :if block executed in controller scope returns true' do
|
57
|
-
|
58
|
-
expect {
|
42
|
+
subject.stub(:condition?).and_return(true)
|
43
|
+
expect { subject.run_callbacks(:process_action) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
|
59
44
|
end
|
60
45
|
|
61
46
|
it 'does not raise exception if :if block executed in controller scope returns false' do
|
62
|
-
|
63
|
-
expect {
|
47
|
+
subject.stub(:condition?).and_return(false)
|
48
|
+
expect { subject.run_callbacks(:process_action) }.not_to raise_error
|
64
49
|
end
|
65
50
|
end
|
66
51
|
|
67
52
|
describe 'with unless condition' do
|
68
|
-
before
|
69
|
-
TestController.class_eval do
|
70
|
-
check_permissions unless: ->{ self.var == true }
|
71
|
-
end
|
72
|
-
end
|
53
|
+
before { klass.check_permissions unless: :condition? }
|
73
54
|
|
74
55
|
it 'raises exception if :unless block executed in controller scope returns false' do
|
75
|
-
|
76
|
-
expect {
|
56
|
+
subject.stub(:condition?).and_return(false)
|
57
|
+
expect { subject.run_callbacks(:process_action) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
|
77
58
|
end
|
78
59
|
|
79
60
|
it 'does not raise exception if :unless block executed in controller scope returns false' do
|
80
|
-
|
81
|
-
expect {
|
61
|
+
subject.stub(:condition?).and_return(true)
|
62
|
+
expect { subject.run_callbacks(:process_action) }.not_to raise_error
|
82
63
|
end
|
83
64
|
end
|
84
65
|
end
|
85
66
|
|
86
67
|
describe '.skip_check_permissions' do
|
87
68
|
it 'sets @_permitted to true to bypass permissions checking' do
|
88
|
-
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
|
-
before_filters.first.call(subject)
|
69
|
+
klass.skip_check_permissions
|
70
|
+
subject.run_callbacks(:process_action)
|
93
71
|
expect(subject.permitted?).to be_true
|
94
72
|
end
|
95
73
|
end
|
@@ -126,4 +104,26 @@ describe Cannie::ControllerExtensions do
|
|
126
104
|
expect { subject.permit! :update, on: [3,6,11] }.to raise_error(Cannie::ActionForbidden)
|
127
105
|
end
|
128
106
|
end
|
107
|
+
|
108
|
+
describe '#current_permissions' do
|
109
|
+
before(:all) do
|
110
|
+
Permissions = Class.new do
|
111
|
+
attr_reader :user
|
112
|
+
def initialize(user)
|
113
|
+
@user = user
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
before { subject.stub(:current_user).and_return 'User' }
|
119
|
+
|
120
|
+
it 'creates new Permissions object' do
|
121
|
+
expect(subject.current_permissions).to be_instance_of(Permissions)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'passes current_user to Permissions::new' do
|
125
|
+
subject.stub(:current_user).and_return 'User'
|
126
|
+
expect(subject.current_permissions.user).to eq('User')
|
127
|
+
end
|
128
|
+
end
|
129
129
|
end
|