2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.UserController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for Users
|
|
|
|
"""
|
2018-10-11 17:37:39 +02:00
|
|
|
use MobilizonWeb, :controller
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.User
|
|
|
|
alias Mobilizon.Repo
|
|
|
|
alias Mobilizon.Actors.Service.{Activation, ResetPassword}
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
action_fallback(MobilizonWeb.FallbackController)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
def index(conn, _params) do
|
2018-05-18 09:56:21 +02:00
|
|
|
users = Actors.list_users_with_actors()
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "index.json", users: users)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
def register(conn, %{"username" => username, "email" => email, "password" => password}) do
|
2018-07-27 10:45:35 +02:00
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
Actors.register(%{email: email, password: password, username: username}) do
|
2018-07-04 14:29:17 +02:00
|
|
|
Activation.send_confirmation_email(user, "locale")
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
conn
|
2018-07-27 10:45:35 +02:00
|
|
|
|> put_status(:created)
|
|
|
|
|> render("confirmation.json", %{user: user})
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate(conn, %{"token" => token}) do
|
|
|
|
with {:ok, %User{} = user} <- Activation.check_confirmation_token(token) do
|
2018-10-11 17:37:39 +02:00
|
|
|
{:ok, token, _claims} = MobilizonWeb.Guardian.encode_and_sign(user)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
conn
|
|
|
|
|> put_resp_header("location", user_path(conn, :show_current_actor))
|
|
|
|
|> render("show_with_token.json", %{user: user, token: token})
|
|
|
|
else
|
|
|
|
{:error, msg} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"error" => msg})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
@time_before_resend 3600
|
2018-07-04 14:29:17 +02:00
|
|
|
def resend_confirmation(conn, %{"email" => email}) do
|
|
|
|
with {:ok, %User{} = user} <- Actors.find_by_email(email),
|
|
|
|
false <- is_nil(user.confirmation_token),
|
2018-07-27 10:45:35 +02:00
|
|
|
true <-
|
2018-08-24 11:34:00 +02:00
|
|
|
Timex.before?(
|
|
|
|
Timex.shift(user.confirmation_sent_at, seconds: @time_before_resend),
|
|
|
|
DateTime.utc_now()
|
|
|
|
) do
|
2018-07-04 14:29:17 +02:00
|
|
|
Activation.resend_confirmation_email(user)
|
|
|
|
render(conn, "confirmation.json", %{user: user})
|
|
|
|
else
|
|
|
|
{:error, :not_found} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"error" => "Unable to find an user with this email"})
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
_ ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
2018-08-24 11:34:00 +02:00
|
|
|
|> json(%{
|
|
|
|
"error" =>
|
|
|
|
"Unable to resend the validation token. Please wait a while before you can ask for resending token"
|
|
|
|
})
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_reset_password(conn, %{"email" => email}) do
|
|
|
|
with {:ok, %User{} = user} <- Actors.find_by_email(email),
|
|
|
|
{:ok, _} <- ResetPassword.send_password_reset_email(user) do
|
|
|
|
render(conn, "password_reset.json", %{user: user})
|
|
|
|
else
|
2018-08-24 11:34:00 +02:00
|
|
|
{:error, nil} ->
|
2018-07-04 14:29:17 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"errors" => "Unable to find an user with this email"})
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
{:error, :email_too_soon} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"errors" => "You requested a new reset password too early"})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_password(conn, %{"password" => password, "token" => token}) do
|
|
|
|
with {:ok, %User{} = user} <- ResetPassword.check_reset_password_token(password, token) do
|
2018-10-11 17:37:39 +02:00
|
|
|
{:ok, token, _claims} = MobilizonWeb.Guardian.encode_and_sign(user)
|
2018-07-04 14:29:17 +02:00
|
|
|
render(conn, "show_with_token.json", %{user: user, token: token})
|
|
|
|
else
|
|
|
|
{:error, :invalid_token} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"errors" => %{"token" => ["Wrong token for password reset"]}})
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:unprocessable_entity)
|
2018-10-11 17:37:39 +02:00
|
|
|
|> render(MobilizonWeb.ChangesetView, "error.json", changeset: changeset)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def show_current_actor(conn, _params) do
|
2018-07-27 10:45:35 +02:00
|
|
|
user =
|
|
|
|
conn
|
|
|
|
|> Guardian.Plug.current_resource()
|
|
|
|
|> Repo.preload(:actors)
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "show_simple.json", user: user)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
# defp handle_changeset_errors(errors) do
|
|
|
|
# errors
|
|
|
|
# |> Enum.map(fn {field, detail} ->
|
|
|
|
# "#{field} " <> render_detail(detail)
|
|
|
|
# end)
|
|
|
|
# |> Enum.join()
|
|
|
|
# end
|
|
|
|
|
|
|
|
# defp render_detail({message, values}) do
|
|
|
|
# Enum.reduce(values, message, fn {k, v}, acc ->
|
|
|
|
# String.replace(acc, "%{#{k}}", to_string(v))
|
|
|
|
# end)
|
|
|
|
# end
|
|
|
|
|
|
|
|
# defp render_detail(message) do
|
|
|
|
# message
|
|
|
|
# end
|
2018-01-09 17:52:26 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
def update(conn, %{"id" => id, "user" => user_params}) do
|
2018-05-18 09:56:21 +02:00
|
|
|
user = Actors.get_user!(id)
|
2018-01-09 17:52:26 +01:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
with {:ok, %User{} = user} <- Actors.update_user(user, user_params) do
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "show.json", user: user)
|
|
|
|
end
|
|
|
|
end
|
2018-01-09 17:52:26 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
def delete(conn, %{"id" => id}) do
|
2018-05-18 09:56:21 +02:00
|
|
|
user = Actors.get_user!(id)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
with {:ok, %User{}} <- Actors.delete_user(user) do
|
2018-01-13 23:33:03 +01:00
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
2018-01-09 17:52:26 +01:00
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|