Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

CRM: Expose 2FA user status & allow CS to force disable it (#5670)

Adam Rutkowskia5a71f782879
diff --git a/extra/lib/plausible_web/live/customer_support/user/components/overview.ex b/extra/lib/plausible_web/live/customer_support/user/components/overview.ex
index 3845deff3f..77ee4a47be 100644
--- a/extra/lib/plausible_web/live/customer_support/user/components/overview.ex
+++ b/extra/lib/plausible_web/live/customer_support/user/components/overview.ex
@@ -5,6 +5,8 @@ defmodule PlausibleWeb.CustomerSupport.User.Components.Overview do
use PlausibleWeb, :live_component
import PlausibleWeb.CustomerSupport.Live
+ alias Plausible.Auth.TOTP
+
def update(%{user: user}, socket) do
form = user |> Plausible.Auth.User.changeset() |> to_form()
{:ok, assign(socket, user: user, form: form)}
@@ -13,6 +15,22 @@ defmodule PlausibleWeb.CustomerSupport.User.Components.Overview do
def render(assigns) do
~H"""
<div class="mt-8">
+ <div class="mb-8">
+ <div class="mt-4 flex items-center justify-between">
+ <div>
+ <span class="text-sm">
+ Two-Factor Authentication:
+ <span class={[
+ "text-sm font-medium",
+ if(TOTP.enabled?(@user), do: "text-green-600", else: "text-gray-500")
+ ]}>
+ {if TOTP.enabled?(@user), do: "Enabled", else: "Disabled"}
+ </span>
+ </span>
+ </div>
+ </div>
+ </div>
+
<.table rows={@user.team_memberships}>
<:thead>
<.th>Team</.th>
@@ -33,17 +51,30 @@ defmodule PlausibleWeb.CustomerSupport.User.Components.Overview do
<.form :let={f} for={@form} phx-target={@myself} phx-submit="save-user" class="mt-8">
<.input type="textarea" field={f[:notes]} label="Notes" />
<div class="flex justify-between">
- <.button phx-target={@myself} type="submit">
- Save
- </.button>
- <.button
- phx-target={@myself}
- phx-click="delete-user"
- data-confirm="Are you sure you want to delete this user?"
- theme="danger"
- >
- Delete User
- </.button>
+ <div>
+ <.button phx-target={@myself} type="submit">
+ Save
+ </.button>
+ </div>
+ <div class="flex gap-2">
+ <.button
+ :if={TOTP.enabled?(@user)}
+ phx-target={@myself}
+ phx-click="force-disable-2fa"
+ data-confirm="Are you sure you want to force disable 2FA for this user? This action cannot be undone."
+ theme="danger"
+ >
+ Force Disable 2FA
+ </.button>
+ <.button
+ phx-target={@myself}
+ phx-click="delete-user"
+ data-confirm="Are you sure you want to delete this user?"
+ theme="danger"
+ >
+ Delete User
+ </.button>
+ </div>
</div>
</.form>
</div>
@@ -68,6 +99,14 @@ defmodule PlausibleWeb.CustomerSupport.User.Components.Overview do
end
end
+ def handle_event("force-disable-2fa", _params, socket) do
+ user = socket.assigns.user
+
+ {:ok, updated_user} = TOTP.force_disable(user)
+ send(self(), {:success, "2FA has been force disabled for this user"})
+ {:noreply, assign(socket, user: updated_user)}
+ end
+
def handle_event("save-user", %{"user" => params}, socket) do
user = socket.assigns.user
diff --git a/test/plausible_web/live/customer_support/users_test.exs b/test/plausible_web/live/customer_support/users_test.exs
index ed274feaea..8ed51938c5 100644
--- a/test/plausible_web/live/customer_support/users_test.exs
+++ b/test/plausible_web/live/customer_support/users_test.exs
@@ -112,5 +112,50 @@ defmodule PlausibleWeb.Live.CustomerSupport.UsersTest do
refute html =~ k3.key_prefix
end
end
+
+ describe "2FA management" do
+ setup [:create_user, :log_in, :create_site]
+
+ setup %{user: user} do
+ patch_env(:super_admin_user_ids, [user.id])
+ end
+
+ test "shows 2FA disabled status when user has no 2FA", %{conn: conn, user: user} do
+ {:ok, _lv, html} = live(conn, open_user(user.id))
+
+ assert text(html) =~ "Two-Factor Authentication: Disabled"
+ refute text(html) =~ "Force Disable 2FA"
+ end
+
+ test "shows 2FA enabled status and disable button when user has 2FA", %{
+ conn: conn,
+ user: user
+ } do
+ {:ok, user, _} = Plausible.Auth.TOTP.initiate(user)
+ {:ok, user, _} = Plausible.Auth.TOTP.enable(user, :skip_verify)
+
+ {:ok, _lv, html} = live(conn, open_user(user.id))
+
+ assert text(html) =~ "Two-Factor Authentication: Enabled"
+ assert text(html) =~ "Force Disable 2FA"
+ end
+
+ test "force disables 2FA when button is clicked", %{conn: conn, user: user} do
+ {:ok, user, _} = Plausible.Auth.TOTP.initiate(user)
+ {:ok, user, _} = Plausible.Auth.TOTP.enable(user, :skip_verify)
+
+ {:ok, lv, _html} = live(conn, open_user(user.id))
+
+ lv |> element("[phx-click='force-disable-2fa']") |> render_click()
+
+ html = render(lv)
+ assert text(html) =~ "2FA has been force disabled"
+ assert text(html) =~ "Two-Factor Authentication: Disabled"
+ refute text(html) =~ "Force Disable 2FA"
+
+ updated_user = Plausible.Repo.reload(user)
+ refute Plausible.Auth.TOTP.enabled?(updated_user)
+ end
+ end
end
end