diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index b42c7470c4..3f9da3e592 100755 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -243,6 +243,12 @@ def new @hide_dashboard = true @unposted = current_user.unposted_work + # Check if collection is closed and user doesn't have permission to post + if @collection&.closed? && !@collection&.user_is_maintainer?(current_user) + flash[:error] = t(".closed_collection", collection_title: @collection.title) + redirect_to collection_path(@collection) and return + end + if params[:load_unposted] && @unposted @work = @unposted @chapter = @work.first_chapter diff --git a/config/locales/controllers/en.yml b/config/locales/controllers/en.yml index 1b2490d77f..18229cd430 100644 --- a/config/locales/controllers/en.yml +++ b/config/locales/controllers/en.yml @@ -304,6 +304,8 @@ en: page_title: "%{username} - Drafts" edit_tags: page_title: Edit Work Tags + new: + closed_collection: Sorry, the collection %{collection_title} is closed. New works cannot be added to it. show: page_title: unrevealed: Mystery Work diff --git a/spec/controllers/works/default_rails_actions_spec.rb b/spec/controllers/works/default_rails_actions_spec.rb index 6016291f17..44e97a5d30 100644 --- a/spec/controllers/works/default_rails_actions_spec.rb +++ b/spec/controllers/works/default_rails_actions_spec.rb @@ -195,6 +195,60 @@ def call_with_params(params) it_redirects_to_simple(user_path(banned_user)) expect(flash[:error]).to include("Your account has been banned.") end + + context "when collection is closed" do + let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: true)) } + let(:user) { create(:user) } + + before { fake_login_known_user(user) } + + it "redirects to collection page with error for non-maintainers" do + get :new, params: { collection_id: collection.name } + it_redirects_to_with_error(collection_path(collection), "Sorry, the collection Excalibur is closed. New works cannot be added to it.") + end + end + + context "when collection is closed but user is owner" do + let(:user) { create(:user) } + let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: true)) } + let!(:participant) { collection.collection_participants.create(pseud: user.default_pseud, participant_role: CollectionParticipant::OWNER) } + + before do + fake_login_known_user(user) + end + + it "allows access to new work form" do + get :new, params: { collection_id: collection.name } + expect(response).to render_template("new") + end + end + + context "when collection is closed but user is maintainer" do + let(:user) { create(:user) } + let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: true)) } + let!(:participant) { collection.collection_participants.create(pseud: user.default_pseud, participant_role: CollectionParticipant::MODERATOR) } + + before do + fake_login_known_user(user) + end + + it "allows access to new work form" do + get :new, params: { collection_id: collection.name } + expect(response).to render_template("new") + end + end + + context "when collection is open" do + let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: false)) } + let(:user) { create(:user) } + + before { fake_login_known_user(user) } + + it "allows access to new work form" do + get :new, params: { collection_id: collection.name } + expect(response).to render_template("new") + end + end end describe "create" do