Skip to content

Commit

Permalink
authorize return record even with passed record with namespace arry
Browse files Browse the repository at this point in the history
  • Loading branch information
QWYNG committed Nov 10, 2019
1 parent 78c509d commit 62cfec6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def authorize(user, record, query, policy_class: nil)

raise NotAuthorizedError, query: query, record: record, policy: policy unless policy.public_send(query)

record
record.is_a?(Array) ? record.last : record
end

# Retrieves the policy scope for the given record.
Expand Down Expand Up @@ -222,7 +222,7 @@ def authorize(record, query = nil, policy_class: nil)

raise NotAuthorizedError, query: query, record: record, policy: policy unless policy.public_send(query)

record
record.is_a?(Array) ? record.last : record
end

# Allow this action not to perform authorization.
Expand Down
12 changes: 12 additions & 0 deletions spec/pundit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
expect(Pundit.authorize(user, post, :update?)).to be_truthy
end

it "returns the record on successful authorization" do
expect(Pundit.authorize(user, post, :update?)).to be(post)
end

it "returns the record when passed record with namespace " do
expect(Pundit.authorize(user, [:project, comment], :update?)).to be(comment)
end

it "can be given a different policy class" do
expect(Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy)).to be_truthy
end
Expand Down Expand Up @@ -413,6 +421,10 @@
expect(controller.authorize(post)).to be(post)
end

it "returns the record when passed record with namespace " do
expect(Pundit.authorize(user, [:project, comment], :update?)).to be(comment)
end

it "can be given a different permission to check" do
expect(controller.authorize(post, :show?)).to be_truthy
expect { controller.authorize(post, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ class CriteriaPolicy < Struct.new(:user, :criteria); end

module Project
class CommentPolicy < Struct.new(:user, :comment)

def update?
true
end

class Scope < Struct.new(:user, :scope)
def resolve
scope
Expand Down

0 comments on commit 62cfec6

Please sign in to comment.