2018-11-06 10:30:27 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.User do
|
|
|
|
alias Mobilizon.Actors.{User, Actor}
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Find an user by it's ID
|
|
|
|
"""
|
|
|
|
def find_user(_parent, %{id: id}, _resolution) do
|
2018-11-12 18:17:53 +01:00
|
|
|
Actors.get_user_with_actors(id)
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Return current logged-in user
|
|
|
|
"""
|
|
|
|
def get_current_user(_parent, _args, %{context: %{current_user: user}}) do
|
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_current_user(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to view current user"}
|
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc """
|
2018-11-06 10:30:27 +01:00
|
|
|
Login an user. Returns a token and the user
|
|
|
|
"""
|
|
|
|
def login_user(_parent, %{email: email, password: password}, _resolution) do
|
|
|
|
with {:ok, %User{} = user} <- Actors.get_user_by_email(email, true),
|
|
|
|
{:ok, token, _} <- Actors.authenticate(%{user: user, password: password}),
|
|
|
|
%Actor{} = actor <- Actors.get_actor_for_user(user) do
|
2018-11-28 14:48:55 +01:00
|
|
|
{:ok, %{token: token, user: user, person: actor}}
|
2018-11-06 10:30:27 +01:00
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
|
|
|
{:error, "User with email not found"}
|
|
|
|
|
|
|
|
{:error, :unauthorized} ->
|
|
|
|
{:error, "Impossible to authenticate"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc """
|
2018-11-06 10:30:27 +01:00
|
|
|
Register an user :
|
|
|
|
- create the user
|
|
|
|
- create the actor
|
|
|
|
- set the user's default_actor to the newly created actor
|
|
|
|
- send a validation email to the user
|
|
|
|
"""
|
|
|
|
@spec create_user_actor(any(), map(), any()) :: tuple()
|
|
|
|
def create_user_actor(_parent, args, _resolution) do
|
|
|
|
with {:ok, %Actor{user: user} = actor} <- Actors.register(args) do
|
|
|
|
Mobilizon.Actors.Service.Activation.send_confirmation_email(user)
|
|
|
|
{:ok, actor}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Validate an user, get it's actor and a token
|
|
|
|
"""
|
|
|
|
def validate_user(_parent, %{token: token}, _resolution) do
|
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
Mobilizon.Actors.Service.Activation.check_confirmation_token(token),
|
|
|
|
%Actor{} = actor <- Actors.get_actor_for_user(user),
|
|
|
|
{:ok, token, _} <- MobilizonWeb.Guardian.encode_and_sign(user) do
|
2018-11-23 15:03:53 +01:00
|
|
|
{:ok, %{token: token, user: user, person: actor}}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send the confirmation email again.
|
|
|
|
We only do this to accounts unconfirmed
|
|
|
|
"""
|
|
|
|
def resend_confirmation_email(_parent, %{email: email, locale: locale}, _resolution) do
|
|
|
|
with {:ok, user} <- Actors.get_user_by_email(email, false),
|
2018-11-06 10:32:53 +01:00
|
|
|
{:ok, email} <-
|
|
|
|
Mobilizon.Actors.Service.Activation.resend_confirmation_email(user, locale) do
|
2018-11-06 10:30:27 +01:00
|
|
|
{:ok, email}
|
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
|
|
|
{:error, "No user to validate with this email was found"}
|
2018-11-06 10:32:53 +01:00
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
{:error, :email_too_soon} ->
|
|
|
|
{:error, "You requested again a confirmation email too soon"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send an email to reset the password from an user
|
|
|
|
"""
|
|
|
|
def send_reset_password(_parent, %{email: email, locale: locale}, _resolution) do
|
|
|
|
with {:ok, user} <- Actors.get_user_by_email(email, false),
|
2018-11-27 17:54:54 +01:00
|
|
|
{:ok, %Bamboo.Email{} = _email_html} <-
|
2018-11-06 10:32:53 +01:00
|
|
|
Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user, locale) do
|
2018-11-06 10:30:27 +01:00
|
|
|
{:ok, email}
|
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
2018-11-27 17:54:54 +01:00
|
|
|
# TODO : implement rate limits for this endpoint
|
|
|
|
{:error, "No user with this email was found"}
|
2018-11-06 10:32:53 +01:00
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
{:error, :email_too_soon} ->
|
|
|
|
{:error, "You requested again a confirmation email too soon"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Reset the password from an user
|
|
|
|
"""
|
|
|
|
def reset_password(_parent, %{password: password, token: token}, _resolution) do
|
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
Mobilizon.Actors.Service.ResetPassword.check_reset_password_token(password, token),
|
|
|
|
%Actor{} = actor <- Actors.get_actor_for_user(user),
|
|
|
|
{:ok, token, _} <- MobilizonWeb.Guardian.encode_and_sign(user) do
|
|
|
|
{:ok, %{token: token, user: user, actor: actor}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc "Change an user default actor"
|
2018-11-06 10:30:27 +01:00
|
|
|
def change_default_actor(_parent, %{preferred_username: username}, %{
|
|
|
|
context: %{current_user: user}
|
|
|
|
}) do
|
|
|
|
with %Actor{id: id} <- Actors.get_local_actor_by_name(username) do
|
|
|
|
Actors.update_user(user, %{default_actor_id: id})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|