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
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error("Can't use both --enabled 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?),
|
|
|
|
attrs <- process_new_value(attrs, :mail, new_email, user.email),
|
|
|
|
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
|
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}
|
|
|
|
- Activated: #{if user.confirmed_at, do: user.confirmed_at, else: "False"}
|
|
|
|
""")
|
|
|
|
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
|