Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'features/able_to' into wip

  • Loading branch information...
commit 75a313c1f591c26452e3db0fe25493ff8ef52293 2 parents d0e9a69 + 28dccce
@denyago authored
View
10 lib/heimdallr/evaluator.rb
@@ -139,12 +139,20 @@ def can?(action)
# Return a Hash to be mixed in in +reflect_on_security+ methods of {Proxy::Collection}
# and {Proxy::Record}.
+ #
+ # @deprecated use +#actions+ instead
def reflection
{
- operations: [ :view, :create, :update ].select { |op| can? op }
+ operations: actions
}
end
+ # Returns Array to be used in +reflect_on_security+ methods of {Proxy::Collection}
+ # and {Proxy::Record}.
+ def actions
+ ([ :view, :create, :update ] | @allowed_fields.keys ).select { |op| can? op }
+ end
+
# Compute the restrictions for a given +context+ and possibly a specific +record+.
# Switches model class to the +klass+ if given.
# Invokes a +block+ passed to the +initialize+ once.
View
3  lib/heimdallr/proxy/collection.rb
@@ -289,7 +289,8 @@ def reflect_on_security
scope: @scope,
options: @options,
restrictions: @restrictions,
- }.merge(@restrictions.reflection)
+ operations: @restrictions.actions
+ }
end
def creatable?
View
4 lib/heimdallr/proxy/record.rb
@@ -289,13 +289,15 @@ def inspect
#
# @return [Hash]
def reflect_on_security
+ operations = @restrictions.actions | ( destroyable? ? [:delete] : [] )
{
model: @record.class,
context: @context,
record: @record,
options: @options,
restrictions: @restrictions,
- }.merge(@restrictions.reflection)
+ operations: operations
+ }
end
def visible?
View
13 spec/proxy_examples.rb
@@ -111,6 +111,19 @@ def run_specs(user_model, article_model, dont_save_model, comments_model)
article.restrict(@looser).able_to?(:foo).should_not be_true
end
+ describe "contains all available actions in reflect_on_security[:operations]" do
+ it "of Proxy::Record" do
+ article = article_model.create! :owner_id => @john.id, :content => 'test', :secrecy_level => 4
+ article.restrict(@john).reflect_on_security[:operations].should =~ [:create, :view, :update, :delete, :foo]
+ article.restrict(@looser).reflect_on_security[:operations].should =~ [:create, :view]
+ end
+
+ it "of Proxy::Collection" do
+ article_model.restrict(@john).reflect_on_security[:operations].should =~ [:create, :view]
+ article_model.restrict(@looser).reflect_on_security[:operations].should =~ [:create, :view]
+ end
+ end
+
it "should not create anything else if it did not saved" do
expect {
article_model.restrict(@looser).create! :content => 'test', :secrecy_level => 10, :dont_save => 'ok' rescue nil
Please sign in to comment.
Something went wrong with that request. Please try again.