Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop support for actions without a subject #489

Merged
merged 8 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lib/cancan/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions lib/cancan/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +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 && subject.nil?
@base_behavior = base_behavior
@actions = Array(action)
@subjects = Array(subject)
Expand Down Expand Up @@ -80,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
12 changes: 9 additions & 3 deletions spec/cancan/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,15 @@ 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
expect do
@ability.can :dashboard
end.to raise_error(CanCan::Error, 'Subject is required for dashboard')
end

describe 'unauthorized message' do
Expand Down