From a8be9b2e7265bd780d95d2ee37dc77bb19670655 Mon Sep 17 00:00:00 2001 From: Andrew Aladjev Date: Mon, 20 Nov 2017 19:44:05 +0300 Subject: [PATCH 1/4] subject is required when action is provided --- lib/cancan/rule.rb | 1 + spec/cancan/ability_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index 2578d3ba..cd4f8703 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -17,6 +17,7 @@ def initialize(base_behavior, action, subject, conditions, block) "#{action} #{subject} ability. Use either one." raise Error, both_block_and_hash_error if conditions.is_a?(Hash) && block @match_all = action.nil? && subject.nil? + raise Error, 'Subject is required for action' if !action.nil? && subject.nil? @base_behavior = base_behavior @actions = Array(action) @subjects = Array(subject) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 236cc58b..e0098707 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -488,6 +488,12 @@ class Container < Hash 'Use either one.') end + it 'raises an error when attempting to use action without subject' do + expect do + @ability.can :dashboard + end.to raise_error(CanCan::Error, 'Subject is required for action') + end + describe 'unauthorized message' do after(:each) do I18n.backend = nil From 7e2e2c6ab224eec5c13b44ba5172dabe11e7bb89 Mon Sep 17 00:00:00 2001 From: andrew-aladev Date: Tue, 21 Nov 2017 22:39:18 +0300 Subject: [PATCH 2/4] printed action if subject is nil --- lib/cancan/rule.rb | 2 +- spec/cancan/ability_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index cd4f8703..a24dbc8b 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -17,7 +17,7 @@ def initialize(base_behavior, action, subject, conditions, block) "#{action} #{subject} ability. Use either one." raise Error, both_block_and_hash_error if conditions.is_a?(Hash) && block @match_all = action.nil? && subject.nil? - raise Error, 'Subject is required for action' if !action.nil? && subject.nil? + raise Error, "Subject is required for #{action}" if !action.nil? && subject.nil? @base_behavior = base_behavior @actions = Array(action) @subjects = Array(subject) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index e0098707..a0d8da24 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -491,7 +491,7 @@ class Container < Hash it 'raises an error when attempting to use action without subject' do expect do @ability.can :dashboard - end.to raise_error(CanCan::Error, 'Subject is required for action') + end.to raise_error(CanCan::Error, 'Subject is required for dashboard') end describe 'unauthorized message' do From 7ac9d4dbaa4bdeaf9134bb01af5e730f0ba33678 Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Tue, 13 Mar 2018 09:06:41 +0100 Subject: [PATCH 3/4] fix rubocop issues --- CHANGELOG.rdoc | 2 ++ lib/cancan/exceptions.rb | 3 +++ lib/cancan/rule.rb | 12 ++++++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index e8155dc8..7dfca8be 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,5 +1,7 @@ Unreleased +* Drop support for actions without a subject (andrew-aladev) + * Removed support for dynamic finders (coorasse) 2.1.3 (Jan 16th, 2018) diff --git a/lib/cancan/exceptions.rb b/lib/cancan/exceptions.rb index ef30a537..4b99f317 100644 --- a/lib/cancan/exceptions.rb +++ b/lib/cancan/exceptions.rb @@ -11,6 +11,9 @@ class ImplementationRemoved < Error; end # Raised when using check_authorization without calling authorized! class AuthorizationNotPerformed < Error; end + # Raised when a rule is created with both a block and a hash of conditions + class BlockAndConditionsError < Error; end + # This error is raised when a user isn't allowed to access a given controller action. # This usually happens within a call to ControllerAdditions#authorize! but can be # raised manually. diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index a24dbc8b..4873242f 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -13,11 +13,9 @@ class Rule # :nodoc: # and subject respectively (such as :read, @project). The third argument is a hash # of conditions and the last one is the block passed to the "can" call. def initialize(base_behavior, action, subject, conditions, block) - both_block_and_hash_error = 'You are not able to supply a block with a hash of conditions in '\ - "#{action} #{subject} ability. Use either one." - raise Error, both_block_and_hash_error if conditions.is_a?(Hash) && block + condition_and_block_check(conditions, block, action, subject) @match_all = action.nil? && subject.nil? - raise Error, "Subject is required for #{action}" if !action.nil? && subject.nil? + raise Error, "Subject is required for #{action}" if action && subject.nil? @base_behavior = base_behavior @actions = Array(action) @subjects = Array(subject) @@ -81,5 +79,11 @@ def matches_subject_class?(subject) (subject.is_a?(Module) && subject.ancestors.include?(sub))) end end + + def condition_and_block_check(conditions, block, action, subject) + return unless conditions.is_a?(Hash) && block + raise BlockAndConditionsError, 'A hash of conditions is mutually exclusive with a block.'\ + "Check #{action} #{subject} ability." + end end end From aa6dc7561c5468e8c37ed955710db1ec31ec9c4c Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Tue, 13 Mar 2018 09:46:54 +0100 Subject: [PATCH 4/4] fix tests --- lib/cancan/rule.rb | 4 ++-- spec/cancan/ability_spec.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index 4873242f..9b3ccf79 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -82,8 +82,8 @@ def matches_subject_class?(subject) def condition_and_block_check(conditions, block, action, subject) return unless conditions.is_a?(Hash) && block - raise BlockAndConditionsError, 'A hash of conditions is mutually exclusive with a block.'\ - "Check #{action} #{subject} ability." + raise BlockAndConditionsError, 'A hash of conditions is mutually exclusive with a block. '\ + "Check \":#{action} #{subject}\" ability." end end end diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index a0d8da24..71e3cf70 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -483,9 +483,9 @@ class Container < Hash @ability.can :read, Array, published: true do false end - end.to raise_error(CanCan::Error, - 'You are not able to supply a block with a hash of conditions in read Array ability. '\ - 'Use either one.') + end.to raise_error(CanCan::BlockAndConditionsError, + 'A hash of conditions is mutually exclusive with a block. '\ + 'Check ":read Array" ability.') end it 'raises an error when attempting to use action without subject' do