From b2fed59209a92624884df38a477837cba9a8dbd9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 11 Oct 2020 18:52:35 -0500 Subject: [PATCH 01/11] Handle User.post_register_action/1 in steps --- lib/pleroma/user.ex | 30 +++++++++++++++++-- .../web/twitter_api/twitter_api_controller.ex | 3 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 09ea80793..3f40ac9b3 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -773,12 +773,37 @@ def register(%Ecto.Changeset{} = changeset) do end def post_register_action(%User{} = user) do + instance_config = + Config.get(:instance) + |> Enum.into(%{}) + + do_post_register_action(user, instance_config) + end + + defp do_post_register_action(%User{confirmation_pending: true} = user, %{ + account_activation_required: true + }) do + with {:ok, _} <- try_send_confirmation_email(user) do + {:ok, user} + end + end + + defp do_post_register_action(%User{approval_pending: true} = user, %{ + account_approval_required: true + }) do + # TODO: Send approval explanation email + {:ok, user} + end + + defp do_post_register_action( + %User{approval_pending: false, confirmation_pending: false} = user, + _instance_config + ) do with {:ok, user} <- autofollow_users(user), {:ok, user} <- set_cache(user), {:ok, _} <- send_welcome_email(user), {:ok, _} <- send_welcome_message(user), - {:ok, _} <- send_welcome_chat_message(user), - {:ok, _} <- try_send_confirmation_email(user) do + {:ok, _} <- send_welcome_chat_message(user) do {:ok, user} end end @@ -1570,6 +1595,7 @@ def approve(users) when is_list(users) do def approve(%User{} = user) do change(user, approval_pending: false) |> update_and_set_cache() + |> post_register_action() end def update_notification_settings(%User{} = user, settings) do diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index c2de26b0b..25dad547c 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -34,7 +34,8 @@ def confirm_email(conn, %{"user_id" => uid, "token" => token}) do {:ok, _} <- user |> User.confirmation_changeset(need_confirmation: false) - |> User.update_and_set_cache() do + |> User.update_and_set_cache() + |> User.post_register_action() do redirect(conn, to: "/") end end From c69b20540958b4de9cc4fd66595d43f26bcd5fd1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 11 Oct 2020 19:25:34 -0500 Subject: [PATCH 02/11] Registration: user state is separate from instance state --- lib/pleroma/user.ex | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 3f40ac9b3..3a4f031b1 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -772,33 +772,18 @@ def register(%Ecto.Changeset{} = changeset) do end end - def post_register_action(%User{} = user) do - instance_config = - Config.get(:instance) - |> Enum.into(%{}) - - do_post_register_action(user, instance_config) - end - - defp do_post_register_action(%User{confirmation_pending: true} = user, %{ - account_activation_required: true - }) do + def post_register_action(%User{confirmation_pending: true} = user) do with {:ok, _} <- try_send_confirmation_email(user) do {:ok, user} end end - defp do_post_register_action(%User{approval_pending: true} = user, %{ - account_approval_required: true - }) do + def post_register_action(%User{approval_pending: true} = user) do # TODO: Send approval explanation email {:ok, user} end - defp do_post_register_action( - %User{approval_pending: false, confirmation_pending: false} = user, - _instance_config - ) do + def post_register_action(%User{approval_pending: false, confirmation_pending: false} = user) do with {:ok, user} <- autofollow_users(user), {:ok, user} <- set_cache(user), {:ok, _} <- send_welcome_email(user), From 28005563f00028981cf516cceb16c2b55bd0e97c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 11 Oct 2020 20:50:09 -0500 Subject: [PATCH 03/11] Send approval pending email during registration --- lib/pleroma/emails/user_email.ex | 13 +++++++++++++ lib/pleroma/user.ex | 6 +++++- test/emails/user_email_test.exs | 11 +++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index 1d8c72ae9..831e5464f 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -93,6 +93,19 @@ def account_confirmation_email(user) do |> html_body(html_body) end + def approval_pending_email(user) do + html_body = """ +

Awaiting Approval

+

Your account at #{instance_name()} is being reviewed by staff. You will receive another email once your account is approved.

