From 751fa5bc7432121aa7459991bfa6d2b634957c3c Mon Sep 17 00:00:00 2001 From: Elin Olsson Date: Sat, 28 Dec 2024 19:24:37 +0100 Subject: [PATCH] Move device move audits to templates --- lib/nerves_hub/audit_logs/templates.ex | 22 +++++++++++++++++++++- lib/nerves_hub/devices.ex | 16 +++++----------- lib/nerves_hub_web/live/devices/index.ex | 10 ++++++++-- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/nerves_hub/audit_logs/templates.ex b/lib/nerves_hub/audit_logs/templates.ex index 476ab093d..62265c840 100644 --- a/lib/nerves_hub/audit_logs/templates.ex +++ b/lib/nerves_hub/audit_logs/templates.ex @@ -1,5 +1,6 @@ defmodule NervesHub.AuditLogs.Templates do alias NervesHub.AuditLogs + alias NervesHub.Repo require Logger @@ -12,6 +13,25 @@ defmodule NervesHub.AuditLogs.Templates do AuditLogs.audit!(user, device, description) end + @doc """ + Creates audit log entries for device, target product and source product on device move. + Rollbacks on failure. + """ + def audit_device_moved(user, device, target_product, source_product) do + description = + "user #{user.name} moved device #{device.identifier} to #{target_product.org.name} : #{target_product.name}" + + Repo.transaction(fn -> + AuditLogs.audit(user, device, description) + AuditLogs.audit(user, target_product, description) + AuditLogs.audit(user, source_product, description) + end) + |> case do + {:ok, result} -> result + {:error, error} -> Repo.rollback(error) + end + end + def audit_request_action(user, device, action) do description = "#{user.name} requested the device (#{device.identifier}) #{action}" AuditLogs.audit!(user, device, description) @@ -96,7 +116,7 @@ defmodule NervesHub.AuditLogs.Templates do AuditLogs.audit_with_ref!(deployment, device, description, reference_id) end - ### RESOUCE: DEPLOYMENT + ### RESOURCE: DEPLOYMENT def audit_deployment_created(user, deployment) do description = "#{user.name} created deployment #{deployment.name}" diff --git a/lib/nerves_hub/devices.ex b/lib/nerves_hub/devices.ex index 3b2a24e9d..b7749c018 100644 --- a/lib/nerves_hub/devices.ex +++ b/lib/nerves_hub/devices.ex @@ -31,6 +31,8 @@ defmodule NervesHub.Devices do alias NervesHub.Repo alias NervesHub.TaskSupervisor, as: Tasks + require Logger + @min_fwup_delta_updatable_version ">=1.10.0" def get_device(device_id) when is_integer(device_id) do @@ -1051,9 +1053,6 @@ defmodule NervesHub.Devices do _ = maybe_copy_firmware_keys(device, product.org) - description = - "user #{user.name} moved device #{device.identifier} to #{product.org.name} : #{product.name}" - source_product = %Product{ id: device.product_id, org_id: device.org_id @@ -1061,14 +1060,8 @@ defmodule NervesHub.Devices do Multi.new() |> Multi.run(:move, fn _, _ -> update_device(device, attrs) end) - |> Multi.run(:audit_device, fn _, _ -> - AuditLogs.audit(user, device, description) - end) - |> Multi.run(:audit_target, fn _, _ -> - AuditLogs.audit(user, product, description) - end) - |> Multi.run(:audit_source, fn _, _ -> - AuditLogs.audit(user, source_product, description) + |> Multi.run(:audit, fn _, _ -> + Templates.audit_device_moved(user, device, product, source_product) end) |> Repo.transaction() |> case do @@ -1077,6 +1070,7 @@ defmodule NervesHub.Devices do {:ok, updated} err -> + Logger.warning("Could not move device: #{inspect(err)}") err end end diff --git a/lib/nerves_hub_web/live/devices/index.ex b/lib/nerves_hub_web/live/devices/index.ex index b9d6eb8a4..f24ac351a 100644 --- a/lib/nerves_hub_web/live/devices/index.ex +++ b/lib/nerves_hub_web/live/devices/index.ex @@ -273,7 +273,7 @@ defmodule NervesHubWeb.Live.Devices.Index do end def handle_event("move-devices", _, socket) do - %{ok: successfuls} = + %{ok: successfuls, error: errors} = Devices.get_devices_by_id(socket.assigns.selected_devices) |> Devices.move_many(socket.assigns.target_product, socket.assigns.user) @@ -286,7 +286,13 @@ defmodule NervesHubWeb.Live.Devices.Index do |> assign_display_devices() |> assign(:target_product, nil) - {:noreply, socket} + case length(errors) do + 0 -> + {:noreply, socket} + + num -> + {:noreply, put_flash(socket, :error, "Could not move #{num} device(s)")} + end end def handle_event("disable-updates-for-devices", _, socket) do