2018-11-06 09:30:27 +00:00
|
|
|
defmodule MobilizonWeb.Resolvers.User do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles the user-related GraphQL calls
|
|
|
|
"""
|
2019-03-05 16:23:05 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-03-22 09:53:38 +00:00
|
|
|
alias Mobilizon.CommonConfig
|
2019-03-05 16:23:05 +00:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
alias Mobilizon.{Actors, Users}
|
2019-03-14 14:00:34 +00:00
|
|
|
alias Mobilizon.Service.Users.{ResetPassword, Activation}
|
2019-03-06 17:45:26 +00:00
|
|
|
import Mobilizon.Users.Guards
|
2018-11-28 16:16:23 +00:00
|
|
|
require Logger
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Find an user by it's ID
|
|
|
|
"""
|
|
|
|
def find_user(_parent, %{id: id}, _resolution) do
|
2019-03-05 16:23:05 +00:00
|
|
|
Users.get_user_with_actors(id)
|
2018-11-06 09:30:27 +00: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
|
|
|
|
|
2019-03-01 10:41:28 +00:00
|
|
|
@doc """
|
|
|
|
List instance users
|
|
|
|
"""
|
|
|
|
def list_and_count_users(
|
|
|
|
_parent,
|
|
|
|
%{page: page, limit: limit, sort: sort, direction: direction},
|
2019-03-06 17:45:26 +00:00
|
|
|
%{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2019-03-05 16:23:05 +00:00
|
|
|
total = Task.async(&Users.count_users/0)
|
|
|
|
elements = Task.async(fn -> Users.list_users(page, limit, sort, direction) end)
|
2019-03-01 10:41:28 +00:00
|
|
|
|
|
|
|
{:ok, %{total: Task.await(total), elements: Task.await(elements)}}
|
|
|
|
end
|
|
|
|
|
2019-03-06 17:45:26 +00:00
|
|
|
def list_and_count_users(_parent, _args, _resolution),
|
|
|
|
do: {:error, "You need to have admin access to list users"}
|
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
@doc """
|
2018-11-06 09:30:27 +00:00
|
|
|
Login an user. Returns a token and the user
|
|
|
|
"""
|
|
|
|
def login_user(_parent, %{email: email, password: password}, _resolution) do
|
2019-03-05 16:23:05 +00:00
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(email, true),
|
|
|
|
{:ok, token, _} <- Users.authenticate(%{user: user, password: password}) do
|
2018-11-29 16:43:22 +00:00
|
|
|
{:ok, %{token: token, user: user}}
|
2018-11-06 09:30:27 +00:00
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
|
|
|
{:error, "User with email not found"}
|
|
|
|
|
|
|
|
{:error, :unauthorized} ->
|
2019-01-21 14:08:22 +00:00
|
|
|
{:error, "Impossible to authenticate, either your email or password are invalid."}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
@doc """
|
2019-03-22 09:53:38 +00:00
|
|
|
Register an user:
|
|
|
|
- check registrations are enabled
|
2018-11-06 09:30:27 +00:00
|
|
|
- create the user
|
|
|
|
- send a validation email to the user
|
|
|
|
"""
|
2019-01-25 12:59:58 +00:00
|
|
|
@spec create_user(any(), map(), any()) :: tuple()
|
|
|
|
def create_user(_parent, args, _resolution) do
|
2019-03-22 09:53:38 +00:00
|
|
|
with {:registrations_open, true} <- {:registrations_open, CommonConfig.registrations_open?()},
|
|
|
|
{:ok, %User{} = user} <- Users.register(args) do
|
2019-03-05 16:23:05 +00:00
|
|
|
Activation.send_confirmation_email(user)
|
2018-11-29 16:43:22 +00:00
|
|
|
{:ok, user}
|
2019-03-22 09:53:38 +00:00
|
|
|
else
|
|
|
|
{:registrations_open, false} ->
|
|
|
|
{:error, "Registrations are not enabled"}
|
|
|
|
|
|
|
|
err ->
|
|
|
|
err
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Validate an user, get it's actor and a token
|
|
|
|
"""
|
|
|
|
def validate_user(_parent, %{token: token}, _resolution) do
|
2018-11-28 16:16:23 +00:00
|
|
|
with {:check_confirmation_token, {:ok, %User{} = user}} <-
|
2019-03-05 16:23:05 +00:00
|
|
|
{:check_confirmation_token, Activation.check_confirmation_token(token)},
|
|
|
|
{:get_actor, actor} <- {:get_actor, Users.get_actor_for_user(user)},
|
2018-11-28 16:16:23 +00:00
|
|
|
{:guardian_encode_and_sign, {:ok, token, _}} <-
|
|
|
|
{:guardian_encode_and_sign, MobilizonWeb.Guardian.encode_and_sign(user)} do
|
2019-01-30 14:54:21 +00:00
|
|
|
{:ok, %{token: token, user: Map.put(user, :default_actor, actor)}}
|
2018-11-28 16:16:23 +00:00
|
|
|
else
|
|
|
|
err ->
|
|
|
|
Logger.info("Unable to validate user with token #{token}")
|
|
|
|
Logger.debug(inspect(err))
|
2019-01-25 12:59:58 +00:00
|
|
|
{:error, "Unable to validate user"}
|
2018-11-06 09:30:27 +00: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
|
2019-03-05 16:23:05 +00:00
|
|
|
with {:ok, user} <- Users.get_user_by_email(email, false),
|
2018-11-06 09:32:53 +00:00
|
|
|
{:ok, email} <-
|
2019-03-05 16:23:05 +00:00
|
|
|
Activation.resend_confirmation_email(user, locale) do
|
2018-11-06 09:30:27 +00:00
|
|
|
{:ok, email}
|
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
|
|
|
{:error, "No user to validate with this email was found"}
|
2018-11-06 09:32:53 +00:00
|
|
|
|
2018-11-06 09:30:27 +00: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
|
2019-03-05 16:23:05 +00:00
|
|
|
with {:ok, user} <- Users.get_user_by_email(email, true),
|
2018-11-27 16:54:54 +00:00
|
|
|
{:ok, %Bamboo.Email{} = _email_html} <-
|
2019-03-05 16:23:05 +00:00
|
|
|
ResetPassword.send_password_reset_email(user, locale) do
|
2018-11-06 09:30:27 +00:00
|
|
|
{:ok, email}
|
|
|
|
else
|
|
|
|
{:error, :user_not_found} ->
|
2018-11-27 16:54:54 +00:00
|
|
|
# TODO : implement rate limits for this endpoint
|
|
|
|
{:error, "No user with this email was found"}
|
2018-11-06 09:32:53 +00:00
|
|
|
|
2018-11-06 09:30:27 +00: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} <-
|
2019-03-05 16:23:05 +00:00
|
|
|
ResetPassword.check_reset_password_token(password, token),
|
2018-11-06 09:30:27 +00:00
|
|
|
{:ok, token, _} <- MobilizonWeb.Guardian.encode_and_sign(user) do
|
2018-11-29 16:43:22 +00:00
|
|
|
{:ok, %{token: token, user: user}}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
@doc "Change an user default actor"
|
2018-11-06 09:30:27 +00:00
|
|
|
def change_default_actor(_parent, %{preferred_username: username}, %{
|
|
|
|
context: %{current_user: user}
|
|
|
|
}) do
|
2018-11-29 16:43:22 +00:00
|
|
|
with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(username),
|
|
|
|
{:user_actor, true} <-
|
2019-03-05 16:23:05 +00:00
|
|
|
{:user_actor, actor_id in Enum.map(Users.get_actors_for_user(user), & &1.id)},
|
|
|
|
%User{} = user <- Users.update_user_default_actor(user.id, actor_id) do
|
2018-11-29 16:43:22 +00:00
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
{:user_actor, _} ->
|
|
|
|
{:error, :actor_not_from_user}
|
|
|
|
|
|
|
|
_err ->
|
|
|
|
{:error, :unable_to_change_default_actor}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|