[#114] Added email confirmation resend action. Added tests
for registration, authentication, email confirmation, confirmation resending. Made admin methods create confirmed users.
This commit is contained in:
parent
aed0f90287
commit
b096e30cff
9 changed files with 230 additions and 16 deletions
|
|
@ -103,8 +103,8 @@ defmodule Mix.Tasks.Pleroma.User do
|
|||
bio: bio
|
||||
}
|
||||
|
||||
user = User.register_changeset(%User{}, params)
|
||||
Repo.insert!(user)
|
||||
changeset = User.register_changeset(%User{}, params, confirmed: true)
|
||||
{:ok, _user} = User.register(changeset)
|
||||
|
||||
Mix.shell().info("User #{nickname} created")
|
||||
|
||||
|
|
|
|||
|
|
@ -74,13 +74,15 @@ defmodule Pleroma.User do
|
|||
|
||||
def user_info(%User{} = user) do
|
||||
oneself = if user.local, do: 1, else: 0
|
||||
user_info = user.info
|
||||
|
||||
%{
|
||||
following_count: length(user.following) - oneself,
|
||||
note_count: user.info.note_count,
|
||||
follower_count: user.info.follower_count,
|
||||
locked: user.info.locked,
|
||||
default_scope: user.info.default_scope
|
||||
note_count: user_info.note_count,
|
||||
follower_count: user_info.follower_count,
|
||||
locked: user_info.locked,
|
||||
confirmation_pending: user_info.confirmation_pending,
|
||||
default_scope: user_info.default_scope
|
||||
}
|
||||
end
|
||||
|
||||
|
|
@ -209,17 +211,21 @@ defmodule Pleroma.User do
|
|||
@doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
|
||||
def register(%Ecto.Changeset{} = changeset) do
|
||||
with {:ok, user} <- Repo.insert(changeset) do
|
||||
if user.info.confirmation_pending do
|
||||
{:ok, _} =
|
||||
user
|
||||
|> Pleroma.UserEmail.account_confirmation_email()
|
||||
|> Pleroma.Mailer.deliver()
|
||||
end
|
||||
|
||||
{:ok, _} = try_send_confirmation_email(user)
|
||||
{:ok, user}
|
||||
end
|
||||
end
|
||||
|
||||
def try_send_confirmation_email(%User{} = user) do
|
||||
if user.info.confirmation_pending do
|
||||
user
|
||||
|> Pleroma.UserEmail.account_confirmation_email()
|
||||
|> Pleroma.Mailer.deliver()
|
||||
else
|
||||
{:ok, :noop}
|
||||
end
|
||||
end
|
||||
|
||||
def needs_update?(%User{local: true}), do: false
|
||||
|
||||
def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||
}) do
|
||||
with %User{} = user <- User.get_by_nickname_or_email(name),
|
||||
true <- Pbkdf2.checkpw(password, user.password_hash),
|
||||
true <- User.auth_active?(user),
|
||||
{:auth_active, true} <- {:auth_active, User.auth_active?(user)},
|
||||
%App{} = app <- Repo.get_by(App, client_id: client_id),
|
||||
{:ok, auth} <- Authorization.create_authorization(app, user) do
|
||||
# Special case: Local MastodonFE.
|
||||
|
|
@ -64,6 +64,15 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||
|
||||
redirect(conn, external: url)
|
||||
end
|
||||
else
|
||||
{:auth_active, false} ->
|
||||
conn
|
||||
|> put_flash(:error, "Account confirmation pending")
|
||||
|> put_status(:forbidden)
|
||||
|> authorize(params)
|
||||
|
||||
error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -102,7 +111,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||
with %App{} = app <- get_app_from_request(conn, params),
|
||||
%User{} = user <- User.get_by_nickname_or_email(name),
|
||||
true <- Pbkdf2.checkpw(password, user.password_hash),
|
||||
true <- User.auth_active?(user),
|
||||
{:auth_active, true} <- {:auth_active, User.auth_active?(user)},
|
||||
{:ok, auth} <- Authorization.create_authorization(app, user),
|
||||
{:ok, token} <- Token.exchange_token(app, auth) do
|
||||
response = %{
|
||||
|
|
@ -115,6 +124,11 @@ defmodule Pleroma.Web.OAuth.OAuthController do
|
|||
|
||||
json(conn, response)
|
||||
else
|
||||
{:auth_active, false} ->
|
||||
conn
|
||||
|> put_status(:forbidden)
|
||||
|> json(%{error: "Account confirmation pending"})
|
||||
|
||||
_error ->
|
||||
put_status(conn, 400)
|
||||
|> json(%{error: "Invalid credentials"})
|
||||
|
|
|
|||
|
|
@ -284,6 +284,8 @@ defmodule Pleroma.Web.Router do
|
|||
|
||||
get("/account/confirm_email/:token", TwitterAPI.Controller, :confirm_email, as: :confirm_email)
|
||||
|
||||
post("/account/resend_confirmation_email", TwitterAPI.Controller, :resend_confirmation_email)
|
||||
|
||||
get("/search", TwitterAPI.Controller, :search)
|
||||
get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
require Logger
|
||||
|
||||
plug(:only_if_public_instance when action in [:public_timeline, :public_and_external_timeline])
|
||||
plug(:fetch_flash when action in [:confirm_email])
|
||||
plug(:fetch_flash when action in [:confirm_email, :resend_confirmation_email])
|
||||
action_fallback(:errors)
|
||||
|
||||
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|
||||
|
|
@ -385,6 +385,17 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
end
|
||||
end
|
||||
|
||||
def resend_confirmation_email(conn, params) do
|
||||
nickname_or_email = params["email"] || params["nickname"]
|
||||
|
||||
with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
|
||||
{:ok, _} <- User.try_send_confirmation_email(user) do
|
||||
conn
|
||||
|> put_flash(:info, "Email confirmation has been sent.")
|
||||
|> json_response(:no_content, "")
|
||||
end
|
||||
end
|
||||
|
||||
def update_avatar(%{assigns: %{user: user}} = conn, params) do
|
||||
{:ok, object} = ActivityPub.upload(params, type: :avatar)
|
||||
change = Changeset.change(user, %{avatar: object.data})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue