2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.User do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Handles the user-related GraphQL calls.
|
2019-01-03 13:59:59 +00:00
|
|
|
"""
|
2019-09-07 22:05:54 +00:00
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
import Mobilizon.Users.Guards
|
|
|
|
|
2020-06-15 17:41:11 +00:00
|
|
|
alias Mobilizon.{Actors, Admin, Config, Events, Users}
|
2019-03-05 16:23:05 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-02-13 14:48:12 +00:00
|
|
|
alias Mobilizon.Crypto
|
2020-06-11 17:13:21 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
2020-06-27 17:12:45 +00:00
|
|
|
alias Mobilizon.Service.Auth.Authenticator
|
2020-02-18 07:57:00 +00:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2019-09-07 22:05:54 +00:00
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.{Auth, Email}
|
2020-01-22 23:55:07 +00:00
|
|
|
|
2018-11-28 16:16:23 +00:00
|
|
|
require Logger
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-02-13 14:48:12 +00:00
|
|
|
@confirmation_token_length 30
|
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
@doc """
|
2019-10-15 19:18:03 +00:00
|
|
|
Find an user by its ID
|
2018-11-06 09:30:27 +00:00
|
|
|
"""
|
2020-06-11 17:13:21 +00:00
|
|
|
def find_user(_parent, %{id: id}, %{context: %{current_user: %User{role: role}}})
|
|
|
|
when is_moderator(role) do
|
|
|
|
with {:ok, %User{} = user} <- Users.get_user_with_actors(id) do
|
|
|
|
{:ok, user}
|
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Return current logged-in user
|
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
def get_current_user(_parent, _args, %{context: %{current_user: user}}) do
|
2018-11-06 09:30:27 +00: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 10:41:28 +00:00
|
|
|
@doc """
|
|
|
|
List instance users
|
|
|
|
"""
|
2020-06-11 17:13:21 +00:00
|
|
|
def list_users(
|
2019-03-01 10:41:28 +00:00
|
|
|
_parent,
|
2020-06-11 17:13:21 +00:00
|
|
|
%{email: email, page: page, limit: limit, sort: sort, direction: direction},
|
2020-01-26 19:34:25 +00:00
|
|
|
%{context: %{current_user: %User{role: role}}}
|
2019-03-06 17:45:26 +00:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2020-06-11 17:13:21 +00:00
|
|
|
{:ok, Users.list_users(email, page, limit, sort, direction)}
|
2019-03-01 10:41:28 +00:00
|
|
|
end
|
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
def list_users(_parent, _args, _resolution) do
|
2020-01-26 19:34:25 +00:00
|
|
|
{:error, "You need to have admin access to list users"}
|
|
|
|
end
|
2019-03-06 17:45:26 +00:00
|
|
|
|
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
|
2020-06-27 17:12:45 +00:00
|
|
|
case Authenticator.authenticate(email, password) do
|
|
|
|
{:ok,
|
|
|
|
%{access_token: _access_token, refresh_token: _refresh_token, user: _user} =
|
|
|
|
user_and_tokens} ->
|
|
|
|
{:ok, user_and_tokens}
|
2019-10-15 16:13:05 +00:00
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
{:error, :user_not_found} ->
|
2019-10-15 16:13:05 +00:00
|
|
|
{:error, "No user with this email was found"}
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-07-06 15:33:40 +00:00
|
|
|
{:error, :disabled_user} ->
|
|
|
|
{:error, "This user has been disabled"}
|
|
|
|
|
2020-06-27 17:12:45 +00:00
|
|
|
{:error, _error} ->
|
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
|
|
|
|
|
2019-08-12 14:04:16 +00:00
|
|
|
@doc """
|
|
|
|
Refresh a token
|
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
def refresh_token(_parent, %{refresh_token: refresh_token}, _context) do
|
2020-01-23 20:59:50 +00:00
|
|
|
with {:ok, user, _claims} <- Auth.Guardian.resource_from_token(refresh_token),
|
2019-08-12 15:41:41 +00:00
|
|
|
{:ok, _old, {exchanged_token, _claims}} <-
|
2020-01-23 20:59:50 +00:00
|
|
|
Auth.Guardian.exchange(refresh_token, ["access", "refresh"], "access"),
|
2020-06-27 17:12:45 +00:00
|
|
|
{:ok, refresh_token} <- Authenticator.generate_refresh_token(user) do
|
2019-08-12 14:04:16 +00:00
|
|
|
{: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
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
def refresh_token(_parent, _params, _context) do
|
|
|
|
{:error, "You need to have an existing token to get a refresh token"}
|
|
|
|
end
|
2019-08-12 14:04:16 +00:00
|
|
|
|
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
|
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
@spec create_user(any, map, any) :: tuple
|
2019-01-25 12:59:58 +00:00
|
|
|
def create_user(_parent, args, _resolution) do
|
2019-12-17 11:09:24 +00:00
|
|
|
with :registration_ok <- check_registration_config(args),
|
2019-03-22 09:53:38 +00:00
|
|
|
{:ok, %User{} = user} <- Users.register(args) do
|
2020-01-22 23:55:07 +00:00
|
|
|
Email.User.send_confirmation_email(user, Map.get(args, :locale, "en"))
|
2018-11-29 16:43:22 +00:00
|
|
|
{:ok, user}
|
2019-03-22 09:53:38 +00:00
|
|
|
else
|
2019-12-17 11:09:24 +00:00
|
|
|
:registration_closed ->
|
2019-03-22 09:53:38 +00:00
|
|
|
{:error, "Registrations are not enabled"}
|
|
|
|
|
2019-12-17 11:09:24 +00:00
|
|
|
:not_whitelisted ->
|
|
|
|
{:error, "Your email is not on the whitelist"}
|
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
error ->
|
|
|
|
error
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
@spec check_registration_config(map) :: atom
|
2019-12-17 11:09:24 +00:00
|
|
|
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 09:30:27 +00:00
|
|
|
@doc """
|
2019-10-15 19:18:03 +00:00
|
|
|
Validate an user, get its actor and a token
|
2018-11-06 09:30:27 +00:00
|
|
|
"""
|
|
|
|
def validate_user(_parent, %{token: token}, _resolution) do
|
2018-11-28 16:16:23 +00:00
|
|
|
with {:check_confirmation_token, {:ok, %User{} = user}} <-
|
2020-01-22 23:55:07 +00:00
|
|
|
{:check_confirmation_token, Email.User.check_confirmation_token(token)},
|
2019-03-05 16:23:05 +00:00
|
|
|
{:get_actor, actor} <- {:get_actor, Users.get_actor_for_user(user)},
|
2019-08-12 15:41:41 +00:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token}} <-
|
2020-06-27 17:12:45 +00:00
|
|
|
Authenticator.generate_tokens(user) do
|
2019-08-12 15:41:41 +00:00
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
access_token: access_token,
|
|
|
|
refresh_token: refresh_token,
|
|
|
|
user: Map.put(user, :default_actor, actor)
|
|
|
|
}}
|
2018-11-28 16:16:23 +00:00
|
|
|
else
|
2019-09-07 17:54:11 +00:00
|
|
|
error ->
|
2018-11-28 16:16:23 +00:00
|
|
|
Logger.info("Unable to validate user with token #{token}")
|
2019-09-07 17:54:11 +00:00
|
|
|
Logger.debug(inspect(error))
|
2020-01-26 19:34:25 +00:00
|
|
|
|
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
|
|
|
|
"""
|
2019-10-01 11:08:09 +00: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 09:32:53 +00:00
|
|
|
{:ok, email} <-
|
2020-01-22 23:55:07 +00:00
|
|
|
Email.User.resend_confirmation_email(user, Map.get(args, :locale, 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
|
|
|
|
"""
|
2019-10-01 11:08:09 +00: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),
|
2020-06-27 17:12:45 +00:00
|
|
|
{:can_reset_password, true} <-
|
|
|
|
{:can_reset_password, Authenticator.can_reset_password?(user)},
|
2018-11-27 16:54:54 +00:00
|
|
|
{:ok, %Bamboo.Email{} = _email_html} <-
|
2020-01-22 23:55:07 +00:00
|
|
|
Email.User.send_password_reset_email(user, Map.get(args, :locale, locale)) do
|
2018-11-06 09:30:27 +00:00
|
|
|
{:ok, email}
|
|
|
|
else
|
2020-06-27 17:12:45 +00:00
|
|
|
{:can_reset_password, false} ->
|
|
|
|
{:error, "This user can't reset their password"}
|
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
{: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
|
2020-06-27 17:12:45 +00:00
|
|
|
with {:ok, %User{email: email} = user} <-
|
2020-01-22 23:55:07 +00:00
|
|
|
Email.User.check_reset_password_token(password, token),
|
2019-08-12 15:41:41 +00:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token}} <-
|
2020-06-27 17:12:45 +00:00
|
|
|
Authenticator.authenticate(email, password) do
|
2019-08-12 14:04:16 +00:00
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_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"
|
2019-08-12 14:04:16 +00:00
|
|
|
def change_default_actor(
|
|
|
|
_parent,
|
|
|
|
%{preferred_username: username},
|
2020-01-22 23:55:07 +00:00
|
|
|
%{context: %{current_user: user}}
|
2019-08-12 14:04:16 +00:00
|
|
|
) 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}
|
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
_error ->
|
2018-11-29 16:43:22 +00:00
|
|
|
{:error, :unable_to_change_default_actor}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-18 15:32:37 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of events for all of this user's identities are going to
|
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
def user_participations(
|
|
|
|
%User{id: user_id},
|
|
|
|
args,
|
2020-06-11 17:13:21 +00:00
|
|
|
%{context: %{current_user: %User{id: logged_user_id, role: role}}}
|
2020-01-26 19:34:25 +00:00
|
|
|
) do
|
2020-06-11 17:13:21 +00:00
|
|
|
with true <- user_id == logged_user_id or is_moderator(role),
|
2020-02-18 07:57:00 +00:00
|
|
|
%Page{} = page <-
|
2019-09-18 15:32:37 +00: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
|
2020-02-18 07:57:00 +00:00
|
|
|
{:ok, page}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of groups this user is a member is a member of
|
|
|
|
"""
|
|
|
|
def user_memberships(
|
|
|
|
%User{id: user_id},
|
|
|
|
%{page: page, limit: limit} = _args,
|
|
|
|
%{context: %{current_user: %User{id: logged_user_id}}}
|
|
|
|
) do
|
|
|
|
with true <- user_id == logged_user_id,
|
|
|
|
memberships <-
|
|
|
|
Actors.list_memberships_for_user(
|
|
|
|
user_id,
|
|
|
|
page,
|
|
|
|
limit
|
|
|
|
) do
|
|
|
|
{:ok, memberships}
|
2019-09-18 15:32:37 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-24 16:08:33 +00:00
|
|
|
|
2019-10-02 15:59:07 +00:00
|
|
|
@doc """
|
|
|
|
Returns the list of draft events for the current user
|
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
def user_drafted_events(
|
|
|
|
%User{id: user_id},
|
|
|
|
args,
|
|
|
|
%{context: %{current_user: %User{id: logged_user_id}}}
|
|
|
|
) do
|
2019-10-02 15:59:07 +00:00
|
|
|
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
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
def change_password(
|
|
|
|
_parent,
|
|
|
|
%{old_password: old_password, new_password: new_password},
|
2020-06-27 17:12:45 +00:00
|
|
|
%{context: %{current_user: %User{} = user}}
|
2020-01-26 19:34:25 +00:00
|
|
|
) do
|
2020-06-27 17:12:45 +00:00
|
|
|
with {:can_change_password, true} <-
|
|
|
|
{:can_change_password, Authenticator.can_change_password?(user)},
|
|
|
|
{:current_password, {:ok, %User{}}} <-
|
|
|
|
{:current_password, Authenticator.login(user.email, old_password)},
|
2019-09-24 16:08:33 +00:00
|
|
|
{:same_password, false} <- {:same_password, old_password == new_password},
|
|
|
|
{:ok, %User{} = user} <-
|
|
|
|
user
|
2020-01-26 19:34:25 +00:00
|
|
|
|> User.password_change_changeset(%{"password" => new_password})
|
2019-09-24 16:08:33 +00:00
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, user}
|
|
|
|
else
|
2020-06-27 17:12:45 +00:00
|
|
|
{:current_password, _} ->
|
2019-09-24 16:08:33 +00:00
|
|
|
{: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
|
2020-02-13 14:48:12 +00:00
|
|
|
|
|
|
|
def change_email(_parent, %{email: new_email, password: password}, %{
|
2020-06-27 17:12:45 +00:00
|
|
|
context: %{current_user: %User{email: old_email} = user}
|
2020-02-13 14:48:12 +00:00
|
|
|
}) do
|
2020-06-27 17:12:45 +00:00
|
|
|
with {:can_change_password, true} <-
|
|
|
|
{:can_change_password, Authenticator.can_change_email?(user)},
|
|
|
|
{:current_password, {:ok, %User{}}} <-
|
|
|
|
{:current_password, Authenticator.login(user.email, password)},
|
2020-02-13 14:48:12 +00:00
|
|
|
{:same_email, false} <- {:same_email, new_email == old_email},
|
|
|
|
{:email_valid, true} <- {:email_valid, Email.Checker.valid?(new_email)},
|
|
|
|
{:ok, %User{} = user} <-
|
|
|
|
user
|
|
|
|
|> User.changeset(%{
|
|
|
|
unconfirmed_email: new_email,
|
|
|
|
confirmation_token: Crypto.random_string(@confirmation_token_length),
|
|
|
|
confirmation_sent_at: DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
})
|
|
|
|
|> Repo.update() do
|
|
|
|
user
|
|
|
|
|> Email.User.send_email_reset_old_email()
|
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
|
|
|
|
user
|
|
|
|
|> Email.User.send_email_reset_new_email()
|
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
|
|
|
|
{:ok, user}
|
|
|
|
else
|
2020-06-27 17:12:45 +00:00
|
|
|
{:current_password, _} ->
|
2020-02-13 14:48:12 +00:00
|
|
|
{:error, "The password provided is invalid"}
|
|
|
|
|
|
|
|
{:same_email, true} ->
|
|
|
|
{:error, "The new email must be different"}
|
|
|
|
|
|
|
|
{:email_valid, _} ->
|
|
|
|
{:error, "The new email doesn't seem to be valid"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_email(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to change your email"}
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_email(_parent, %{token: token}, _resolution) do
|
|
|
|
with %User{} = user <- Users.get_user_by_activation_token(token),
|
|
|
|
{:ok, %User{} = user} <-
|
|
|
|
user
|
|
|
|
|> User.changeset(%{
|
|
|
|
email: user.unconfirmed_email,
|
|
|
|
unconfirmed_email: nil,
|
|
|
|
confirmation_token: nil,
|
|
|
|
confirmation_sent_at: nil
|
|
|
|
})
|
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-18 15:21:58 +00:00
|
|
|
def delete_account(_parent, %{user_id: user_id}, %{
|
|
|
|
context: %{current_user: %User{role: role} = moderator_user}
|
|
|
|
})
|
|
|
|
when is_moderator(role) do
|
|
|
|
with {:moderator_actor, %Actor{} = moderator_actor} <-
|
|
|
|
{:moderator_actor, Users.get_actor_for_user(moderator_user)},
|
|
|
|
%User{disabled: false} = user <- Users.get_user(user_id),
|
|
|
|
{:ok, %User{}} <-
|
|
|
|
do_delete_account(%User{} = user, Relay.get_actor()) do
|
|
|
|
Admin.log_action(moderator_actor, "delete", user)
|
|
|
|
else
|
|
|
|
{:moderator_actor, nil} ->
|
|
|
|
{:error, "No actor found for the moderator user"}
|
|
|
|
|
|
|
|
%User{disabled: true} ->
|
|
|
|
{:error, "User already disabled"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-27 17:12:45 +00:00
|
|
|
def delete_account(_parent, args, %{
|
|
|
|
context: %{current_user: %User{email: email} = user}
|
2020-02-13 14:48:12 +00:00
|
|
|
}) do
|
2020-06-27 17:12:45 +00:00
|
|
|
with {:user_has_password, true} <- {:user_has_password, Authenticator.has_password?(user)},
|
|
|
|
{:confirmation_password, password} when not is_nil(password) <-
|
|
|
|
{:confirmation_password, Map.get(args, :password)},
|
|
|
|
{:current_password, {:ok, _}} <-
|
|
|
|
{:current_password, Authenticator.authenticate(email, password)} do
|
|
|
|
do_delete_account(user)
|
|
|
|
else
|
|
|
|
# If the user hasn't got any password (3rd-party auth)
|
|
|
|
{:user_has_password, false} ->
|
2020-06-11 17:13:21 +00:00
|
|
|
do_delete_account(user)
|
|
|
|
|
2020-06-27 17:12:45 +00:00
|
|
|
{:confirmation_password, nil} ->
|
|
|
|
{:error, "The password provided is invalid"}
|
|
|
|
|
|
|
|
{:current_password, _} ->
|
2020-02-13 14:48:12 +00:00
|
|
|
{:error, "The password provided is invalid"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_account(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to delete your account"}
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp do_delete_account(%User{} = user, actor_performing \\ nil) do
|
2020-06-11 17:13:21 +00:00
|
|
|
with actors <- Users.get_actors_for_user(user),
|
|
|
|
activated <- not is_nil(user.confirmed_at),
|
|
|
|
# Detach actors from user
|
|
|
|
:ok <-
|
|
|
|
if(activated,
|
|
|
|
do: :ok,
|
|
|
|
else: Enum.each(actors, fn actor -> Actors.update_actor(actor, %{user_id: nil}) end)
|
|
|
|
),
|
|
|
|
# Launch a background job to delete actors
|
|
|
|
:ok <-
|
|
|
|
Enum.each(actors, fn actor ->
|
2020-07-09 15:24:28 +00:00
|
|
|
actor_performing = actor_performing || actor
|
|
|
|
ActivityPub.delete(actor, actor_performing, true)
|
2020-06-11 17:13:21 +00:00
|
|
|
end),
|
|
|
|
# Delete user
|
|
|
|
{:ok, user} <- Users.delete_user(user, reserve_email: activated) do
|
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@spec user_settings(User.t(), map(), map()) :: {:ok, list(Setting.t())} | {:error, String.t()}
|
2020-06-11 17:13:21 +00:00
|
|
|
def user_settings(%User{} = user, _args, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when is_moderator(role) do
|
|
|
|
with {:setting, settings} <- {:setting, Users.get_setting(user)} do
|
|
|
|
{:ok, settings}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def user_settings(%User{id: user_id} = user, _args, %{
|
|
|
|
context: %{current_user: %User{id: logged_user_id}}
|
|
|
|
}) do
|
|
|
|
with {:same_user, true} <- {:same_user, user_id == logged_user_id},
|
|
|
|
{:setting, settings} <- {:setting, Users.get_setting(user)} do
|
|
|
|
{:ok, settings}
|
|
|
|
else
|
|
|
|
{:same_user, _} ->
|
|
|
|
{:error, "User requested is not logged-in"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec set_user_setting(map(), map(), map()) :: {:ok, Setting.t()} | {:error, any()}
|
|
|
|
def set_user_setting(_parent, attrs, %{
|
|
|
|
context: %{current_user: %User{id: logged_user_id}}
|
|
|
|
}) do
|
|
|
|
attrs = Map.put(attrs, :user_id, logged_user_id)
|
|
|
|
|
|
|
|
res =
|
|
|
|
case Users.get_setting(logged_user_id) do
|
|
|
|
nil ->
|
|
|
|
Users.create_setting(attrs)
|
|
|
|
|
|
|
|
%Setting{} = setting ->
|
|
|
|
Users.update_setting(setting, attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
case res do
|
|
|
|
{:ok, %Setting{} = setting} ->
|
|
|
|
{:ok, setting}
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
Logger.debug(inspect(changeset))
|
|
|
|
{:error, "Error while saving user setting"}
|
|
|
|
end
|
|
|
|
end
|
2020-06-16 16:00:27 +00:00
|
|
|
|
|
|
|
def update_locale(_parent, %{locale: locale}, %{
|
2020-06-16 16:02:53 +00:00
|
|
|
context: %{current_user: %User{locale: current_locale} = user}
|
2020-06-16 16:00:27 +00:00
|
|
|
}) do
|
2020-06-16 16:02:53 +00:00
|
|
|
with true <- current_locale != locale,
|
2020-06-16 16:00:27 +00:00
|
|
|
{:ok, %User{} = updated_user} <- Users.update_user(user, %{locale: locale}) do
|
|
|
|
{:ok, updated_user}
|
2020-06-17 13:54:24 +00:00
|
|
|
else
|
|
|
|
false ->
|
|
|
|
{:ok, user}
|
2020-06-16 16:00:27 +00:00
|
|
|
end
|
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|