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

[6.1] Allow authors to link to all pages again #2505

Merged
merged 1 commit into from
Jun 30, 2023
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: 0 additions & 2 deletions app/controllers/alchemy/api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class Api::PagesController < Api::BaseController
# Returns all pages as json object
#
def index
language = Alchemy::Language.find_by(id: params[:language_id]) || Alchemy::Language.current
@pages = Alchemy::Page.accessible_by(current_ability, :index)
@pages = @pages.where(language: language)
@pages = @pages.includes(*page_includes)
@pages = @pages.ransack(params[:q]).result

Expand Down
8 changes: 4 additions & 4 deletions lib/alchemy/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def alchemy_guest_user_rules
e.public? && !e.restricted?
end

can :read, Alchemy::Page, Alchemy::Page.published.not_restricted do |p|
p.public? && !p.restricted?
can :read, Alchemy::Page, Alchemy::Page.published.not_restricted.from_current_site do |p|
p.public? && !p.restricted? && p.site == Alchemy::Site.current
end
end
end
Expand All @@ -72,8 +72,8 @@ def alchemy_member_rules
e.public?
end

can :read, Alchemy::Page, Alchemy::Page.published do |p|
p.public?
can :read, Alchemy::Page, Alchemy::Page.published.from_current_site do |p|
p.public? && p.site == Alchemy::Site.current
end
end
end
Expand Down
37 changes: 12 additions & 25 deletions spec/controllers/alchemy/api/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ module Alchemy
let(:language_2) { create(:alchemy_language, site: site_2) }
let!(:site_2_page) { create(:alchemy_page, :public, language: language_2) }
let!(:unpublished_page) { create(:alchemy_page, language: default_language) }
let!(:restricted_page) { create(:alchemy_page, :public, :restricted, language: default_language) }

context "as guest user" do
it "only returns public pages for current site" do
it "only returns published unrestricted pages for current site" do
get :index, format: :json
expect(result["pages"].map { |r| r["id"] }).to match_array([
page.parent_id,
Expand All @@ -97,43 +98,29 @@ module Alchemy
end
end

context "as author user" do
context "as member user" do
before do
authorize_user(build(:alchemy_dummy_user, :as_author))
authorize_user(build(:alchemy_dummy_user))
end

it "returns all pages for current site" do
it "only returns all published pages for current site" do
get :index, format: :json
expect(result["pages"].map { |r| r["id"] }).to match_array([
page.parent_id,
page.id,
unpublished_page.id,
restricted_page.id,
])
end
end
end

context "with language_id param" do
subject { get :index, params: { format: :json, language_id: try(:language)&.id } }

let!(:default_language_page) { create(:alchemy_page, :public, language: default_language, name: "same-name") }

context "when a language with that id exists" do
let(:language) { create(:alchemy_language, :klingon) }
let!(:default_language_page) { create(:alchemy_page, :public, language: default_language, name: "same-name") }
let!(:klingon_page) { create(:alchemy_page, :public, language: language, name: "same-name") }

it "only returns results from that language" do
subject
expect(result["pages"].map { |r| r["id"] }).to match_array [language.pages.root.id, klingon_page.id]
context "as author user" do
before do
authorize_user(build(:alchemy_dummy_user, :as_author))
end
end

context "when a language with that id does not exist" do
it "uses the default language" do
subject
expect(result["pages"].map { |r| r["id"] }).to include(default_language_page.id)
expect(result["pages"].map { |r| r["language_code"] }).not_to include("kl")
it "returns all pages" do
get :index, format: :json
expect(result["pages"].map { |r| r["id"] }).to match_array(Alchemy::Page.pluck(:id))
end
end
end
Expand Down