Automatically confirm logged-in users
authorAlex Gleason <alex@alexgleason.me>
Thu, 31 Dec 2020 18:13:08 +0000 (12:13 -0600)
committerAlex Gleason <alex@alexgleason.me>
Thu, 31 Dec 2020 18:13:08 +0000 (12:13 -0600)
lib/pleroma/web/endpoint.ex
lib/pleroma/web/plugs/confirm_user_plug.ex [new file with mode: 0644]
test/pleroma/web/plugs/confirm_user_plug_test.exs [new file with mode: 0644]

index f26542e888c435917c48b08d62664fc569d4aa5d..705035845d0921badfb112052351298ba39832ea 100644 (file)
@@ -169,6 +169,8 @@ defmodule Pleroma.Web.Endpoint do
 
   plug(MetricsExporterCaller)
 
+  plug(Pleroma.Web.Plugs.ConfirmUserPlug)
+
   plug(Pleroma.Web.Router)
 
   @doc """
diff --git a/lib/pleroma/web/plugs/confirm_user_plug.ex b/lib/pleroma/web/plugs/confirm_user_plug.ex
new file mode 100644 (file)
index 0000000..218068d
--- /dev/null
@@ -0,0 +1,30 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Plugs.ConfirmUserPlug do
+  @moduledoc """
+  If a user has ever been granted an OAuth token, they are eligible to become
+  confirmed, regardless of the account_activation_required setting. This plug
+  will confirm a user if found.
+  """
+
+  alias Pleroma.User
+  import Plug.Conn
+
+  def init(opts), do: opts
+
+  def call(%{assigns: %{user: %User{confirmation_pending: true} = user}} = conn, _opts) do
+    with {:ok, user} <- confirm_user(user) do
+      assign(conn, :user, user)
+    end
+  end
+
+  def call(conn, _opts), do: conn
+
+  defp confirm_user(%User{} = user) do
+    user
+    |> User.confirmation_changeset(need_confirmation: false)
+    |> User.update_and_set_cache()
+  end
+end
diff --git a/test/pleroma/web/plugs/confirm_user_plug_test.exs b/test/pleroma/web/plugs/confirm_user_plug_test.exs
new file mode 100644 (file)
index 0000000..43c1c28
--- /dev/null
@@ -0,0 +1,30 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Plugs.ConfirmUserPlugTest do
+  use Pleroma.Web.ConnCase, async: true
+  alias Pleroma.User
+  alias Pleroma.Web.Plugs.ConfirmUserPlug
+  import Pleroma.Factory
+
+  test "it confirms an unconfirmed user", %{conn: conn} do
+    %User{id: user_id} = user = insert(:user, confirmation_pending: true)
+
+    conn =
+      conn
+      |> assign(:user, user)
+      |> ConfirmUserPlug.call(%{})
+
+    assert %Plug.Conn{assigns: %{user: %User{id: ^user_id, confirmation_pending: false}}} = conn
+    assert %User{confirmation_pending: false} = User.get_by_id(user_id)
+  end
+
+  test "it does nothing without an unconfirmed user", %{conn: conn} do
+    assert conn == ConfirmUserPlug.call(conn, %{})
+
+    user = insert(:user, confirmation_pending: false)
+    conn = assign(conn, :user, user)
+    assert conn == ConfirmUserPlug.call(conn, %{})
+  end
+end