2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.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
|
|
|
|
|
2020-06-15 19:41:11 +02:00
|
|
|
alias Mobilizon.{Actors, Admin, Config, Events, Users}
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Relay}
|
2020-06-27 19:12:45 +02:00
|
|
|
alias Mobilizon.Service.Auth.Authenticator
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.{Auth, Email}
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-01-23 00:55:07 +01:00
|
|
|
|
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
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec find_user(any(), map(), Absinthe.Resolution.t()) :: {:ok, User.t()} | {:error, String.t()}
|
2020-06-11 19:13:21 +02: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 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Return current logged-in user
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_current_user(any, map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:error, :unauthenticated} | {:ok, Mobilizon.Users.User.t()}
|
2021-09-10 11:35:32 +02:00
|
|
|
def get_current_user(_parent, _args, %{context: %{current_user: %User{} = user}}) do
|
2018-11-06 10:30:27 +01:00
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_current_user(_parent, _args, _resolution) do
|
2020-11-30 10:24:31 +01:00
|
|
|
{:error, :unauthenticated}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2019-03-01 11:41:28 +01:00
|
|
|
@doc """
|
|
|
|
List instance users
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec list_users(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(User.t())} | {:error, :unauthorized}
|
2020-06-11 19:13:21 +02:00
|
|
|
def list_users(
|
2019-03-01 11:41:28 +01:00
|
|
|
_parent,
|
2020-06-11 19:13:21 +02:00
|
|
|
%{email: email, page: page, limit: limit, sort: sort, direction: direction},
|
2020-01-26 20:34:25 +01:00
|
|
|
%{context: %{current_user: %User{role: role}}}
|
2019-03-06 18:45:26 +01:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2020-06-11 19:13:21 +02:00
|
|
|
{:ok, Users.list_users(email, page, limit, sort, direction)}
|
2019-03-01 11:41:28 +01:00
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
def list_users(_parent, _args, _resolution) do
|
2020-11-30 10:24:31 +01:00
|
|
|
{:error, :unauthorized}
|
2020-01-26 20:34:25 +01:00
|
|
|
end
|
2019-03-06 18:45:26 +01:00
|
|
|
|
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
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec login_user(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, map()} | {:error, :user_not_found | String.t()}
|
2020-08-28 09:23:49 +02:00
|
|
|
def login_user(_parent, %{email: email, password: password}, %{context: context}) do
|
|
|
|
with {:ok,
|
|
|
|
%{
|
|
|
|
access_token: _access_token,
|
|
|
|
refresh_token: _refresh_token,
|
|
|
|
user: %User{} = user
|
|
|
|
} = user_and_tokens} <- Authenticator.authenticate(email, password),
|
2021-09-10 11:27:59 +02:00
|
|
|
{:ok, %User{} = user} <- update_user_login_information(user, context) do
|
|
|
|
{:ok, %{user_and_tokens | user: user}}
|
2020-08-28 09:23:49 +02:00
|
|
|
else
|
2018-11-06 10:30:27 +01:00
|
|
|
{:error, :user_not_found} ->
|
2020-11-30 10:24:31 +01:00
|
|
|
{:error, :user_not_found}
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2020-07-06 17:33:40 +02:00
|
|
|
{:error, :disabled_user} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "This user has been disabled")}
|
2020-07-06 17:33:40 +02:00
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
{:error, _error} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"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
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec refresh_token(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, map()} | {:error, String.t()}
|
2021-05-31 12:08:06 +02:00
|
|
|
def refresh_token(_parent, %{refresh_token: refresh_token}, _resolution) do
|
2020-01-23 21:59:50 +01:00
|
|
|
with {:ok, user, _claims} <- Auth.Guardian.resource_from_token(refresh_token),
|
2019-08-12 17:41:41 +02:00
|
|
|
{:ok, _old, {exchanged_token, _claims}} <-
|
2021-05-31 12:08:06 +02:00
|
|
|
Auth.Guardian.exchange(refresh_token, "refresh", "access"),
|
|
|
|
{:ok, new_refresh_token} <- Authenticator.generate_refresh_token(user),
|
|
|
|
{:ok, _claims} <- Auth.Guardian.revoke(refresh_token) do
|
|
|
|
{:ok, %{access_token: exchanged_token, refresh_token: new_refresh_token}}
|
2019-08-12 16:04:16 +02:00
|
|
|
else
|
|
|
|
{:error, message} ->
|
|
|
|
Logger.debug("Cannot refresh user token: #{inspect(message)}")
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Cannot refresh the token")}
|
2019-08-12 16:04:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def refresh_token(_parent, _params, _context) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to have an existing token to get a refresh token")}
|
2020-01-26 20:34:25 +01:00
|
|
|
end
|
2019-08-12 16:04:16 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec logout(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, String.t()}
|
|
|
|
| {:error, :token_not_found | :unable_to_logout | :unauthenticated | :invalid_argument}
|
2021-05-25 11:00:46 +02:00
|
|
|
def logout(_parent, %{refresh_token: refresh_token}, %{context: %{current_user: %User{}}}) do
|
|
|
|
with {:ok, _claims} <- Auth.Guardian.decode_and_verify(refresh_token, %{"typ" => "refresh"}),
|
|
|
|
{:ok, _claims} <- Auth.Guardian.revoke(refresh_token) do
|
|
|
|
{:ok, refresh_token}
|
|
|
|
else
|
|
|
|
{:error, :token_not_found} ->
|
|
|
|
{:error, :token_not_found}
|
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
Logger.debug("Cannot remove user refresh token: #{inspect(error)}")
|
|
|
|
{:error, :unable_to_logout}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout(_parent, %{refresh_token: _refresh_token}, _context) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout(_parent, _params, _context) do
|
|
|
|
{:error, :invalid_argument}
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_user(any, %{email: String.t()}, any) :: {:ok, User.t()} | {:error, String.t()}
|
2021-08-08 19:46:39 +02:00
|
|
|
def create_user(_parent, %{email: email} = args, _resolution) do
|
|
|
|
with :registration_ok <- check_registration_config(email),
|
|
|
|
:not_deny_listed <- check_registration_denylist(email),
|
2021-03-09 11:56:09 +01:00
|
|
|
{:ok, %User{} = user} <- Users.register(args),
|
2021-04-20 15:27:33 +02:00
|
|
|
%Bamboo.Email{} <-
|
2021-03-09 11:56:09 +01:00
|
|
|
Email.User.send_confirmation_email(user, Map.get(args, :locale, "en")) do
|
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 ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Registrations are not open")}
|
2019-03-22 10:53:38 +01:00
|
|
|
|
2020-09-29 09:53:48 +02:00
|
|
|
:not_allowlisted ->
|
|
|
|
{:error, dgettext("errors", "Your email is not on the allowlist")}
|
2019-12-17 12:09:24 +01:00
|
|
|
|
2021-08-08 19:46:39 +02:00
|
|
|
:deny_listed ->
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"Your e-mail has been denied registration or uses a disallowed e-mail provider"
|
|
|
|
)}
|
|
|
|
|
2020-10-01 15:07:15 +02:00
|
|
|
{:error, error} ->
|
|
|
|
{:error, error}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec check_registration_config(String.t()) ::
|
|
|
|
:registration_ok | :registration_closed | :not_allowlisted
|
2021-08-08 19:46:39 +02:00
|
|
|
defp check_registration_config(email) do
|
2019-12-17 12:09:24 +01:00
|
|
|
cond do
|
|
|
|
Config.instance_registrations_open?() ->
|
|
|
|
:registration_ok
|
|
|
|
|
2020-09-29 09:53:48 +02:00
|
|
|
Config.instance_registrations_allowlist?() ->
|
2021-09-10 11:27:59 +02:00
|
|
|
check_allow_listed_email(email)
|
2019-12-17 12:09:24 +01:00
|
|
|
|
|
|
|
true ->
|
|
|
|
:registration_closed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec check_registration_denylist(String.t()) :: :deny_listed | :not_deny_listed
|
2021-08-08 19:46:39 +02:00
|
|
|
defp check_registration_denylist(email) do
|
|
|
|
# Remove everything behind the +
|
|
|
|
email = String.replace(email, ~r/(\+.*)(?=\@)/, "")
|
|
|
|
|
|
|
|
if email_in_list(email, Config.instance_registrations_denylist()),
|
|
|
|
do: :deny_listed,
|
|
|
|
else: :not_deny_listed
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec check_allow_listed_email(String.t()) :: :registration_ok | :not_allowlisted
|
|
|
|
defp check_allow_listed_email(email) do
|
2021-08-08 19:46:39 +02:00
|
|
|
if email_in_list(email, Config.instance_registrations_allowlist()),
|
|
|
|
do: :registration_ok,
|
|
|
|
else: :not_allowlisted
|
|
|
|
end
|
|
|
|
|
|
|
|
defp email_in_list(email, list) do
|
2019-12-17 12:09:24 +01:00
|
|
|
[_, domain] = String.split(email, "@", parts: 2, trim: true)
|
|
|
|
|
2021-08-08 19:46:39 +02:00
|
|
|
domain in list or email in list
|
2019-12-17 12:09:24 +01:00
|
|
|
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
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec validate_user(map(), %{token: String.t()}, map()) :: {:ok, map()} | {:error, String.t()}
|
2018-11-06 10:30:27 +01:00
|
|
|
def validate_user(_parent, %{token: token}, _resolution) do
|
2021-09-10 11:35:32 +02:00
|
|
|
case Email.User.check_confirmation_token(token) do
|
|
|
|
{:ok, %User{} = user} ->
|
|
|
|
actor = Users.get_actor_for_user(user)
|
|
|
|
|
|
|
|
{:ok, %{access_token: access_token, refresh_token: refresh_token}} =
|
|
|
|
Authenticator.generate_tokens(user)
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
access_token: access_token,
|
|
|
|
refresh_token: refresh_token,
|
|
|
|
user: Map.put(user, :default_actor, actor)
|
|
|
|
}}
|
|
|
|
|
|
|
|
{:error, :invalid_token} ->
|
|
|
|
Logger.info("Invalid token #{token} to validate user")
|
|
|
|
{:error, dgettext("errors", "Unable to validate user")}
|
2020-01-26 20:34:25 +01:00
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
Logger.info("Unable to validate user with token #{token}")
|
|
|
|
Logger.debug(inspect(err))
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Unable to validate user")}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send the confirmation email again.
|
2021-03-25 10:22:40 +01:00
|
|
|
We only do this to accounts not activated
|
2018-11-06 10:30:27 +01:00
|
|
|
"""
|
2019-10-01 13:08:09 +02:00
|
|
|
def resend_confirmation_email(_parent, args, _resolution) do
|
|
|
|
with {:ok, %User{locale: locale} = user} <-
|
2021-03-25 10:22:40 +01:00
|
|
|
Users.get_user_by_email(Map.get(args, :email), activated: false, unconfirmed: 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} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "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} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You requested again a confirmation email too soon")}
|
2018-11-06 10:30:27 +01:00
|
|
|
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),
|
2021-03-25 10:22:40 +01:00
|
|
|
{:ok, %User{locale: locale} = user} <-
|
|
|
|
Users.get_user_by_email(email, activated: true, unconfirmed: false),
|
2020-06-27 19:12:45 +02:00
|
|
|
{:can_reset_password, true} <-
|
|
|
|
{:can_reset_password, Authenticator.can_reset_password?(user)},
|
2021-04-20 15:27:33 +02:00
|
|
|
{:ok, %Bamboo.Email{}} <-
|
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
|
2020-06-27 19:12:45 +02:00
|
|
|
{:can_reset_password, false} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "This user can't reset their password")}
|
2020-06-27 19:12:45 +02:00
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
{:error, :user_not_found} ->
|
2018-11-27 17:54:54 +01:00
|
|
|
# TODO : implement rate limits for this endpoint
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "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} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You requested again a confirmation email too soon")}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Reset the password from an user
|
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec reset_password(map(), %{password: String.t(), token: String.t()}, map()) ::
|
|
|
|
{:ok, map()} | {:error, String.t()}
|
2018-11-06 10:30:27 +01:00
|
|
|
def reset_password(_parent, %{password: password, token: token}, _resolution) do
|
2021-09-10 11:27:59 +02:00
|
|
|
case Email.User.check_reset_password_token(password, token) do
|
|
|
|
{:ok, %User{email: email} = user} ->
|
|
|
|
{:ok, tokens} = Authenticator.authenticate(email, password)
|
|
|
|
{:ok, Map.put(tokens, :user, user)}
|
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
{:error, %Ecto.Changeset{errors: [password: {"registration.error.password_too_short", _}]}} ->
|
|
|
|
{:error,
|
|
|
|
gettext(
|
|
|
|
"The password you have choosen is too short. Please make sure your password contains at least 6 charaters."
|
|
|
|
)}
|
|
|
|
|
|
|
|
{:error, _err} ->
|
|
|
|
{:error,
|
|
|
|
gettext(
|
|
|
|
"The token you provided is invalid. Make sure that the URL is exactly the one provided inside the email you got."
|
|
|
|
)}
|
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},
|
2021-09-10 11:35:32 +02:00
|
|
|
%{context: %{current_user: %User{id: user_id} = user}}
|
2019-08-12 16:04:16 +02:00
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with %Actor{id: actor_id} = actor <- Actors.get_local_actor_by_name(username),
|
2018-11-29 17:43:22 +01:00
|
|
|
{: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)},
|
2021-09-10 11:35:32 +02:00
|
|
|
%User{} = user <- Users.update_user_default_actor(user_id, actor) 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
|
|
|
|
2021-03-25 10:38:31 +01:00
|
|
|
def change_default_actor(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
|
|
|
|
2019-09-18 17:32:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of events for all of this user's identities are going to
|
|
|
|
"""
|
2020-01-26 20:34:25 +01:00
|
|
|
def user_participations(
|
|
|
|
%User{id: user_id},
|
|
|
|
args,
|
2020-06-11 19:13:21 +02:00
|
|
|
%{context: %{current_user: %User{id: logged_user_id, role: role}}}
|
2020-01-26 20:34:25 +01:00
|
|
|
) do
|
2020-06-11 19:13:21 +02:00
|
|
|
with true <- user_id == logged_user_id or is_moderator(role),
|
2020-02-18 08:57:00 +01:00
|
|
|
%Page{} = page <-
|
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
|
2020-02-18 08:57:00 +01: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 17:32:37 +02:00
|
|
|
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
|
|
|
|
"""
|
2020-01-26 20:34:25 +01:00
|
|
|
def user_drafted_events(
|
|
|
|
%User{id: user_id},
|
|
|
|
args,
|
|
|
|
%{context: %{current_user: %User{id: logged_user_id}}}
|
|
|
|
) do
|
2019-10-02 17:59:07 +02: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 20:34:25 +01:00
|
|
|
def change_password(
|
|
|
|
_parent,
|
|
|
|
%{old_password: old_password, new_password: new_password},
|
2020-06-27 19:12:45 +02:00
|
|
|
%{context: %{current_user: %User{} = user}}
|
2020-01-26 20:34:25 +01:00
|
|
|
) do
|
2020-06-27 19:12:45 +02: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 18:08:33 +02:00
|
|
|
{:same_password, false} <- {:same_password, old_password == new_password},
|
|
|
|
{:ok, %User{} = user} <-
|
|
|
|
user
|
2020-01-26 20:34:25 +01:00
|
|
|
|> User.password_change_changeset(%{"password" => new_password})
|
2019-09-24 18:08:33 +02:00
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, user}
|
|
|
|
else
|
2021-09-10 11:27:59 +02:00
|
|
|
{:can_change_password, false} ->
|
|
|
|
{:error, dgettext("errors", "You cannot change your password.")}
|
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
{:current_password, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "The current password is invalid")}
|
2019-09-24 18:08:33 +02:00
|
|
|
|
|
|
|
{:same_password, true} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "The new password must be different")}
|
2019-09-24 18:08:33 +02:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{errors: [password: {"registration.error.password_too_short", _}]}} ->
|
|
|
|
{:error,
|
2020-09-29 09:53:48 +02:00
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"The password you have chosen is too short. Please make sure your password contains at least 6 characters."
|
|
|
|
)}
|
2019-09-24 18:08:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_password(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to change your password")}
|
2019-09-24 18:08:33 +02:00
|
|
|
end
|
2020-02-13 15:48:12 +01:00
|
|
|
|
|
|
|
def change_email(_parent, %{email: new_email, password: password}, %{
|
2020-06-27 19:12:45 +02:00
|
|
|
context: %{current_user: %User{email: old_email} = user}
|
2020-02-13 15:48:12 +01:00
|
|
|
}) do
|
2020-06-27 19:12:45 +02: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 15:48:12 +01:00
|
|
|
{:same_email, false} <- {:same_email, new_email == old_email},
|
|
|
|
{:email_valid, true} <- {:email_valid, Email.Checker.valid?(new_email)},
|
2021-03-25 10:22:40 +01:00
|
|
|
{:ok, %User{} = user} <- Users.update_user_email(user, new_email) do
|
2020-02-13 15:48:12 +01:00
|
|
|
user
|
|
|
|
|> Email.User.send_email_reset_old_email()
|
2021-04-20 15:02:24 +02:00
|
|
|
|> Email.Mailer.send_email_later()
|
2020-02-13 15:48:12 +01:00
|
|
|
|
|
|
|
user
|
|
|
|
|> Email.User.send_email_reset_new_email()
|
2021-04-20 15:02:24 +02:00
|
|
|
|> Email.Mailer.send_email_later()
|
2020-02-13 15:48:12 +01:00
|
|
|
|
|
|
|
{:ok, user}
|
|
|
|
else
|
2021-09-10 11:27:59 +02:00
|
|
|
{:current_password, {:error, _}} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "The password provided is invalid")}
|
2020-02-13 15:48:12 +01:00
|
|
|
|
|
|
|
{:same_email, true} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "The new email must be different")}
|
2020-02-13 15:48:12 +01:00
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
{:email_valid, false} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "The new email doesn't seem to be valid")}
|
2021-09-10 11:27:59 +02:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
{:error, dgettext("errors", "Failed to update user email")}
|
2020-02-13 15:48:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_email(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to change your email")}
|
2020-02-13 15:48:12 +01:00
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec validate_email(map(), %{token: String.t()}, map()) ::
|
|
|
|
{:ok, User.t()} | {:error, String.t()}
|
2020-02-13 15:48:12 +01:00
|
|
|
def validate_email(_parent, %{token: token}, _resolution) do
|
2021-09-10 11:27:59 +02:00
|
|
|
case Users.get_user_by_activation_token(token) do
|
|
|
|
%User{} = user ->
|
|
|
|
case Users.validate_email(user) do
|
|
|
|
{:ok, %User{} = user} ->
|
|
|
|
{:ok, user}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
{:error, dgettext("errors", "Failed to validate user email")}
|
|
|
|
end
|
|
|
|
|
|
|
|
nil ->
|
2021-03-23 16:38:37 +01:00
|
|
|
{:error, dgettext("errors", "Invalid activation token")}
|
2020-02-13 15:48:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-18 17:21:58 +02:00
|
|
|
def delete_account(_parent, %{user_id: user_id}, %{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{
|
|
|
|
current_user: %User{role: role},
|
|
|
|
current_actor: %Actor{} = moderator_actor
|
|
|
|
}
|
2020-08-18 17:21:58 +02:00
|
|
|
})
|
|
|
|
when is_moderator(role) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with %User{disabled: false} = user <- Users.get_user(user_id),
|
2020-08-18 17:21:58 +02:00
|
|
|
{:ok, %User{}} <-
|
2020-10-13 15:07:51 +02:00
|
|
|
do_delete_account(%User{} = user, actor_performing: Relay.get_actor()) do
|
2020-08-18 17:21:58 +02:00
|
|
|
Admin.log_action(moderator_actor, "delete", user)
|
|
|
|
else
|
|
|
|
%User{disabled: true} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "User already disabled")}
|
2020-08-18 17:21:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
def delete_account(_parent, args, %{
|
|
|
|
context: %{current_user: %User{email: email} = user}
|
2020-02-13 15:48:12 +01:00
|
|
|
}) do
|
2020-06-27 19:12:45 +02: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
|
2020-10-13 15:07:51 +02:00
|
|
|
do_delete_account(user, reserve_email: false)
|
2020-06-27 19:12:45 +02:00
|
|
|
else
|
|
|
|
# If the user hasn't got any password (3rd-party auth)
|
|
|
|
{:user_has_password, false} ->
|
2020-10-13 15:07:51 +02:00
|
|
|
do_delete_account(user, reserve_email: false)
|
2020-06-11 19:13:21 +02:00
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
{:confirmation_password, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "The password provided is invalid")}
|
2020-06-27 19:12:45 +02:00
|
|
|
|
|
|
|
{:current_password, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "The password provided is invalid")}
|
2020-02-13 15:48:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_account(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to delete your account")}
|
2020-02-13 15:48:12 +01:00
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-10-13 15:07:51 +02:00
|
|
|
@spec do_delete_account(User.t(), Keyword.t()) :: {:ok, User.t()}
|
|
|
|
defp do_delete_account(%User{} = user, options) do
|
2020-06-11 19:13:21 +02:00
|
|
|
with actors <- Users.get_actors_for_user(user),
|
|
|
|
activated <- not is_nil(user.confirmed_at),
|
|
|
|
# Detach actors from user
|
2020-10-13 15:07:51 +02:00
|
|
|
:ok <- Enum.each(actors, fn actor -> Actors.update_actor(actor, %{user_id: nil}) end),
|
2020-06-11 19:13:21 +02:00
|
|
|
# Launch a background job to delete actors
|
|
|
|
:ok <-
|
|
|
|
Enum.each(actors, fn actor ->
|
2020-10-13 15:07:51 +02:00
|
|
|
actor_performing = Keyword.get(options, :actor_performing, actor)
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Delete.delete(actor, actor_performing, true)
|
2020-06-11 19:13:21 +02:00
|
|
|
end),
|
|
|
|
# Delete user
|
2020-10-13 15:07:51 +02:00
|
|
|
{:ok, user} <-
|
|
|
|
Users.delete_user(user, reserve_email: Keyword.get(options, :reserve_email, activated)) do
|
2020-06-11 19:13:21 +02:00
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec user_settings(User.t(), map(), map()) :: {:ok, list(Setting.t())} | {:error, String.t()}
|
2020-06-11 19:13:21 +02: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 08:57:00 +01: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, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "User requested is not logged-in")}
|
2020-02-18 08:57:00 +01:00
|
|
|
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))
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Error while saving user settings")}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
2020-06-16 18:00:27 +02:00
|
|
|
|
|
|
|
def update_locale(_parent, %{locale: locale}, %{
|
2020-06-16 18:02:53 +02:00
|
|
|
context: %{current_user: %User{locale: current_locale} = user}
|
2020-06-16 18:00:27 +02:00
|
|
|
}) do
|
2021-09-10 11:27:59 +02:00
|
|
|
if current_locale != locale do
|
|
|
|
case Users.update_user(user, %{locale: locale}) do
|
|
|
|
{:ok, %User{} = updated_user} ->
|
|
|
|
{:ok, updated_user}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
Logger.debug(err)
|
|
|
|
{:error, dgettext("errors", "Error while updating locale")}
|
|
|
|
end
|
2020-06-17 15:54:24 +02:00
|
|
|
else
|
2021-09-10 11:27:59 +02:00
|
|
|
{:ok, user}
|
2020-06-16 18:00:27 +02:00
|
|
|
end
|
|
|
|
end
|
2020-08-28 09:23:49 +02:00
|
|
|
|
2020-11-23 10:38:01 +01:00
|
|
|
def user_medias(%User{id: user_id}, %{page: page, limit: limit}, %{
|
|
|
|
context: %{current_user: %User{id: logged_in_user_id}}
|
|
|
|
})
|
|
|
|
when user_id == logged_in_user_id do
|
2020-11-26 11:41:13 +01:00
|
|
|
%{elements: elements, total: total} = Mobilizon.Medias.medias_for_user(user_id, page, limit)
|
2020-11-23 10:38:01 +01:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
elements:
|
|
|
|
Enum.map(elements, fn element ->
|
|
|
|
%{
|
|
|
|
name: element.file.name,
|
|
|
|
url: element.file.url,
|
|
|
|
id: element.id,
|
|
|
|
content_type: element.file.content_type,
|
|
|
|
size: element.file.size
|
|
|
|
}
|
|
|
|
end),
|
|
|
|
total: total
|
|
|
|
}}
|
|
|
|
end
|
|
|
|
|
2020-08-28 09:23:49 +02:00
|
|
|
@spec update_user_login_information(User.t(), map()) ::
|
|
|
|
{:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
defp update_user_login_information(
|
|
|
|
%User{current_sign_in_at: current_sign_in_at, current_sign_in_ip: current_sign_in_ip} =
|
|
|
|
user,
|
|
|
|
context
|
|
|
|
) do
|
|
|
|
with current_ip <- Map.get(context, :ip),
|
|
|
|
now <- DateTime.utc_now() do
|
|
|
|
Users.update_user(user, %{
|
|
|
|
last_sign_in_at: current_sign_in_at || now,
|
|
|
|
last_sign_in_ip: current_sign_in_ip || current_ip,
|
|
|
|
current_sign_in_ip: current_ip,
|
|
|
|
current_sign_in_at: now
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|