2020-12-15 14:24:14 +01:00
|
|
|
defmodule RecycledCloudWeb.UserRegistrationController do
|
|
|
|
use RecycledCloudWeb, :controller
|
|
|
|
|
2021-02-03 10:12:23 +01:00
|
|
|
alias RecycledCloud.Captcha
|
2020-12-15 14:24:14 +01:00
|
|
|
alias RecycledCloud.Accounts
|
|
|
|
alias RecycledCloud.Accounts.User
|
|
|
|
alias RecycledCloudWeb.UserAuth
|
|
|
|
|
2021-01-08 14:41:37 +01:00
|
|
|
defp is_registration_enabled?() do
|
|
|
|
Application.get_env(:recycledcloud, :enable_registration)
|
|
|
|
end
|
|
|
|
|
2020-12-15 14:24:14 +01:00
|
|
|
def new(conn, _params) do
|
|
|
|
changeset = Accounts.change_user_registration(%User{})
|
2021-02-03 10:12:23 +01:00
|
|
|
{captcha_text, captcha_result} = Captcha.generate()
|
2021-01-08 14:41:37 +01:00
|
|
|
|
|
|
|
with_notice = unless is_registration_enabled?() do
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Registration is disabled for the time being.")
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
|
|
|
|
with_notice
|
2021-02-03 10:12:23 +01:00
|
|
|
|> assign(:captcha, captcha_text)
|
|
|
|
|> put_session(:captcha, captcha_result)
|
2020-12-21 15:40:19 +01:00
|
|
|
|> render("new.html", changeset: changeset)
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"user" => user_params}) do
|
2021-01-08 14:41:37 +01:00
|
|
|
unless is_registration_enabled?() do
|
|
|
|
conn
|
|
|
|
|> redirect(to: Routes.user_registration_path(conn, :new))
|
|
|
|
else
|
2021-02-03 10:12:23 +01:00
|
|
|
attrs = user_params
|
|
|
|
|> Map.put("expected_captcha", get_session(conn, :captcha))
|
|
|
|
|
|
|
|
case Accounts.register_user(attrs) do
|
2021-01-08 14:41:37 +01:00
|
|
|
{:ok, user} ->
|
|
|
|
{:ok, _} =
|
|
|
|
Accounts.deliver_user_confirmation_instructions(
|
|
|
|
user,
|
|
|
|
&Routes.user_confirmation_url(conn, :confirm, &1)
|
|
|
|
)
|
2020-12-15 14:24:14 +01:00
|
|
|
|
2021-01-08 14:41:37 +01:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "User created successfully.")
|
|
|
|
|> UserAuth.log_in_user(user)
|
2020-12-15 14:24:14 +01:00
|
|
|
|
2021-01-08 14:41:37 +01:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2021-02-03 10:12:23 +01:00
|
|
|
{captcha_text, captcha_result} = Captcha.generate()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:captcha, captcha_text)
|
|
|
|
|> put_session(:captcha, captcha_result)
|
|
|
|
|> render("new.html", changeset: changeset)
|
2021-01-08 14:41:37 +01:00
|
|
|
end
|
|
|
|
end
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
end
|