Skip to content

Commit

Permalink
Do not leak all elements for guest user in API
Browse files Browse the repository at this point in the history
CanCanCan does not respect any scope set before `accessible_by`.

We need to make sure the additional scopes get called afterwards.
  • Loading branch information
tvdeyen committed Jul 1, 2021
1 parent 0b24271 commit d3b37a0
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions app/controllers/alchemy/api/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ class Api::ElementsController < Api::BaseController
# If you want to only load a specific type of element pass ?named=an_element_name
#
def index
if params[:page_id].present?
@page = Page.find(params[:page_id])
@elements = @page.elements.not_nested
# Fix for cancancan not able to merge multiple AR scopes for logged in users
if cannot? :manage, Alchemy::Element
@elements = Alchemy::Element.accessible_by(current_ability, :index)
else
@elements = Element.not_nested.joins(:page_version).merge(PageVersion.published)
@elements = Alchemy::Element.all
end

# Fix for cancancan not able to merge multiple AR scopes for logged in users
if cannot? :manage, Alchemy::Element
@elements = @elements.accessible_by(current_ability, :index)
@elements = @elements.not_nested.joins(:page_version).merge(PageVersion.published)

if params[:page_id].present?
@elements = @elements.where(alchemy_pages: { id: params[:page_id] })
end

if params[:named].present?
@elements = @elements.named(params[:named])
end
Expand Down

0 comments on commit d3b37a0

Please sign in to comment.