add filter override param

This commit is contained in:
byte[] 2019-12-03 20:00:48 -05:00
parent 12fb45170c
commit a9c276ae2f
2 changed files with 21 additions and 0 deletions

View file

@ -0,0 +1,20 @@
defmodule PhilomenaWeb.FilterIdPlug do
alias Philomena.Filters.Filter
alias Philomena.Repo
# No options
def init([]), do: false
def call(conn, _opts) do
filter = load_filter(conn.params)
user = conn.assigns.current_user
case Canada.Can.can?(user, :show, filter) do
true -> Plug.Conn.assign(conn, :current_filter, filter)
false -> conn
end
end
defp load_filter(%{"filter_id" => filter_id}), do: Repo.get(Filter, filter_id)
defp load_filter(_params), do: nil
end

View file

@ -25,6 +25,7 @@ defmodule PhilomenaWeb.Router do
plug PhilomenaWeb.ApiTokenPlug plug PhilomenaWeb.ApiTokenPlug
plug PhilomenaWeb.EnsureUserEnabledPlug plug PhilomenaWeb.EnsureUserEnabledPlug
plug PhilomenaWeb.CurrentFilterPlug plug PhilomenaWeb.CurrentFilterPlug
plug PhilomenaWeb.FilterIdPlug
plug PhilomenaWeb.ImageFilterPlug plug PhilomenaWeb.ImageFilterPlug
plug PhilomenaWeb.PaginationPlug plug PhilomenaWeb.PaginationPlug
end end