diff --git a/lib/accounts/user.ex b/lib/accounts/user.ex index bcaf506..65eef35 100644 --- a/lib/accounts/user.ex +++ b/lib/accounts/user.ex @@ -86,7 +86,13 @@ defmodule Mv.Accounts.User do # - :create_user (for manual user creation with optional member link) # - :register_with_password (for password-based registration) # - :register_with_rauthy (for OIDC-based registration) - defaults [:read, :destroy] + defaults [:read] + + destroy :destroy do + primary? true + # Required because custom validation (system actor protection) cannot run atomically + require_atomic? false + end # Primary generic update action: # - Selected by AshAdmin's generated "Edit" UI and generic AshPhoenix @@ -359,6 +365,19 @@ defmodule Mv.Accounts.User do :ok end end + + # Prevent deletion of the system actor user (required for internal operations) + validate fn changeset, _context -> + if to_string(changeset.data.email) == Mv.Helpers.SystemActor.system_user_email() do + {:error, + field: :email, + message: + "Cannot delete system actor user. This user is required for internal operations."} + else + :ok + end + end, + on: [:destroy] end def validate_oidc_id_present(changeset, _context) do