From f4c2207a8004cba4c8a54f7d4383f548794a50a8 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 27 Apr 2025 13:58:47 +0300 Subject: [PATCH] Update the Oban Dashboard to show only for admin users. --- .../plugs/require_admin.ex | 21 +++++++++++++++++++ lib/decentralised_book_index_web/router.ex | 16 ++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 lib/decentralised_book_index_web/plugs/require_admin.ex diff --git a/lib/decentralised_book_index_web/plugs/require_admin.ex b/lib/decentralised_book_index_web/plugs/require_admin.ex new file mode 100644 index 0000000..879164a --- /dev/null +++ b/lib/decentralised_book_index_web/plugs/require_admin.ex @@ -0,0 +1,21 @@ +defmodule DecentralisedBookIndexWeb.Plugs.RequireAdmin do + import Plug.Conn + import Phoenix.Controller + + alias DecentralisedBookIndex.Accounts.Role + + def init(default), do: default + + def call(conn, _opts) do + current_user = conn.assigns[:current_user] + + if current_user && Role.can_administrate?(current_user.role) do + conn + else + conn + |> put_flash(:error, "Unauthorized") + |> redirect(to: "/") + |> halt() + end + end +end diff --git a/lib/decentralised_book_index_web/router.ex b/lib/decentralised_book_index_web/router.ex index 82ad057..c8b1cd1 100644 --- a/lib/decentralised_book_index_web/router.ex +++ b/lib/decentralised_book_index_web/router.ex @@ -23,6 +23,10 @@ defmodule DecentralisedBookIndexWeb.Router do plug :set_actor, :user end + pipeline :admin_authenticated_routes do + plug DecentralisedBookIndexWeb.Plugs.RequireAdmin + end + scope "/api/v1/json" do pipe_through [:api] @@ -90,6 +94,12 @@ defmodule DecentralisedBookIndexWeb.Router do end end + scope "/" do + pipe_through [:browser, :admin_authenticated_routes] + + oban_dashboard("/oban") + end + scope "/", DecentralisedBookIndexWeb do pipe_through :browser @@ -135,11 +145,5 @@ defmodule DecentralisedBookIndexWeb.Router do live_dashboard "/dashboard", metrics: DecentralisedBookIndexWeb.Telemetry forward "/mailbox", Plug.Swoosh.MailboxPreview end - - scope "/" do - pipe_through :browser - - oban_dashboard("/oban") - end end end