stasi 0.1.0 → 0.1.2
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/Gemfile.lock +1 -1
- data/README.md +11 -4
- data/Rakefile +2 -0
- data/lib/stasi/authorization/status.rb +3 -2
- data/test/status_test.rb +33 -1
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Stasi
|
1
|
+
# Stasi [](http://travis-ci.org/VonD/stasi)
|
2
2
|
|
3
3
|
A small authorization library inspired by CanCan
|
4
4
|
|
@@ -30,7 +30,7 @@ Robotnik::Authorization::Law.define do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
status :admin do
|
33
|
-
can :edit, Post, if: Proc.new{ |post| post.editable }
|
33
|
+
can :edit, Post, if: Proc.new{ |post| post.editable? }
|
34
34
|
can :destroy, Post
|
35
35
|
end
|
36
36
|
|
@@ -49,7 +49,14 @@ The `can` method takes two arguments : an action name as a symbol, and a resourc
|
|
49
49
|
* a class, eg. `Post`
|
50
50
|
* a symbol, eg. `:commentable`. The authorization will be applied if `@post.commentable` returns `true`. This method can take one argument, in which case, the user object will be passed to it.
|
51
51
|
|
52
|
-
|
52
|
+
When checking permission, you can pass an `:as` option to provide the right resource. As the gem in ORM agnostic, this can be useful when checking on a collection :
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# if you defined the ability as : can :read, Post
|
56
|
+
current_user.can? :read, Post.published, as: Post
|
57
|
+
```
|
58
|
+
|
59
|
+
Optionnally, the `can` method can take a hash with conditions (hash keys can be `if` and `unless`, values can be Proc, or a symbol on which will be called `to_proc`. The resource tested will be yielded).
|
53
60
|
Finally, the `can` method can take a block, in which case the `can?` method will return the return value of the block. This is useful when defining abilities on collections :
|
54
61
|
|
55
62
|
```ruby
|
@@ -62,7 +69,7 @@ The `cannot` method takes only two arguments : the action name, and the resource
|
|
62
69
|
|
63
70
|
## Milestones
|
64
71
|
|
72
|
+
* reload config in dev mode in rails
|
65
73
|
* yield user to blocks and procs in defining abilities
|
66
|
-
* pass symbol or proc to `:if` and `:unless` conditions
|
67
74
|
* alias actions :manage, :all, :read => [:index, :show], :create => [:new, :create], …
|
68
75
|
* load specific permissions from db
|
data/Rakefile
CHANGED
@@ -26,8 +26,8 @@ module Robotnik
|
|
26
26
|
action_condition.call(resource)
|
27
27
|
else
|
28
28
|
deliberation = true
|
29
|
-
deliberation = deliberation && action_condition[:if].call(resource) if action_condition.has_key?(:if)
|
30
|
-
deliberation = deliberation && (! action_condition[:unless].call(resource)) if deliberation && action_condition.has_key?(:unless)
|
29
|
+
deliberation = deliberation && action_condition[:if].to_proc.call(resource) if action_condition.has_key?(:if)
|
30
|
+
deliberation = deliberation && (! action_condition[:unless].to_proc.call(resource)) if deliberation && action_condition.has_key?(:unless)
|
31
31
|
deliberation
|
32
32
|
end
|
33
33
|
end
|
@@ -37,6 +37,7 @@ module Robotnik
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.matches? rule_condition, resource, options
|
40
|
+
return true if options.has_key?(:as) && options[:as] == rule_condition
|
40
41
|
rule_condition = rule_condition.to_proc if rule_condition.respond_to?(:to_proc)
|
41
42
|
begin
|
42
43
|
rule_condition === resource
|
data/test/status_test.rb
CHANGED
@@ -3,6 +3,14 @@ require 'test_helper'
|
|
3
3
|
class StatusTest < ActiveSupport::TestCase
|
4
4
|
|
5
5
|
Book = Class.new
|
6
|
+
Book.class_eval do
|
7
|
+
def method_returning_true
|
8
|
+
true
|
9
|
+
end
|
10
|
+
def method_returning_false
|
11
|
+
false
|
12
|
+
end
|
13
|
+
end
|
6
14
|
|
7
15
|
def setup
|
8
16
|
@status = Robotnik::Authorization::Status.new
|
@@ -32,7 +40,7 @@ class StatusTest < ActiveSupport::TestCase
|
|
32
40
|
assert_equal true, @status.instance_variable_get('@rules')[Book][:read]
|
33
41
|
end
|
34
42
|
|
35
|
-
test "it defines authorization with if and unless options" do
|
43
|
+
test "it defines authorization with if and unless options and a Proc" do
|
36
44
|
Post = Struct.new :name
|
37
45
|
assertions = [true, false, false, true, false, true, false, false]
|
38
46
|
[[true, nil], [nil, true], [false, nil], [nil, false], [true, true], [true, false], [false, true], [false, false]].each_with_index do |conditions, i|
|
@@ -51,6 +59,24 @@ class StatusTest < ActiveSupport::TestCase
|
|
51
59
|
end
|
52
60
|
end
|
53
61
|
|
62
|
+
test "it defines authorization with if and unless options and a symbol" do
|
63
|
+
assertions = [true, false, false, true, false, true, false, false]
|
64
|
+
[[true, nil], [nil, true], [false, nil], [nil, false], [true, true], [true, false], [false, true], [false, false]].each_with_index do |conditions, i|
|
65
|
+
conditions_hash = {}
|
66
|
+
[:if, :unless].each_with_index do |operator, j|
|
67
|
+
unless conditions[j].nil?
|
68
|
+
if conditions[j]
|
69
|
+
conditions_hash[operator] = :method_returning_true
|
70
|
+
else
|
71
|
+
conditions_hash[operator] = :method_returning_false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
@status.can :read, Book, conditions_hash
|
76
|
+
assert_equal assertions[i], @status.can?(:read, Book.new)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
54
80
|
test "it defines authorizations with a block" do
|
55
81
|
Book.class_eval do
|
56
82
|
attr_accessor :collection
|
@@ -110,5 +136,11 @@ class StatusTest < ActiveSupport::TestCase
|
|
110
136
|
@status.can :read, :taggable
|
111
137
|
assert @status.can? :read, o
|
112
138
|
end
|
139
|
+
|
140
|
+
test "it overrides the matching condition when :as option is present" do
|
141
|
+
@status.can :read, Fixnum
|
142
|
+
refute @status.can? :read, Object.new
|
143
|
+
assert @status.can? :read, Object.new, as: Fixnum
|
144
|
+
end
|
113
145
|
|
114
146
|
end
|