sevn 0.0.2 → 0.0.3
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/sevn/ability.rb +30 -0
- data/lib/sevn/errors.rb +12 -0
- data/lib/sevn/version.rb +1 -1
- data/spec/support/valid_abilities_example.rb +61 -2
- 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: 01bac45609156ae06537b04f1214238b248d0e53
|
4
|
+
data.tar.gz: d0caa0640db4d178e40e11efea16c995874656e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 734c28abc2a8c661be4506c41be5f29ea1732ebab76b7af7f95df065c5f501702465e6dd52a1279fa9142697395e1b9b4e88fc13b3038b9c1a0ec6752a9b9d69
|
7
|
+
data.tar.gz: 85e2311e7f2c8603828e02fe92f7b7926c07f11f37ca63d699b8e752105836e50879caed2ebc1a0d22a4d1f84d2862d97322699af530a0142a219dc232cc2361
|
data/lib/sevn/ability.rb
CHANGED
@@ -57,6 +57,36 @@ module Sevn
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
+
# Check if +object+ is authorized to do +actions+ in +subject+
|
61
|
+
# if action is not allowed it will raise an Unauthorized error
|
62
|
+
#
|
63
|
+
# == Parameters:
|
64
|
+
# actions::
|
65
|
+
# Symbol or Array of Symbols of the actions to check
|
66
|
+
# object::
|
67
|
+
# object trying to access resource
|
68
|
+
# subject::
|
69
|
+
# resource to be accessed
|
70
|
+
# options::
|
71
|
+
# a list of options to consider when checking.
|
72
|
+
#
|
73
|
+
# == Options:
|
74
|
+
# use_pack::
|
75
|
+
# check for actions in the specified pack instead of auto-determining the pack.
|
76
|
+
#
|
77
|
+
# == Returns:
|
78
|
+
# +subject+
|
79
|
+
#
|
80
|
+
# == Exceptions:
|
81
|
+
# if object is not allowed to do action on subject, it will raise an UnauthorizedError
|
82
|
+
#
|
83
|
+
def authorize!(object, actions, subject, options = {})
|
84
|
+
if !allowed?(object, actions, subject, options)
|
85
|
+
raise Sevn::Errors::UnauthorizedError.new(object, actions, subject)
|
86
|
+
end
|
87
|
+
subject
|
88
|
+
end
|
89
|
+
|
60
90
|
private
|
61
91
|
def add_pack(name, pack)
|
62
92
|
if valid_rules_pack?(pack)
|
data/lib/sevn/errors.rb
CHANGED
@@ -32,5 +32,17 @@ module Sevn
|
|
32
32
|
'RulesPack abilities must be an "Array"'
|
33
33
|
end
|
34
34
|
end
|
35
|
+
|
36
|
+
class UnauthorizedError < StandardError
|
37
|
+
def initialize(object, action, subject)
|
38
|
+
@object = object
|
39
|
+
@action = action
|
40
|
+
@subject = subject
|
41
|
+
end
|
42
|
+
|
43
|
+
def message
|
44
|
+
"#{@object.inspect} is not authorized to do '#{@action}' to #{@subject.inspect}"
|
45
|
+
end
|
46
|
+
end
|
35
47
|
end
|
36
48
|
end
|
data/lib/sevn/version.rb
CHANGED
@@ -8,8 +8,8 @@ shared_examples :valid_abilities do
|
|
8
8
|
@mikes_book = Book.new("Life", @mike)
|
9
9
|
end
|
10
10
|
|
11
|
-
def allowed?(
|
12
|
-
abilities.allowed?(
|
11
|
+
def allowed?(object, action, subject)
|
12
|
+
abilities.allowed?(object, action, subject)
|
13
13
|
end
|
14
14
|
|
15
15
|
describe "should return true or false depend on access" do
|
@@ -49,4 +49,63 @@ shared_examples :valid_abilities do
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
|
+
|
53
|
+
describe :authorize! do
|
54
|
+
before do
|
55
|
+
@jim = Author.new("Jim")
|
56
|
+
@mike = Author.new("Mike")
|
57
|
+
|
58
|
+
@jims_book = Book.new("The Game", @jim)
|
59
|
+
@mikes_book = Book.new("Life", @mike)
|
60
|
+
end
|
61
|
+
|
62
|
+
def authorize!(object, action, subject)
|
63
|
+
abilities.authorize!(object, action, subject)
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "should return the subject or raise UnauthorizedError" do
|
67
|
+
context :read_book do
|
68
|
+
it { expect(authorize!(@jim, :read_book, @jims_book)).to be(@jims_book) }
|
69
|
+
it { expect(authorize!(@mike, :read_book, @mikes_book)).to be(@mikes_book) }
|
70
|
+
it { expect(authorize!(@jim, :read_book, @mikes_book)).to be(@mikes_book) }
|
71
|
+
it { expect(authorize!(@mike, :read_book, @jims_book)).to be(@jims_book) }
|
72
|
+
end
|
73
|
+
|
74
|
+
context :rate_book do
|
75
|
+
it { expect{authorize!(@jim, :rate_book, @jims_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
76
|
+
it { expect{authorize!(@mike, :rate_book, @mikes_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
77
|
+
it { expect(authorize!(@jim, :rate_book, @mikes_book)).to be(@mikes_book) }
|
78
|
+
it { expect(authorize!(@mike, :rate_book, @jims_book)).to be(@jims_book) }
|
79
|
+
end
|
80
|
+
|
81
|
+
context :edit_book do
|
82
|
+
it { expect(authorize!(@jim, :edit_book, @jims_book)).to be(@jims_book) }
|
83
|
+
it { expect(authorize!(@mike,:edit_book, @mikes_book)).to be(@mikes_book) }
|
84
|
+
it { expect{authorize!(@jim, :edit_book, @mikes_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
85
|
+
it { expect{authorize!(@mike,:edit_book, @jims_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
86
|
+
end
|
87
|
+
|
88
|
+
context :publish_book do
|
89
|
+
it { expect{authorize!(@jim, :publish_book, @jims_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
90
|
+
it { expect{authorize!(@mike,:publish_book, @mikes_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
91
|
+
it { expect{authorize!(@jim, :publish_book, @mikes_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
92
|
+
it { expect{authorize!(@mike,:publish_book, @jims_book)}.to raise_error(Sevn::Errors::UnauthorizedError) }
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'passing multiple actions' do
|
96
|
+
it { expect(authorize!(@jim, [:read_book, :edit_book], @jims_book)).to be(@jims_book) }
|
97
|
+
it {
|
98
|
+
expect {
|
99
|
+
authorize!(@jim, [:ead_book, :publish_book, :edit_book], @jims_book)
|
100
|
+
}.to raise_error(Sevn::Errors::UnauthorizedError)
|
101
|
+
}
|
102
|
+
it { expect(authorize!(@mike, [:read_book, :edit_book], @mikes_book)).to be(@mikes_book) }
|
103
|
+
it {
|
104
|
+
expect {
|
105
|
+
authorize!(@mike, [:rate_book, :publish_book, :edit_book], @mikes_book)
|
106
|
+
}.to raise_error(Sevn::Errors::UnauthorizedError)
|
107
|
+
}
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
52
111
|
end
|