From 9a36cfb65bfa33ec59d63f338db8528399a7d759 Mon Sep 17 00:00:00 2001 From: Adrian Salamon Date: Mon, 30 Oct 2023 08:48:50 -0700 Subject: [PATCH] add export csv button --- lib/haj/policy/policy.ex | 5 +++++ lib/haj_web/controllers/group_controller.ex | 16 ++++++---------- lib/haj_web/live/group_live/admin.html.heex | 15 ++++++++++++--- lib/haj_web/live/group_live/show.html.heex | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/haj/policy/policy.ex b/lib/haj/policy/policy.ex index 90fa528..3dc89d8 100644 --- a/lib/haj/policy/policy.ex +++ b/lib/haj/policy/policy.ex @@ -61,6 +61,11 @@ defmodule Haj.Policy do allow :is_chef allow role: :admin end + + action :export do + allow :is_chef + allow role: :admin + end end object :responsibility_comment do diff --git a/lib/haj_web/controllers/group_controller.ex b/lib/haj_web/controllers/group_controller.ex index d0af441..12dd603 100644 --- a/lib/haj_web/controllers/group_controller.ex +++ b/lib/haj_web/controllers/group_controller.ex @@ -1,16 +1,20 @@ defmodule HajWeb.GroupController do use HajWeb, :controller + alias Haj.Policy def csv(conn, %{"show_group_id" => show_group_id}) do show_group = Haj.Spex.get_show_group!(show_group_id) users = Enum.map(show_group.group_memberships, fn %{user: user} -> user end) - if is_admin?(conn, show_group) do + if Policy.authorize?(:show_group_export, conn.assigns.current_user, show_group) do csv_data = to_csv(users) conn |> put_resp_content_type("text/csv") - |> put_resp_header("content-disposition", "attachment; filename=\"medlemmar.csv\"") + |> put_resp_header( + "content-disposition", + "attachment; filename=\"haj-medlemmar-#{show_group.group.name}.csv\"" + ) |> put_root_layout(false) |> send_resp(200, csv_data) else @@ -68,12 +72,4 @@ defmodule HajWeb.GroupController do CSV.encode([titles | users]) |> Enum.to_list() end - - defp is_admin?(conn, show_group) do - conn.assigns.current_user.role == :admin || - show_group.group_memberships - |> Enum.any?(fn %{user_id: id, role: role} -> - role == :chef && id == conn.assigns.current_user.id - end) - end end diff --git a/lib/haj_web/live/group_live/admin.html.heex b/lib/haj_web/live/group_live/admin.html.heex index fab53f9..f0426f2 100644 --- a/lib/haj_web/live/group_live/admin.html.heex +++ b/lib/haj_web/live/group_live/admin.html.heex @@ -19,9 +19,9 @@

Välj vilken typ av medlem (chef/gruppis), sök på användare och lägg sedan till!

-
+
<.form :let={f} for={%{}} as={:role_form} phx-change="update_role"> - <%= select(f, :role, @roles, class: "h-full rounded-md border-zinc-400", value: @role) %> + <%= select(f, :role, @roles, class: "h-full w-full rounded-md border-zinc-300", value: @role) %> <.form @@ -33,8 +33,17 @@ autocomplete={:off} class="flex-grow" > - <%= text_input(f, :q, value: @query, class: "w-full rounded-md border-zinc-400") %> + <%= text_input(f, :q, value: @query, class: "w-full rounded-md border-zinc-300") %> + + <.link + href={~p"/group/#{@show_group}/csv"} + method="post" + class="ml-auto flex flex-row items-center gap-2 rounded-lg border px-3 py-2 hover:bg-gray-50" + > + <.icon name={:arrow_down_on_square_stack} mini class="h-5 w-5" /> + Exportera +
    diff --git a/lib/haj_web/live/group_live/show.html.heex b/lib/haj_web/live/group_live/show.html.heex index 9e69b5b..29b0681 100644 --- a/lib/haj_web/live/group_live/show.html.heex +++ b/lib/haj_web/live/group_live/show.html.heex @@ -35,7 +35,7 @@ Administrera
-
+

Vad gör <%= @group.group.name %>?

<%= @group.group.description %>