+ """ + + new() + |> to(recipient(user)) + |> from(sender()) + |> subject("Your account is awaiting approval") + |> html_body(html_body) + end + @doc """ Email used in digest email notifications Includes Mentions and New Followers data diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 3a4f031b1..cde9ff0eb 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -779,7 +779,11 @@ def post_register_action(%User{confirmation_pending: true} = user) do end def post_register_action(%User{approval_pending: true} = user) do - # TODO: Send approval explanation email + # Send approval pending email + user + |> Pleroma.Emails.UserEmail.approval_pending_email() + |> Pleroma.Emails.Mailer.deliver_async() + {:ok, user} end diff --git a/test/emails/user_email_test.exs b/test/emails/user_email_test.exs index a75623bb4..a214e59a7 100644 --- a/test/emails/user_email_test.exs +++ b/test/emails/user_email_test.exs @@ -45,4 +45,15 @@ test "build account confirmation email" do assert email.html_body =~ Router.Helpers.confirm_email_url(Endpoint, :confirm_email, user.id, "conf-token") end + + test "build approval pending email" do + config = Pleroma.Config.get(:instance) + user = insert(:user) + email = UserEmail.approval_pending_email(user) + + assert email.from == {config[:name], config[:notify_email]} + assert email.to == [{user.name, user.email}] + assert email.subject == "Your account is awaiting approval" + assert email.html_body =~ "Awaiting Approval" + end end From 521e965884d916c21d76ff12544e678b7fcdb1d4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 11 Oct 2020 21:38:01 -0500 Subject: [PATCH 04/11] Registration tests --- lib/pleroma/user.ex | 8 +++++--- .../web/twitter_api/twitter_api_controller.ex | 4 ++-- test/user_test.exs | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index cde9ff0eb..ae2fe93fc 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1582,9 +1582,11 @@ def approve(users) when is_list(users) do end def approve(%User{} = user) do - change(user, approval_pending: false) - |> update_and_set_cache() - |> post_register_action() + with chg <- change(user, approval_pending: false), + {:ok, user} <- update_and_set_cache(chg) do + post_register_action(user) + {:ok, user} + end end def update_notification_settings(%User{} = user, settings) do diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 25dad547c..6961118ca 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -34,8 +34,8 @@ def confirm_email(conn, %{"user_id" => uid, "token" => token}) do {:ok, _} <- user |> User.confirmation_changeset(need_confirmation: false) - |> User.update_and_set_cache() - |> User.post_register_action() do + |> User.update_and_set_cache() do + User.post_register_action(user) redirect(conn, to: "/") end end diff --git a/test/user_test.exs b/test/user_test.exs index d506f7047..2d3a6564b 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -517,6 +517,22 @@ test "it sends a confirm email" do |> assert_email_sent() end + test "sends a pending approval email" do + clear_config([:instance, :account_approval_required], true) + + {:ok, user} = + User.register_changeset(%User{}, @full_user_data) + |> User.register() + + ObanHelpers.perform_all() + + assert_email_sent( + from: Pleroma.Config.Helpers.sender(), + to: {user.name, user.email}, + subject: "Your account is awaiting approval" + ) + end + test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do Pleroma.Config.put([:instance, :account_activation_required], true) From bb8c0614efa978ad33d15fdafd452792180a1c15 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 11 Oct 2020 21:44:22 -0500 Subject: [PATCH 05/11] Move admin approval email logic into User.post_register_action/1 --- lib/pleroma/user.ex | 11 ++++++++++- lib/pleroma/web/twitter_api/twitter_api.ex | 13 ------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index ae2fe93fc..b56620c54 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -779,11 +779,20 @@ def post_register_action(%User{confirmation_pending: true} = user) do end def post_register_action(%User{approval_pending: true} = user) do - # Send approval pending email + # Send approval pending email to user user |> Pleroma.Emails.UserEmail.approval_pending_email() |> Pleroma.Emails.Mailer.deliver_async() + # Notify admins + all_superusers() + |> Enum.filter(fn user -> not is_nil(user.email) end) + |> Enum.each(fn superuser -> + superuser + |> Pleroma.Emails.AdminEmail.new_unapproved_registration(user) + |> Pleroma.Emails.Mailer.deliver_async() + end) + {:ok, user} end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 5d7948507..8e20b0d55 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -45,7 +45,6 @@ defp create_user(params, opts) do case User.register(changeset) do {:ok, user} -> - maybe_notify_admins(user) {:ok, user} {:error, changeset} -> @@ -58,18 +57,6 @@ defp create_user(params, opts) do end end - defp maybe_notify_admins(%User{} = account) do - if Pleroma.Config.get([:instance, :account_approval_required]) do - User.all_superusers() - |> Enum.filter(fn user -> not is_nil(user.email) end) - |> Enum.each(fn superuser -> - superuser - |> Pleroma.Emails.AdminEmail.new_unapproved_registration(account) - |> Pleroma.Emails.Mailer.deliver_async() - end) - end - end - def password_reset(nickname_or_email) do with true <- is_binary(nickname_or_email), %User{local: true, email: email, deactivated: false} = user when is_binary(email) <- From 9ddc292ca82ae14754a4a2d71105832eddbb126e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 12 Oct 2020 15:24:53 -0500 Subject: [PATCH 06/11] TwitterAPI: test pending approval user email --- test/web/twitter_api/twitter_api_test.exs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 20a45cb6f..4a418dee3 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -80,13 +80,9 @@ test "it sends confirmation email if :account_activation_required is specified i end test "it sends an admin email if :account_approval_required is specified in instance config" do - admin = insert(:user, is_admin: true) - setting = Pleroma.Config.get([:instance, :account_approval_required]) + clear_config([:instance, :account_approval_required], true) - unless setting do - Pleroma.Config.put([:instance, :account_approval_required], true) - on_exit(fn -> Pleroma.Config.put([:instance, :account_approval_required], setting) end) - end + admin = insert(:user, is_admin: true) data = %{ :username => "lain", @@ -103,15 +99,24 @@ test "it sends an admin email if :account_approval_required is specified in inst assert user.approval_pending - email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user) + user_email = Pleroma.Emails.UserEmail.approval_pending_email(user) + admin_email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user) notify_email = Pleroma.Config.get([:instance, :notify_email]) instance_name = Pleroma.Config.get([:instance, :name]) + # User approval email + Swoosh.TestAssertions.assert_email_sent( + from: {instance_name, notify_email}, + to: {user.name, user.email}, + html_body: user_email.html_body + ) + + # Admin email Swoosh.TestAssertions.assert_email_sent( from: {instance_name, notify_email}, to: {admin.name, admin.email}, - html_body: email.html_body + html_body: admin_email.html_body ) end From 6ebec50df643325a524926858371d43f44e4c6da Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 12 Oct 2020 16:32:34 -0500 Subject: [PATCH 07/11] Refactor User.confirm/1, add more tests --- lib/pleroma/user.ex | 16 +++++ .../web/twitter_api/twitter_api_controller.ex | 6 +- test/user_test.exs | 72 +++++++++++++++++++ 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index b56620c54..4329fde12 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1598,6 +1598,22 @@ def approve(%User{} = user) do end end + def confirm(users) when is_list(users) do + Repo.transaction(fn -> + Enum.map(users, fn user -> + with {:ok, user} <- confirm(user), do: user + end) + end) + end + + def confirm(%User{} = user) do + with chg <- confirmation_changeset(user, need_confirmation: false), + {:ok, user} <- update_and_set_cache(chg) do + post_register_action(user) + {:ok, user} + end + end + def update_notification_settings(%User{} = user, settings) do user |> cast(%{notification_settings: settings}, []) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 6961118ca..b12606472 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -31,11 +31,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do def confirm_email(conn, %{"user_id" => uid, "token" => token}) do with %User{} = user <- User.get_cached_by_id(uid), true <- user.local and user.confirmation_pending and user.confirmation_token == token, - {:ok, _} <- - user - |> User.confirmation_changeset(need_confirmation: false) - |> User.update_and_set_cache() do - User.post_register_action(user) + {:ok, _} <- User.confirm(user) do redirect(conn, to: "/") end end diff --git a/test/user_test.exs b/test/user_test.exs index 2d3a6564b..e83275fa5 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -1352,6 +1352,78 @@ test "approves a list of users" do assert false == user.approval_pending end) end + + test "it sends welcome email if it is set" do + clear_config([:welcome, :email, :enabled], true) + clear_config([:welcome, :email, :sender], "tester@test.me") + + user = insert(:user, approval_pending: true) + welcome_user = insert(:user, email: "tester@test.me") + instance_name = Pleroma.Config.get([:instance, :name]) + + User.approve(user) + + ObanHelpers.perform_all() + + assert_email_sent( + from: {instance_name, welcome_user.email}, + to: {user.name, user.email}, + html_body: "Welcome to #{instance_name}" + ) + end + end + + describe "confirm" do + test "confirms a user" do + user = insert(:user, confirmation_pending: true) + assert true == user.confirmation_pending + {:ok, user} = User.confirm(user) + assert false == user.confirmation_pending + end + + test "confirms a list of users" do + unconfirmed_users = [ + insert(:user, confirmation_pending: true), + insert(:user, confirmation_pending: true), + insert(:user, confirmation_pending: true) + ] + + {:ok, users} = User.confirm(unconfirmed_users) + + assert Enum.count(users) == 3 + + Enum.each(users, fn user -> + assert false == user.confirmation_pending + end) + end + + test "sends approval emails when `approval_pending: true`" do + admin = insert(:user, is_admin: true) + user = insert(:user, confirmation_pending: true, approval_pending: true) + User.confirm(user) + + ObanHelpers.perform_all() + + user_email = Pleroma.Emails.UserEmail.approval_pending_email(user) + admin_email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user) + + notify_email = Pleroma.Config.get([:instance, :notify_email]) + instance_name = Pleroma.Config.get([:instance, :name]) + + # User approval email + assert_email_sent( + from: {instance_name, notify_email}, + to: {user.name, user.email}, + html_body: user_email.html_body + ) + + # Admin email + assert_email_sent( + from: {instance_name, notify_email}, + to: {admin.name, admin.email}, + html_body: admin_email.html_body + ) + end end describe "delete" do From cb29769a224104882ed7572087f8cd2db48475ef Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 12 Oct 2020 16:42:59 -0500 Subject: [PATCH 08/11] Make User.confirm/1 and User.approve/1 idempotent --- lib/pleroma/user.ex | 8 ++++++-- test/user_test.exs | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 4329fde12..c6767cfca 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1590,7 +1590,7 @@ def approve(users) when is_list(users) do end) end - def approve(%User{} = user) do + def approve(%User{approval_pending: true} = user) do with chg <- change(user, approval_pending: false), {:ok, user} <- update_and_set_cache(chg) do post_register_action(user) @@ -1598,6 +1598,8 @@ def approve(%User{} = user) do end end + def approve(%User{} = user), do: {:ok, user} + def confirm(users) when is_list(users) do Repo.transaction(fn -> Enum.map(users, fn user -> @@ -1606,7 +1608,7 @@ def confirm(users) when is_list(users) do end) end - def confirm(%User{} = user) do + def confirm(%User{confirmation_pending: true} = user) do with chg <- confirmation_changeset(user, need_confirmation: false), {:ok, user} <- update_and_set_cache(chg) do post_register_action(user) @@ -1614,6 +1616,8 @@ def confirm(%User{} = user) do end end + def confirm(%User{} = user), do: {:ok, user} + def update_notification_settings(%User{} = user, settings) do user |> cast(%{notification_settings: settings}, []) diff --git a/test/user_test.exs b/test/user_test.exs index e83275fa5..18a143919 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -1371,6 +1371,17 @@ test "it sends welcome email if it is set" do html_body: "Welcome to #{instance_name}" ) end + + test "approving an approved user does not trigger post-register actions" do + clear_config([:welcome, :email, :enabled], true) + + user = insert(:user, approval_pending: false) + User.approve(user) + + ObanHelpers.perform_all() + + assert_no_email_sent() + end end describe "confirm" do @@ -1424,6 +1435,15 @@ test "sends approval emails when `approval_pending: true`" do html_body: admin_email.html_body ) end + + test "confirming a confirmed user does not trigger post-register actions" do + user = insert(:user, confirmation_pending: false, approval_pending: true) + User.confirm(user) + + ObanHelpers.perform_all() + + assert_no_email_sent() + end end describe "delete" do From 66e00ace7c0708f2f9361bc6e1008ccea08cb6ef Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 12 Oct 2020 17:21:08 -0500 Subject: [PATCH 09/11] Refactor User.post_register_action/1 emails --- lib/pleroma/user.ex | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index c6767cfca..0978cc02c 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -779,21 +779,10 @@ def post_register_action(%User{confirmation_pending: true} = user) do end def post_register_action(%User{approval_pending: true} = user) do - # Send approval pending email to user - user - |> Pleroma.Emails.UserEmail.approval_pending_email() - |> Pleroma.Emails.Mailer.deliver_async() - - # Notify admins - all_superusers() - |> Enum.filter(fn user -> not is_nil(user.email) end) - |> Enum.each(fn superuser -> - superuser - |> Pleroma.Emails.AdminEmail.new_unapproved_registration(user) - |> Pleroma.Emails.Mailer.deliver_async() - end) - - {:ok, user} + with {:ok, _} <- send_user_approval_email(user), + {:ok, _} <- send_admin_approval_emails(user) do + {:ok, user} + end end def post_register_action(%User{approval_pending: false, confirmation_pending: false} = user) do @@ -806,6 +795,26 @@ def post_register_action(%User{approval_pending: false, confirmation_pending: fa end end + defp send_user_approval_email(user) do + user + |> Pleroma.Emails.UserEmail.approval_pending_email() + |> Pleroma.Emails.Mailer.deliver_async() + + {:ok, :enqueued} + end + + defp send_admin_approval_emails(user) do + all_superusers() + |> Enum.filter(fn user -> not is_nil(user.email) end) + |> Enum.each(fn superuser -> + superuser + |> Pleroma.Emails.AdminEmail.new_unapproved_registration(user) + |> Pleroma.Emails.Mailer.deliver_async() + end) + + {:ok, :enqueued} + end + def send_welcome_message(user) do if User.WelcomeMessage.enabled?() do User.WelcomeMessage.post_message(user) From dc38dc847207e4724265fbeb111d0a236b75f93f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 13 Oct 2020 21:52:06 -0500 Subject: [PATCH 10/11] Replace User.toggle_confirmation/1 with User.confirm/1, fixes #2235 --- CHANGELOG.md | 1 + docs/administration/CLI_tasks/user.md | 4 ++-- lib/mix/tasks/pleroma/user.ex | 4 ++-- lib/pleroma/user.ex | 12 ------------ .../controllers/admin_api_controller.ex | 2 +- test/mix/tasks/pleroma/user_test.exs | 14 +++++++------- test/pleroma/user_test.exs | 18 ------------------ 7 files changed, 13 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fc1750d1..c546dd0bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Users with the `discoverable` field set to false will not show up in searches. - Minimum lifetime for ephmeral activities changed to 10 minutes and made configurable (`:min_lifetime` option). - Introduced optional dependencies on `ffmpeg`, `ImageMagick`, `exiftool` software packages. Please refer to `docs/installation/optional/media_graphics_packages.md`. +- Changed `mix pleroma.user toggle_confirmed` to `mix pleroma.user confirm` ### Added - Media preview proxy (requires `ffmpeg` and `ImageMagick` to be installed and media proxy to be enabled; see `:media_preview_proxy` config for more details). diff --git a/docs/administration/CLI_tasks/user.md b/docs/administration/CLI_tasks/user.md index c64ed4f22..b57dce0e7 100644 --- a/docs/administration/CLI_tasks/user.md +++ b/docs/administration/CLI_tasks/user.md @@ -264,13 +264,13 @@ === "OTP" ```sh - ./bin/pleroma_ctl user toggle_confirmed + ./bin/pleroma_ctl user confirm ``` === "From Source" ```sh - mix pleroma.user toggle_confirmed + mix pleroma.user confirm ``` ## Set confirmation status for all regular active users diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index e06262804..c454f1d28 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -345,11 +345,11 @@ def run(["delete_activities", nickname]) do end end - def run(["toggle_confirmed", nickname]) do + def run(["confirm", nickname]) do start_pleroma() with %User{} = user <- User.get_cached_by_nickname(nickname) do - {:ok, user} = User.toggle_confirmation(user) + {:ok, user} = User.confirm(user) message = if user.confirmation_pending, do: "needs", else: "doesn't need" diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 0978cc02c..0dabb2a1e 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2113,18 +2113,6 @@ def touch_last_digest_emailed_at(user) do updated_user end - @spec toggle_confirmation(User.t()) :: {:ok, User.t()} | {:error, Changeset.t()} - def toggle_confirmation(%User{} = user) do - user - |> confirmation_changeset(need_confirmation: !user.confirmation_pending) - |> update_and_set_cache() - end - - @spec toggle_confirmation([User.t()]) :: [{:ok, User.t()} | {:error, Changeset.t()}] - def toggle_confirmation(users) do - Enum.map(users, &toggle_confirmation/1) - end - @spec need_confirmation(User.t(), boolean()) :: {:ok, User.t()} | {:error, Changeset.t()} def need_confirmation(%User{} = user, bool) do user diff --git a/lib/pleroma/web/admin_api/controllers/admin_api_controller.ex b/lib/pleroma/web/admin_api/controllers/admin_api_controller.ex index bdd3e195d..c2bd441ee 100644 --- a/lib/pleroma/web/admin_api/controllers/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/admin_api_controller.ex @@ -655,7 +655,7 @@ def reload_emoji(conn, _params) do def confirm_email(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do users = Enum.map(nicknames, &User.get_cached_by_nickname/1) - User.toggle_confirmation(users) + User.confirm(users) ModerationLog.insert_log(%{actor: admin, subject: users, action: "confirm_email"}) diff --git a/test/mix/tasks/pleroma/user_test.exs b/test/mix/tasks/pleroma/user_test.exs index b8c423c48..f58690dbe 100644 --- a/test/mix/tasks/pleroma/user_test.exs +++ b/test/mix/tasks/pleroma/user_test.exs @@ -457,24 +457,24 @@ test "it prints an error message when user is not exist" do end end - describe "running toggle_confirmed" do + describe "running confirm" do test "user is confirmed" do %{id: id, nickname: nickname} = insert(:user, confirmation_pending: false) - assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname]) + assert :ok = Mix.Tasks.Pleroma.User.run(["confirm", nickname]) assert_received {:mix_shell, :info, [message]} - assert message == "#{nickname} needs confirmation." + assert message == "#{nickname} doesn't need confirmation." user = Repo.get(User, id) - assert user.confirmation_pending - assert user.confirmation_token + refute user.confirmation_pending + refute user.confirmation_token end test "user is not confirmed" do %{id: id, nickname: nickname} = insert(:user, confirmation_pending: true, confirmation_token: "some token") - assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname]) + assert :ok = Mix.Tasks.Pleroma.User.run(["confirm", nickname]) assert_received {:mix_shell, :info, [message]} assert message == "#{nickname} doesn't need confirmation." @@ -484,7 +484,7 @@ test "user is not confirmed" do end test "it prints an error message when user is not exist" do - Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"]) + Mix.Tasks.Pleroma.User.run(["confirm", "foo"]) assert_received {:mix_shell, :error, [message]} assert message =~ "No local user" diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 18a143919..2c8ad2089 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -1940,24 +1940,6 @@ test "Only includes users with no read notifications" do end end - describe "toggle_confirmation/1" do - test "if user is confirmed" do - user = insert(:user, confirmation_pending: false) - {:ok, user} = User.toggle_confirmation(user) - - assert user.confirmation_pending - assert user.confirmation_token - end - - test "if user is unconfirmed" do - user = insert(:user, confirmation_pending: true, confirmation_token: "some token") - {:ok, user} = User.toggle_confirmation(user) - - refute user.confirmation_pending - refute user.confirmation_token - end - end - describe "ensure_keys_present" do test "it creates keys for a user and stores them in info" do user = insert(:user) From 4999efad3f0bb0a70a5be5d5c2b970ab564668ca Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 21 Nov 2020 10:24:45 -0600 Subject: [PATCH 11/11] Update CHANGELOG.md: registration workflow --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a402b80ff..cedba088c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Polls now always return a `voters_count`, even if they are single-choice. - Admin Emails: The ap id is used as the user link in emails now. +- Improved registration workflow for email confirmation and account approval modes. - **Breaking:** Changed `mix pleroma.user toggle_confirmed` to `mix pleroma.user confirm` ### Added