From 245594f32b8f4ca8408a6ff61d099df5dcfbe84e Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Fri, 2 Sep 2022 18:46:17 +0200 Subject: [PATCH] Don't allow to modify answer's images for started polls Note that the `create` action doesn't create an image but updates an answer instead. We're removing the references to `:create` in the abilities since it isn't used. In the future we might change the form to add an image to an answer because it's been broken for ages since it shows all the attached images. --- .../questions/answers/images_controller.rb | 3 +- app/models/abilities/administrator.rb | 4 +- app/models/abilities/common.rb | 4 +- .../questions/answers/images/index.html.erb | 32 +++++--- config/locales/en/admin.yml | 3 + config/locales/es/admin.yml | 3 + .../answers/images_controller_spec.rb | 52 +++++++++++++ spec/factories/polls.rb | 4 + spec/models/abilities/administrator_spec.rb | 7 +- .../questions/answers/images/images_spec.rb | 78 +++++++++++++------ 10 files changed, 151 insertions(+), 39 deletions(-) create mode 100644 spec/controllers/admin/poll/questions/answers/images_controller_spec.rb diff --git a/app/controllers/admin/poll/questions/answers/images_controller.rb b/app/controllers/admin/poll/questions/answers/images_controller.rb index a209b058f..9b60f3f4c 100644 --- a/app/controllers/admin/poll/questions/answers/images_controller.rb +++ b/app/controllers/admin/poll/questions/answers/images_controller.rb @@ -2,6 +2,7 @@ class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseContr include ImageAttributes load_and_authorize_resource :answer, class: "::Poll::Question::Answer" + load_and_authorize_resource only: [:destroy] def index end @@ -11,6 +12,7 @@ class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseContr def create @answer.attributes = images_params + authorize! :update, @answer if @answer.save redirect_to admin_answer_images_path(@answer), @@ -21,7 +23,6 @@ class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseContr end def destroy - @image = ::Image.find(params[:id]) @image.destroy! respond_to do |format| diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index ee323ef80..3c94e802e 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -103,8 +103,8 @@ module Abilities can [:create, :update, :destroy], Poll::Question::Answer::Video do |video| can?(:update, video.answer) end - can [:create, :destroy], Image do |image| - image.imageable_type == "Poll::Question::Answer" + can [:destroy], Image do |image| + image.imageable_type == "Poll::Question::Answer" && can?(:update, image.imageable) end can :manage, SiteCustomization::Page diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index 049a2c8b5..264b98521 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -74,7 +74,9 @@ module Abilities document.documentable&.author_id == user.id end - can [:destroy], Image, imageable: { author_id: user.id } + can [:destroy], Image do |image| + image.imageable_type != "Poll::Question::Answer" && image.imageable&.author_id == user.id + end can [:create, :destroy], DirectUpload diff --git a/app/views/admin/poll/questions/answers/images/index.html.erb b/app/views/admin/poll/questions/answers/images/index.html.erb index f031f2b1f..e3f2b3e92 100644 --- a/app/views/admin/poll/questions/answers/images/index.html.erb +++ b/app/views/admin/poll/questions/answers/images/index.html.erb @@ -1,8 +1,20 @@ <%= back_link_to admin_question_path(@answer.question) %> -<%= link_to t("admin.questions.answers.images.add_image"), - new_admin_answer_image_path(@answer), - class: "button hollow float-right" %> +
+ +

+ <%= t("admin.answers.images.index.title") %> +

+ +<% if can?(:update, @answer) %> + <%= link_to t("admin.questions.answers.images.add_image"), + new_admin_answer_image_path(@answer), + class: "button hollow float-right" %> +<% else %> +
+ <%= t("admin.questions.no_edit") %> +
+<% end %>