Skip to content

Commit

Permalink
Move device move audits to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
elinol committed Dec 28, 2024
1 parent 0b20c12 commit 751fa5b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
22 changes: 21 additions & 1 deletion lib/nerves_hub/audit_logs/templates.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule NervesHub.AuditLogs.Templates do
alias NervesHub.AuditLogs
alias NervesHub.Repo

require Logger

Expand All @@ -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)
Expand Down Expand Up @@ -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}"
Expand Down
16 changes: 5 additions & 11 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1051,24 +1053,15 @@ 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
}

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
Expand All @@ -1077,6 +1070,7 @@ defmodule NervesHub.Devices do
{:ok, updated}

err ->
Logger.warning("Could not move device: #{inspect(err)}")
err
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 751fa5b

Please sign in to comment.