Commit aa2598ea9de41fcec569dd2cf67b77b109d73a28

Thomas de Grivel 2022-01-17T07:30:01

TOTP activation and removal notification emails

diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index c3829a7..75e2990 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -279,9 +279,15 @@ defmodule Kmxgit.UserManager do
   end
 
   def update_user_totp(user = %User{}, params) do
-    user
+    case user
     |> User.totp_changeset(params)
     |> Repo.update()
+      do
+      {:ok, user1} ->
+        if (user.totp_last == 0 && user1.totp_last != 0), do: UserNotifier.deliver_totp_enabled_email(user)
+        {:ok, user1}
+      x -> x
+    end
   end
 
   def verify_user_totp(user = %User{}, token) do
@@ -289,9 +295,15 @@ defmodule Kmxgit.UserManager do
   end
 
   def delete_user_totp(user = %User{}) do
-    user
+    case user
     |> User.totp_changeset(:delete)
     |> Repo.update()
+      do
+      {:ok, user1} ->
+        if (user.totp_last != 0 && user1.totp_last == 0), do: UserNotifier.deliver_totp_disabled_email(user)
+        {:ok, user1}
+      x -> x
+    end
   end
 
   def admin_user_present? do
diff --git a/lib/kmxgit/user_manager/user_notifier.ex b/lib/kmxgit/user_manager/user_notifier.ex
index a971577..9979689 100644
--- a/lib/kmxgit/user_manager/user_notifier.ex
+++ b/lib/kmxgit/user_manager/user_notifier.ex
@@ -97,4 +97,28 @@ defmodule Kmxgit.UserManager.UserNotifier do
     If you didn't request this change, please reply to this e-mail.
     """)
   end
+
+  def deliver_totp_enabled_email(user) do
+    deliver(user.email, "TOTP was enabled", """
+    Hi #{user.email},
+
+    TOTP (Google Authenticator) has been activated on your account.
+    You will need to enter a new TOTP each time you login in addition
+    to login / password.
+
+    If you didn't request this change, please reply to this e-mail.
+    """)
+  end
+
+
+  def deliver_totp_disabled_email(user) do
+    deliver(user.email, "TOTP was disabled !", """
+    Hi #{user.email},
+
+    TOTP (Google Authenticator) has been disabled on your account.
+    You will no longer need to enter a new TOTP each time you login.
+
+    If you didn't request this change, please reply to this e-mail.
+    """)
+  end
 end