use canary mapper

This commit is contained in:
byte[] 2019-11-16 21:53:41 -05:00
parent f99b11db1d
commit 664822dbd2
5 changed files with 23 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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