2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.UserController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for Users
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use EventosWeb, :controller
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
alias Eventos.Actors
|
|
|
|
alias Eventos.Actors.User
|
2018-01-13 23:33:03 +01:00
|
|
|
alias Eventos.Repo
|
2018-07-04 14:29:17 +02:00
|
|
|
alias Eventos.Actors.Service.{Activation, ResetPassword}
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
action_fallback EventosWeb.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-04 14:29:17 +02:00
|
|
|
with {:ok, %User{} = user} <- Actors.register(%{email: email, password: password, username: username}) do
|
|
|
|
Activation.send_confirmation_email(user, "locale")
|
|
|
|
conn
|
2018-01-13 23:33:03 +01:00
|
|
|
|> put_status(:created)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> render("confirmation.json", %{user: user})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate(conn, %{"token" => token}) do
|
|
|
|
with {:ok, %User{} = user} <- Activation.check_confirmation_token(token) do
|
|
|
|
{:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user)
|
|
|
|
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
|
|
|
|
|
|
|
|
def resend_confirmation(conn, %{"email" => email}) do
|
|
|
|
with {:ok, %User{} = user} <- Actors.find_by_email(email),
|
|
|
|
false <- is_nil(user.confirmation_token),
|
|
|
|
true <- Timex.before?(Timex.shift(user.confirmation_sent_at, hours: 1), DateTime.utc_now()) do
|
|
|
|
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"})
|
|
|
|
_ ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"error" => "Unable to resend the validation token"})
|
|
|
|
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
|
|
|
|
{:error, :not_found} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"errors" => "Unable to find an user with this email"})
|
|
|
|
{: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
|
|
|
|
{:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user)
|
|
|
|
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"]}})
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:unprocessable_entity)
|
|
|
|
|> render(EventosWeb.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-04 17:16:02 +02:00
|
|
|
user = conn
|
|
|
|
|> Guardian.Plug.current_resource()
|
2018-07-10 10:00:07 +02:00
|
|
|
|> 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-01-13 23:33:03 +01:00
|
|
|
defp handle_changeset_errors(errors) do
|
2018-01-14 17:56:50 +01:00
|
|
|
errors
|
|
|
|
|> Enum.map(fn {field, detail} ->
|
2018-01-13 23:33:03 +01:00
|
|
|
"#{field} " <> render_detail(detail)
|
|
|
|
end)
|
|
|
|
|> Enum.join
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
defp render_detail({message, values}) do
|
|
|
|
Enum.reduce values, message, fn {k, v}, acc ->
|
|
|
|
String.replace(acc, "%{#{k}}", to_string(v))
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
defp render_detail(message) do
|
|
|
|
message
|
2017-12-08 09:58:14 +01:00
|
|
|
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)
|
|
|
|
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
|