allowance 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/allowance/permissions.rb +15 -6
- data/lib/allowance/version.rb +1 -1
- data/spec/permissions_spec.rb +23 -37
- metadata +7 -7
@@ -1,17 +1,16 @@
|
|
1
1
|
module Allowance
|
2
2
|
class Permissions
|
3
|
-
def initialize
|
3
|
+
def initialize
|
4
4
|
@permissions = {}
|
5
|
-
|
6
|
-
instance_exec(context, &blk)
|
5
|
+
yield(self) if block_given?
|
7
6
|
end
|
8
7
|
|
9
|
-
def
|
8
|
+
def allowed?(verb, object = nil)
|
10
9
|
return true if @permissions[[verb, object]]
|
11
10
|
|
12
11
|
# If object is a resource instance, try its class
|
13
12
|
if object.class.respond_to?(:find)
|
14
|
-
if
|
13
|
+
if allowed?(verb, object.class)
|
15
14
|
# See if the object is part of the defined scope
|
16
15
|
return !scoped_model(verb, object.class).
|
17
16
|
find(:first, :conditions => { :id => object.id }).nil?
|
@@ -21,7 +20,7 @@ module Allowance
|
|
21
20
|
false
|
22
21
|
end
|
23
22
|
|
24
|
-
def
|
23
|
+
def allow!(verbs, objects = nil, scope = true, &blk)
|
25
24
|
expand_permissions(verbs).each do |verb|
|
26
25
|
[objects].flatten.each do |object|
|
27
26
|
@permissions[[verb, object]] = scope # TODO: add blk, too
|
@@ -29,6 +28,16 @@ module Allowance
|
|
29
28
|
end
|
30
29
|
end
|
31
30
|
|
31
|
+
def method_missing(name, *args, &blk)
|
32
|
+
if name.to_s =~ /(.+)!$/
|
33
|
+
allow!($1.to_sym, *args, &blk)
|
34
|
+
elsif name.to_s =~ /(.+)\?$/
|
35
|
+
allowed?($1.to_sym, *args, &blk)
|
36
|
+
else
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
32
41
|
def scoped_model(verb, model)
|
33
42
|
if p = @permissions[[verb, model]]
|
34
43
|
if p.is_a?(Hash)
|
data/lib/allowance/version.rb
CHANGED
data/spec/permissions_spec.rb
CHANGED
@@ -6,42 +6,35 @@ module Allowance
|
|
6
6
|
SomeOtherClass = Class.new
|
7
7
|
|
8
8
|
it "should allow simple permissions to be specified" do
|
9
|
-
|
10
|
-
can :moo
|
11
|
-
end
|
9
|
+
subject.moo!
|
12
10
|
|
13
|
-
insist
|
14
|
-
refuse
|
11
|
+
insist subject.moo?
|
12
|
+
refuse subject.quack?
|
15
13
|
end
|
16
14
|
|
17
|
-
it "should
|
18
|
-
p = Permissions.new do
|
19
|
-
can
|
15
|
+
it "should support a block-style initialization" do
|
16
|
+
p = Permissions.new do |can|
|
17
|
+
can.sing!
|
20
18
|
end
|
21
19
|
|
22
|
-
insist p.
|
23
|
-
|
24
|
-
refuse p.can?(:update, SomeOtherClass)
|
20
|
+
insist p.allowed? :sing
|
21
|
+
insist p.sing?
|
25
22
|
end
|
26
23
|
|
27
|
-
it "should
|
28
|
-
|
29
|
-
|
30
|
-
p = Permissions.new(skills) do |skills|
|
31
|
-
can :sing if skills.singing == :good
|
32
|
-
end
|
24
|
+
it "should allow verbs and objects" do
|
25
|
+
subject.update! SomeClass
|
33
26
|
|
34
|
-
insist
|
27
|
+
insist subject.update? SomeClass
|
28
|
+
refuse subject.destroy? SomeClass
|
29
|
+
refuse subject.update? SomeOtherClass
|
35
30
|
end
|
36
31
|
|
37
32
|
it "should expand :view to include :index and :show" do
|
38
|
-
|
39
|
-
can :view, SomeClass
|
40
|
-
end
|
33
|
+
subject.view! SomeClass
|
41
34
|
|
42
|
-
insist
|
43
|
-
insist
|
44
|
-
insist
|
35
|
+
insist subject.view? SomeClass
|
36
|
+
insist subject.index? SomeClass
|
37
|
+
insist subject.show? SomeClass
|
45
38
|
end
|
46
39
|
|
47
40
|
it "should verify permissions against model instances" do
|
@@ -53,11 +46,9 @@ module Allowance
|
|
53
46
|
|
54
47
|
model_class.should_receive(:find).and_return(model_instance)
|
55
48
|
|
56
|
-
|
57
|
-
can :view, model_class, lambda { some_scope }
|
58
|
-
end
|
49
|
+
subject.view! model_class, lambda { some_scope }
|
59
50
|
|
60
|
-
insist
|
51
|
+
insist subject.view? model_instance
|
61
52
|
end
|
62
53
|
|
63
54
|
describe "#scoped_model" do
|
@@ -65,22 +56,17 @@ module Allowance
|
|
65
56
|
model = mock
|
66
57
|
model.should_receive(:some_scope).and_return(scoped_model = mock)
|
67
58
|
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
p.scoped_model(:view, model).should == scoped_model
|
59
|
+
subject.view! model, lambda { some_scope }
|
60
|
+
subject.scoped_model(:view, model).should == scoped_model
|
73
61
|
end
|
74
62
|
|
75
63
|
it "should allow scopes to be defined through where conditions" do
|
76
64
|
model = mock
|
77
65
|
model.should_receive(:where).with(:awesome => true).and_return(scoped_model = mock)
|
78
66
|
|
79
|
-
|
80
|
-
can :view, model, :awesome => true
|
81
|
-
end
|
67
|
+
subject.view! model, :awesome => true
|
82
68
|
|
83
|
-
|
69
|
+
subject.scoped_model(:view, model).should == scoped_model
|
84
70
|
end
|
85
71
|
end
|
86
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: allowance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-05-27 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70251993567840 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70251993567840
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70251993566520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70251993566520
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: watchr
|
38
|
-
requirement: &
|
38
|
+
requirement: &70251993563400 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70251993563400
|
47
47
|
description: A generic, but decidedly awesome authorization control layer.
|
48
48
|
email:
|
49
49
|
- hendrik@mans.de
|