can_i 0.1 → 0.1.1
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.
- data/README.md +12 -0
- data/lib/can_i/authorization.rb +11 -2
- data/lib/can_i/authorization_role.rb +17 -6
- data/lib/can_i/version.rb +1 -1
- data/spec/authorization_roles_spec.rb +11 -8
- data/spec/helpers/admin_role.rb +5 -0
- data/spec/helpers/custom_role.rb +5 -0
- metadata +7 -3
data/README.md
CHANGED
@@ -78,3 +78,15 @@ You associate a role with the authorization system when you create your authoriz
|
|
78
78
|
```ruby
|
79
79
|
App.delegate.authorization = CanI::Authorization.new :admin
|
80
80
|
```
|
81
|
+
|
82
|
+
### Can Do Anything
|
83
|
+
For an admin role, you're most likely going to let the user do anything. There's a method for that too. Note that you can't override the all powerful setting in a subclass of the `AdminRole`.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class AdminRole < BaseRole
|
87
|
+
authorization_roles do
|
88
|
+
can_do_anything!
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
data/lib/can_i/authorization.rb
CHANGED
@@ -30,10 +30,11 @@ module CanI
|
|
30
30
|
|
31
31
|
def initialize(role=:authorization)
|
32
32
|
|
33
|
+
@can_do_anything = false
|
33
34
|
klass = CanI.const_defined?("#{role}_role".camelize) ? CanI.const_get("#{role}_role".camelize) : Kernel.const_get("#{role}_role".camelize)
|
34
35
|
|
35
36
|
@auth_role = klass.new
|
36
|
-
|
37
|
+
klass.registration_blocks.each do |more_roles|
|
37
38
|
instance_eval &more_roles
|
38
39
|
end
|
39
40
|
end
|
@@ -43,13 +44,17 @@ module CanI
|
|
43
44
|
end
|
44
45
|
|
45
46
|
def can?(action)
|
46
|
-
approved_actions.include?
|
47
|
+
can_do_anything? || approved_actions.include?(action.to_sym)
|
47
48
|
end
|
48
49
|
|
49
50
|
def cannot(action)
|
50
51
|
approved_actions.delete action.to_sym
|
51
52
|
end
|
52
53
|
|
54
|
+
def can_do_anything!
|
55
|
+
@can_do_anything = true
|
56
|
+
end
|
57
|
+
|
53
58
|
|
54
59
|
private
|
55
60
|
|
@@ -57,5 +62,9 @@ module CanI
|
|
57
62
|
@approved_actions ||= []
|
58
63
|
end
|
59
64
|
|
65
|
+
def can_do_anything?
|
66
|
+
@can_do_anything
|
67
|
+
end
|
68
|
+
|
60
69
|
end
|
61
70
|
end
|
@@ -17,21 +17,32 @@ module CanI
|
|
17
17
|
# @auth.can? :fire_people
|
18
18
|
# => true
|
19
19
|
#
|
20
|
+
# The above admin example would probably be more realistically written as:
|
21
|
+
# class AdminRole < CanI::AuthorizationRole
|
22
|
+
# authorization_roles do
|
23
|
+
# can_do_anything!
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
20
27
|
|
21
28
|
class << self
|
22
29
|
|
30
|
+
# DSL to register a block to add more roles
|
23
31
|
def authorization_roles(&block)
|
24
|
-
|
32
|
+
class_instance_blocks << block
|
25
33
|
end
|
26
34
|
|
35
|
+
# Returns blocks for this subclass & all superclasses merged together
|
27
36
|
def registration_blocks
|
28
|
-
|
29
|
-
end
|
37
|
+
ancestors.select { |klass| klass <= CanI::AuthorizationRole }.map { |klass| klass.class_instance_blocks }.flatten
|
38
|
+
end
|
30
39
|
|
31
|
-
|
40
|
+
# Stores the blocks registered just for this class
|
41
|
+
def class_instance_blocks
|
42
|
+
@class_instance_blocks ||= []
|
43
|
+
end
|
44
|
+
protected :class_instance_blocks
|
32
45
|
|
33
|
-
# use to define additional rules in your subclass
|
34
|
-
authorization_roles do
|
35
46
|
end
|
36
47
|
|
37
48
|
end
|
data/lib/can_i/version.rb
CHANGED
@@ -11,15 +11,7 @@ describe "auth roles" do
|
|
11
11
|
describe "custom auth roles" do
|
12
12
|
|
13
13
|
before do
|
14
|
-
|
15
|
-
class CustomRole < CanI::AuthorizationRole
|
16
|
-
authorization_roles do
|
17
|
-
can :my_role
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
14
|
@auth = CanI::Authorization.new :custom
|
22
|
-
|
23
15
|
end
|
24
16
|
|
25
17
|
it "should respect my custom auth role" do
|
@@ -32,4 +24,15 @@ describe "auth roles" do
|
|
32
24
|
|
33
25
|
end
|
34
26
|
|
27
|
+
describe "can do anything" do
|
28
|
+
before do
|
29
|
+
@auth = CanI::Authorization.new :admin
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should let me do anything I want" do
|
33
|
+
@auth.can?(Time.now.to_i.to_s).should == true
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
35
38
|
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:
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bubble-wrap
|
@@ -48,6 +48,8 @@ files:
|
|
48
48
|
- lib/can_i/version.rb
|
49
49
|
- spec/authorization_roles_spec.rb
|
50
50
|
- spec/helper_method_spec.rb
|
51
|
+
- spec/helpers/admin_role.rb
|
52
|
+
- spec/helpers/custom_role.rb
|
51
53
|
- spec/helpers/helper_methods.rb
|
52
54
|
- spec/main_spec.rb
|
53
55
|
homepage: https://github.com/macfanatic/rubymotion_roles
|
@@ -64,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
66
|
version: '0'
|
65
67
|
segments:
|
66
68
|
- 0
|
67
|
-
hash:
|
69
|
+
hash: -4423656251752888746
|
68
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
71
|
none: false
|
70
72
|
requirements:
|
@@ -81,5 +83,7 @@ summary: Provides an easy way to define roles for performing actions in your Rub
|
|
81
83
|
test_files:
|
82
84
|
- spec/authorization_roles_spec.rb
|
83
85
|
- spec/helper_method_spec.rb
|
86
|
+
- spec/helpers/admin_role.rb
|
87
|
+
- spec/helpers/custom_role.rb
|
84
88
|
- spec/helpers/helper_methods.rb
|
85
89
|
- spec/main_spec.rb
|