can_i 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -0
- data/lib/can_i/authorization.rb +17 -10
- data/lib/can_i/version.rb +1 -1
- data/spec/authorization_roles_spec.rb +27 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -90,3 +90,17 @@ class AdminRole < BaseRole
|
|
90
90
|
end
|
91
91
|
```
|
92
92
|
|
93
|
+
## Changing Roles At Runtime
|
94
|
+
You can change a role at runtime without creating an entirely new `Authorization` object.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
|
98
|
+
# Say we always start out with a "default" role in our app
|
99
|
+
@auth = CanI::Authorization.new :default
|
100
|
+
|
101
|
+
### ...
|
102
|
+
|
103
|
+
# Now we realize we need to use a different role
|
104
|
+
@auth.reset_with_role! :admin
|
105
|
+
|
106
|
+
```
|
data/lib/can_i/authorization.rb
CHANGED
@@ -28,15 +28,8 @@ module CanI
|
|
28
28
|
|
29
29
|
attr_reader :auth_role
|
30
30
|
|
31
|
-
def initialize(role=:authorization)
|
32
|
-
|
33
|
-
@can_do_anything = false
|
34
|
-
klass = CanI.const_defined?("#{role}_role".camelize) ? CanI.const_get("#{role}_role".camelize) : Kernel.const_get("#{role}_role".camelize)
|
35
|
-
|
36
|
-
@auth_role = klass.new
|
37
|
-
klass.registration_blocks.each do |more_roles|
|
38
|
-
instance_eval &more_roles
|
39
|
-
end
|
31
|
+
def initialize(role=:authorization)
|
32
|
+
reset_with_role! role
|
40
33
|
end
|
41
34
|
|
42
35
|
def can(action)
|
@@ -55,8 +48,17 @@ module CanI
|
|
55
48
|
@can_do_anything = true
|
56
49
|
end
|
57
50
|
|
51
|
+
def reset_with_role!(role)
|
52
|
+
@can_do_anything = false
|
53
|
+
@approved_actions = []
|
58
54
|
|
59
|
-
|
55
|
+
klass = role_for_symbol(role)
|
56
|
+
@auth_role = klass.new
|
57
|
+
klass.registration_blocks.each { |b| instance_eval &b }
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
protected
|
60
62
|
|
61
63
|
def approved_actions
|
62
64
|
@approved_actions ||= []
|
@@ -66,5 +68,10 @@ module CanI
|
|
66
68
|
@can_do_anything
|
67
69
|
end
|
68
70
|
|
71
|
+
def role_for_symbol(sym)
|
72
|
+
role = "#{sym}_role".camelize
|
73
|
+
CanI.const_defined?(role) ? CanI.const_get(role) : Kernel.const_get(role)
|
74
|
+
end
|
75
|
+
|
69
76
|
end
|
70
77
|
end
|
data/lib/can_i/version.rb
CHANGED
@@ -35,4 +35,31 @@ describe "auth roles" do
|
|
35
35
|
|
36
36
|
end
|
37
37
|
|
38
|
+
describe "resetting a role" do
|
39
|
+
|
40
|
+
before do
|
41
|
+
@auth = CanI::Authorization.new :admin
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should be an admin role" do
|
45
|
+
@auth.auth_role.should.be.instance_of AdminRole
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should let me reset back to the default role" do
|
49
|
+
@auth.reset_with_role! :authorization
|
50
|
+
@auth.auth_role.should.be.instance_of CanI::AuthorizationRole
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not have any allowed actions" do
|
54
|
+
@auth.reset_with_role! :authorization
|
55
|
+
@auth.send(:approved_actions).should == []
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return false for #can_do_anything?" do
|
59
|
+
@auth.reset_with_role! :authorization
|
60
|
+
@auth.send(:can_do_anything?).should == false
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
38
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_i
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -66,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
66
|
version: '0'
|
67
67
|
segments:
|
68
68
|
- 0
|
69
|
-
hash: -
|
69
|
+
hash: -2984349318728766085
|
70
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
71
|
none: false
|
72
72
|
requirements:
|