2019-11-21 15:51:13 +01:00
|
|
|
defmodule Mix.Tasks.Mobilizon.Users.Modify do
|
|
|
|
@moduledoc """
|
|
|
|
Task to modify an existing Mobilizon user
|
|
|
|
"""
|
|
|
|
use Mix.Task
|
2020-10-30 15:16:01 +01:00
|
|
|
import Mix.Tasks.Mobilizon.Common
|
2019-11-21 15:51:13 +01:00
|
|
|
alias Mobilizon.Users
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
@shortdoc "Modify a Mobilizon user"
|
|
|
|
|
|
|
|
@impl Mix.Task
|
|
|
|
def run([email | rest]) do
|
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
rest,
|
|
|
|
strict: [
|
|
|
|
email: :string,
|
|
|
|
disable: :boolean,
|
|
|
|
enable: :boolean,
|
|
|
|
user: :boolean,
|
|
|
|
moderator: :boolean,
|
|
|
|
admin: :boolean
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
user? = Keyword.get(options, :user, false)
|
|
|
|
moderator? = Keyword.get(options, :moderator, false)
|
|
|
|
admin? = Keyword.get(options, :admin, false)
|
|
|
|
disable? = Keyword.get(options, :disable, false)
|
|
|
|
enable? = Keyword.get(options, :enable, false)
|
|
|
|
new_email = Keyword.get(options, :email)
|
|
|
|
|
|
|
|
if disable? && enable? do
|
2021-06-28 17:23:17 +02:00
|
|
|
shell_error("Can't use both --enable and --disable options at the same time.")
|
2019-11-21 15:51:13 +01:00
|
|
|
end
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
start_mobilizon()
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(email),
|
|
|
|
attrs <- %{},
|
|
|
|
role <- calculate_role(admin?, moderator?, user?),
|
2021-04-19 16:50:16 +02:00
|
|
|
attrs <- process_new_value(attrs, :email, new_email, user.email),
|
2019-11-21 15:51:13 +01:00
|
|
|
attrs <- process_new_value(attrs, :role, role, user.role),
|
|
|
|
attrs <-
|
|
|
|
if(disable? && !is_nil(user.confirmed_at),
|
|
|
|
do: Map.put(attrs, :confirmed_at, nil),
|
|
|
|
else: attrs
|
|
|
|
),
|
|
|
|
attrs <-
|
|
|
|
if(enable? && is_nil(user.confirmed_at),
|
|
|
|
do: Map.put(attrs, :confirmed_at, DateTime.utc_now()),
|
|
|
|
else: attrs
|
|
|
|
),
|
|
|
|
{:makes_changes, true} <- {:makes_changes, attrs != %{}},
|
|
|
|
{:ok, %User{} = user} <- Users.update_user(user, attrs) do
|
2021-09-10 11:27:59 +02:00
|
|
|
status =
|
|
|
|
case user.confirmed_at do
|
|
|
|
%DateTime{} = confirmed_at ->
|
|
|
|
"Activated on #{DateTime.to_string(confirmed_at)} (UTC)"
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
"disabled"
|
|
|
|
end
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_info("""
|
2019-11-21 15:51:13 +01:00
|
|
|
An user has been modified with the following information:
|
|
|
|
- email: #{user.email}
|
|
|
|
- Role: #{user.role}
|
2021-09-10 11:27:59 +02:00
|
|
|
- account status: #{status}
|
2019-11-21 15:51:13 +01:00
|
|
|
""")
|
|
|
|
else
|
|
|
|
{:makes_changes, false} ->
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_info("No change has been made")
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
{:error, :user_not_found} ->
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error("Error: No such user")
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{errors: errors}} ->
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error(inspect(errors))
|
|
|
|
shell_error("User has not been modified because of the above reason.")
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
err ->
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error(inspect(err))
|
|
|
|
shell_error("User has not been modified because of an unknown reason.")
|
2019-11-21 15:51:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(_) do
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error("mobilizon.users.new requires an email as argument")
|
2019-11-21 15:51:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec process_new_value(map(), atom(), any(), any()) :: map()
|
|
|
|
defp process_new_value(attrs, attribute, new_value, old_value) do
|
|
|
|
if !is_nil(new_value) && new_value != old_value do
|
|
|
|
Map.put(attrs, attribute, new_value)
|
|
|
|
else
|
|
|
|
attrs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec calculate_role(boolean(), boolean(), boolean()) ::
|
|
|
|
:administrator | :moderator | :user | nil
|
|
|
|
defp calculate_role(admin?, moderator?, user?) do
|
|
|
|
cond do
|
|
|
|
admin? -> :administrator
|
|
|
|
moderator? -> :moderator
|
|
|
|
user? -> :user
|
|
|
|
true -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|