From 664822dbd204e224a57b5e2b5efeacd99d92d920 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 16 Nov 2019 21:53:41 -0500 Subject: [PATCH] use canary mapper --- lib/philomena/users/ability.ex | 3 +++ .../controllers/image/fave_controller.ex | 1 + .../controllers/image/hide_controller.ex | 1 + .../controllers/image/vote_controller.ex | 1 + lib/philomena_web/plugs/canary_map_plug.ex | 17 +++++++++++++++++ 5 files changed, 23 insertions(+) create mode 100644 lib/philomena_web/plugs/canary_map_plug.ex diff --git a/lib/philomena/users/ability.ex b/lib/philomena/users/ability.ex index dc575c4f..49efcd64 100644 --- a/lib/philomena/users/ability.ex +++ b/lib/philomena/users/ability.ex @@ -64,6 +64,9 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do when action in [:show, :index], do: true + # Vote on images they can see + def can?(user, :vote, image), do: can?(user, :show, image) + # View non-deleted comments def can?(_user, :show, %Comment{hidden_from_users: false}), do: true diff --git a/lib/philomena_web/controllers/image/fave_controller.ex b/lib/philomena_web/controllers/image/fave_controller.ex index 484aba29..aa39b9bb 100644 --- a/lib/philomena_web/controllers/image/fave_controller.ex +++ b/lib/philomena_web/controllers/image/fave_controller.ex @@ -7,6 +7,7 @@ defmodule PhilomenaWeb.Image.FaveController do alias Ecto.Multi plug PhilomenaWeb.Plugs.FilterBannedUsers + plug PhilomenaWeb.Plugs.CanaryMapPlug, create: :vote, delete: :vote plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true def create(conn, _params) do diff --git a/lib/philomena_web/controllers/image/hide_controller.ex b/lib/philomena_web/controllers/image/hide_controller.ex index 50870062..d1e532c1 100644 --- a/lib/philomena_web/controllers/image/hide_controller.ex +++ b/lib/philomena_web/controllers/image/hide_controller.ex @@ -7,6 +7,7 @@ defmodule PhilomenaWeb.Image.HideController do alias Ecto.Multi plug PhilomenaWeb.Plugs.FilterBannedUsers + plug PhilomenaWeb.Plugs.CanaryMapPlug, create: :vote, delete: :vote plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true def create(conn, _params) do diff --git a/lib/philomena_web/controllers/image/vote_controller.ex b/lib/philomena_web/controllers/image/vote_controller.ex index 7c1d0034..eaf3031b 100644 --- a/lib/philomena_web/controllers/image/vote_controller.ex +++ b/lib/philomena_web/controllers/image/vote_controller.ex @@ -7,6 +7,7 @@ defmodule PhilomenaWeb.Image.VoteController do alias Ecto.Multi plug PhilomenaWeb.Plugs.FilterBannedUsers + plug PhilomenaWeb.Plugs.CanaryMapPlug, create: :vote, delete: :vote plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true def create(conn, params) do diff --git a/lib/philomena_web/plugs/canary_map_plug.ex b/lib/philomena_web/plugs/canary_map_plug.ex new file mode 100644 index 00000000..660ca6d4 --- /dev/null +++ b/lib/philomena_web/plugs/canary_map_plug.ex @@ -0,0 +1,17 @@ +defmodule PhilomenaWeb.Plugs.CanaryMapPlug do + import Plug.Conn + + def init(opts), do: opts + + def call(conn, opts) do + phx_action = conn.private.phoenix_action + canary_action = + case Keyword.fetch(opts, phx_action) do + {:ok, action} -> action + _ -> phx_action + end + + conn + |> assign(:canary_action, canary_action) + end +end \ No newline at end of file