2018-11-06 10:30:27 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.User do
|
2019-01-03 14:59:59 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Handles the user-related GraphQL calls.
|
2019-01-03 14:59:59 +01:00
|
|
|
"""
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
import Mobilizon.Users.Guards
|
|
|
|
|
2019-09-18 17:32:37 +02:00
|
|
|
alias Mobilizon.{Actors, Config, Users, Events}
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-24 18:08:33 +02:00
|
|
|
alias Mobilizon.Storage.Repo
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Users.User
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
alias MobilizonWeb.Email
|
|
|
|
|
2018-11-28 17:16:23 +01:00
|
|
|
require Logger
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
@doc """
|
2019-10-15 21:18:03 +02:00
|
|
|
Find an user by its ID
|
2018-11-06 10:30:27 +01:00
|
|
|
"""
|
|
|
|
def find_user(_parent, %{id: id}, _resolution) do
|
2019-03-05 17:23:05 +01:00
|
|
|
Users.get_user_with_actors(id)
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Return current logged-in user
|
|
|
|
"""
|
2019-08-12 16:04:16 +02:00
|
|
|
def get_current_user(
|
|
|
|
_parent,
|
|
|
|
_args,
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2018-11-06 10:30:27 +01:00
|
|
|
{: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 11:41:28 +01:00
|
|
|
@doc """
|
|
|
|
List instance users
|
|
|
|
"""
|
|
|
|
def list_and_count_users(
|
|
|
|
_parent,
|
|
|
|
%{page: page, limit: limit, sort: sort, direction: direction},
|
2019-03-06 18:45:26 +01:00
|
|
|
%{
|
2019-08-12 16:04:16 +02:00
|
|
|
context: %{
|
|
|
|
current_user: %User{
|
|
|
|
role: role
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 18:45:26 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2019-03-05 17:23:05 +01:00
|
|
|
total = Task.async(&Users.count_users/0)
|
|
|
|
elements = Task.async(fn -> Users.list_users(page, limit, sort, direction) end)
|
2019-03-01 11:41:28 +01:00
|
|
|
|
|
|
|
{:ok, %{total: Task.await(total), elements: Task.await(elements)}}
|
|
|
|
end
|
|
|
|
|
2019-03-06 18:45:26 +01:00
|
|
|
def list_and_count_users(_parent, _args, _resolution),
|
|
|
|
do: {:error, "You need to have admin access to list users"}
|
|
|
|
|
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
|
2019-10-15 18:13:05 +02:00
|
|
|
with {:ok, %User{confirmed_at: %DateTime{}} = user} <- Users.get_user_by_email(email),
|
2019-08-12 17:41:41 +02:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token}} <-
|
|
|
|
Users.authenticate(%{user: user, password: password}) do
|
2019-08-12 16:04:16 +02:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token, user: user}}
|
2018-11-06 10:30:27 +01:00
|
|
|
else
|
2019-10-15 18:13:05 +02:00
|
|
|
{:ok, %User{confirmed_at: nil} = _user} ->
|
|
|
|
{:error, "User account not confirmed"}
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
{:error, :user_not_found} ->
|
2019-10-15 18:13:05 +02:00
|
|
|
{:error, "No user with this email was found"}
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
{:error, :unauthorized} ->
|
2019-01-21 15:08:22 +01:00
|
|
|
{:error, "Impossible to authenticate, either your email or password are invalid."}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-12 16:04:16 +02:00
|
|
|
@doc """
|
|
|
|
Refresh a token
|
|
|
|
"""
|
|
|
|
def refresh_token(
|
|
|
|
_parent,
|
|
|
|
%{
|
|
|
|
refresh_token: refresh_token
|
|
|
|
},
|
|
|
|
_context
|
|
|
|
) do
|
2019-08-12 17:41:41 +02:00
|
|
|
with {:ok, user, _claims} <- MobilizonWeb.Guardian.resource_from_token(refresh_token),
|
|
|
|
{:ok, _old, {exchanged_token, _claims}} <-
|
2019-08-13 08:43:37 +02:00
|
|
|
MobilizonWeb.Guardian.exchange(refresh_token, ["access", "refresh"], "access"),
|
2019-08-12 16:04:16 +02:00
|
|
|
{:ok, refresh_token} <- Users.generate_refresh_token(user) do
|
|
|
|
{:ok, %{access_token: exchanged_token, refresh_token: refresh_token}}
|
|
|
|
else
|
|
|
|
{:error, message} ->
|
|
|
|
Logger.debug("Cannot refresh user token: #{inspect(message)}")
|
|
|
|
{:error, "Cannot refresh the token"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_token(_parent, _params, _context),
|
|
|
|
do: {:error, "You need to have an existing token to get a refresh token"}
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc """
|
2019-03-22 10:53:38 +01:00
|
|
|
Register an user:
|
|
|
|
- check registrations are enabled
|
2018-11-06 10:30:27 +01:00
|
|
|
- create the user
|
|
|
|
- send a validation email to the user
|
|
|
|
"""
|
2019-01-25 13:59:58 +01:00
|
|
|
@spec create_user(any(), map(), any()) :: tuple()
|
|
|
|
def create_user(_parent, args, _resolution) do
|
2019-12-17 12:09:24 +01:00
|
|
|
with :registration_ok <- check_registration_config(args),
|
2019-03-22 10:53:38 +01:00
|
|
|
{:ok, %User{} = user} <- Users.register(args) do
|
2020-01-23 00:55:07 +01:00
|
|
|
Email.User.send_confirmation_email(user, Map.get(args, :locale, "en"))
|
2018-11-29 17:43:22 +01:00
|
|
|
{:ok, user}
|
2019-03-22 10:53:38 +01:00
|
|
|
else
|
2019-12-17 12:09:24 +01:00
|
|
|
:registration_closed ->
|
2019-03-22 10:53:38 +01:00
|
|
|
{:error, "Registrations are not enabled"}
|
|
|
|
|
2019-12-17 12:09:24 +01:00
|
|
|
:not_whitelisted ->
|
|
|
|
{:error, "Your email is not on the whitelist"}
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
error ->
|
|
|
|
error
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-17 12:09:24 +01:00
|
|
|
@spec check_registration_config(map()) :: atom()
|
|
|
|
defp check_registration_config(%{email: email}) do
|
|
|
|
cond do
|
|
|
|
Config.instance_registrations_open?() ->
|
|
|
|
:registration_ok
|
|
|
|
|
|
|
|
Config.instance_registrations_whitelist?() ->
|
|
|
|
check_white_listed_email?(email)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
:registration_closed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec check_white_listed_email?(String.t()) :: :registration_ok | :not_whitelisted
|
|
|
|
defp check_white_listed_email?(email) do
|
|
|
|
[_, domain] = String.split(email, "@", parts: 2, trim: true)
|
|
|
|
|
|
|
|
if domain in Config.instance_registrations_whitelist() or
|
|
|
|
email in Config.instance_registrations_whitelist(),
|
|
|
|
do: :registration_ok,
|
|
|
|
else: :not_whitelisted
|
|
|
|
end
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
@doc """
|
2019-10-15 21:18:03 +02:00
|
|
|
Validate an user, get its actor and a token
|
2018-11-06 10:30:27 +01:00
|
|
|
"""
|
|
|
|
def validate_user(_parent, %{token: token}, _resolution) do
|
2018-11-28 17:16:23 +01:00
|
|
|
with {:check_confirmation_token, {:ok, %User{} = user}} <-
|
2020-01-23 00:55:07 +01:00
|
|
|
{:check_confirmation_token, Email.User.check_confirmation_token(token)},
|
2019-03-05 17:23:05 +01:00
|
|
|
{:get_actor, actor} <- {:get_actor, Users.get_actor_for_user(user)},
|
2019-08-12 17:41:41 +02:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token}} <-
|
|
|
|
Users.generate_tokens(user) do
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
access_token: access_token,
|
|
|
|
refresh_token: refresh_token,
|
|
|
|
user: Map.put(user, :default_actor, actor)
|
|
|
|
}}
|
2018-11-28 17:16:23 +01:00
|
|
|
else
|
2019-09-07 19:54:11 +02:00
|
|
|
error ->
|
2018-11-28 17:16:23 +01:00
|
|
|
Logger.info("Unable to validate user with token #{token}")
|
2019-09-07 19:54:11 +02:00
|
|
|
Logger.debug(inspect(error))
|
2019-01-25 13:59:58 +01:00
|
|
|
{:error, "Unable to validate user"}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send the confirmation email again.
|
|
|
|
We only do this to accounts unconfirmed
|
|
|
|
"""
|
2019-10-01 13:08:09 +02:00
|
|
|
def resend_confirmation_email(_parent, args, _resolution) do
|
|
|
|
with {:ok, %User{locale: locale} = user} <-
|
|
|
|
Users.get_user_by_email(Map.get(args, :email), false),
|
2018-11-06 10:32:53 +01:00
|
|
|
{:ok, email} <-
|
2020-01-23 00:55:07 +01:00
|
|
|
Email.User.resend_confirmation_email(user, Map.get(args, :locale, 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
|
|
|
|
"""
|
2019-10-01 13:08:09 +02:00
|
|
|
def send_reset_password(_parent, args, _resolution) do
|
|
|
|
with email <- Map.get(args, :email),
|
|
|
|
{:ok, %User{locale: locale} = user} <- Users.get_user_by_email(email, true),
|
2018-11-27 17:54:54 +01:00
|
|
|
{:ok, %Bamboo.Email{} = _email_html} <-
|
2020-01-23 00:55:07 +01:00
|
|
|
Email.User.send_password_reset_email(user, Map.get(args, :locale, 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} <-
|
2020-01-23 00:55:07 +01:00
|
|
|
Email.User.check_reset_password_token(password, token),
|
2019-08-12 17:41:41 +02:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token}} <-
|
|
|
|
Users.authenticate(%{user: user, password: password}) do
|
2019-08-12 16:04:16 +02:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token, user: user}}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
@doc "Change an user default actor"
|
2019-08-12 16:04:16 +02:00
|
|
|
def change_default_actor(
|
|
|
|
_parent,
|
|
|
|
%{preferred_username: username},
|
2020-01-23 00:55:07 +01:00
|
|
|
%{context: %{current_user: user}}
|
2019-08-12 16:04:16 +02:00
|
|
|
) do
|
2018-11-29 17:43:22 +01:00
|
|
|
with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(username),
|
|
|
|
{:user_actor, true} <-
|
2019-03-05 17:23:05 +01: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 17:43:22 +01:00
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
{:user_actor, _} ->
|
|
|
|
{:error, :actor_not_from_user}
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
_error ->
|
2018-11-29 17:43:22 +01:00
|
|
|
{:error, :unable_to_change_default_actor}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
2019-09-18 17:32:37 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of events for all of this user's identities are going to
|
|
|
|
"""
|
2019-09-26 16:38:58 +02:00
|
|
|
def user_participations(%User{id: user_id}, args, %{
|
|
|
|
context: %{current_user: %User{id: logged_user_id}}
|
2019-09-18 17:32:37 +02:00
|
|
|
}) do
|
2019-09-26 16:38:58 +02:00
|
|
|
with true <- user_id == logged_user_id,
|
|
|
|
participations <-
|
2019-09-18 17:32:37 +02:00
|
|
|
Events.list_participations_for_user(
|
|
|
|
user_id,
|
|
|
|
Map.get(args, :after_datetime),
|
|
|
|
Map.get(args, :before_datetime),
|
|
|
|
Map.get(args, :page),
|
|
|
|
Map.get(args, :limit)
|
|
|
|
) do
|
|
|
|
{:ok, participations}
|
|
|
|
end
|
|
|
|
end
|
2019-09-24 18:08:33 +02:00
|
|
|
|
2019-10-02 17:59:07 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of draft events for the current user
|
|
|
|
"""
|
|
|
|
def user_drafted_events(%User{id: user_id}, args, %{
|
|
|
|
context: %{current_user: %User{id: logged_user_id}}
|
|
|
|
}) do
|
|
|
|
with {:same_user, true} <- {:same_user, user_id == logged_user_id},
|
|
|
|
events <-
|
|
|
|
Events.list_drafts_for_user(user_id, Map.get(args, :page), Map.get(args, :limit)) do
|
|
|
|
{:ok, events}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-24 18:08:33 +02:00
|
|
|
def change_password(_parent, %{old_password: old_password, new_password: new_password}, %{
|
|
|
|
context: %{current_user: %User{password_hash: old_password_hash} = user}
|
|
|
|
}) do
|
|
|
|
with {:current_password, true} <-
|
|
|
|
{:current_password, Argon2.verify_pass(old_password, old_password_hash)},
|
|
|
|
{:same_password, false} <- {:same_password, old_password == new_password},
|
|
|
|
{:ok, %User{} = user} <-
|
|
|
|
user
|
|
|
|
|> User.password_change_changeset(%{
|
|
|
|
"password" => new_password
|
|
|
|
})
|
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
{:current_password, false} ->
|
|
|
|
{:error, "The current password is invalid"}
|
|
|
|
|
|
|
|
{:same_password, true} ->
|
|
|
|
{:error, "The new password must be different"}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{errors: [password: {"registration.error.password_too_short", _}]}} ->
|
|
|
|
{:error,
|
|
|
|
"The password you have chosen is too short. Please make sure your password contains at least 6 characters."}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_password(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to change your password"}
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|