2019-11-21 15:51:13 +01:00
|
|
|
defmodule Mix.Tasks.Mobilizon.Users.New do
|
|
|
|
@moduledoc """
|
|
|
|
Task to create a new user
|
|
|
|
"""
|
|
|
|
use Mix.Task
|
2020-10-30 15:16:01 +01:00
|
|
|
import Mix.Tasks.Mobilizon.Common
|
2021-07-20 18:22:18 +02:00
|
|
|
import Mix.Tasks.Mobilizon.Actors.Utils
|
|
|
|
alias Mobilizon.Actors.Actor
|
2019-11-21 15:51:13 +01:00
|
|
|
alias Mobilizon.Users
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
@shortdoc "Manages Mobilizon users"
|
|
|
|
|
|
|
|
@impl Mix.Task
|
|
|
|
def run([email | rest]) do
|
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
rest,
|
|
|
|
strict: [
|
|
|
|
password: :string,
|
|
|
|
moderator: :boolean,
|
2021-07-20 18:22:18 +02:00
|
|
|
admin: :boolean,
|
|
|
|
profile_username: :string,
|
|
|
|
profile_display_name: :string
|
2019-11-21 15:51:13 +01:00
|
|
|
],
|
|
|
|
aliases: [
|
|
|
|
p: :password
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
moderator? = Keyword.get(options, :moderator, false)
|
|
|
|
admin? = Keyword.get(options, :admin, false)
|
|
|
|
|
|
|
|
role =
|
|
|
|
cond do
|
|
|
|
admin? -> :administrator
|
|
|
|
moderator? -> :moderator
|
|
|
|
true -> :user
|
|
|
|
end
|
|
|
|
|
|
|
|
password =
|
|
|
|
Keyword.get(
|
|
|
|
options,
|
|
|
|
:password,
|
|
|
|
:crypto.strong_rand_bytes(16) |> Base.encode64() |> binary_part(0, 16)
|
|
|
|
)
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
start_mobilizon()
|
2019-11-21 15:51:13 +01:00
|
|
|
|
|
|
|
case Users.register(%{
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
role: role,
|
|
|
|
confirmed_at: DateTime.utc_now(),
|
|
|
|
confirmation_sent_at: nil,
|
|
|
|
confirmation_token: nil
|
|
|
|
}) do
|
|
|
|
{:ok, %User{} = user} ->
|
2021-07-20 18:22:18 +02:00
|
|
|
profile = maybe_create_profile(user, options)
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_info("""
|
2019-11-21 15:51:13 +01:00
|
|
|
An user has been created with the following information:
|
|
|
|
- email: #{user.email}
|
|
|
|
- password: #{password}
|
|
|
|
- Role: #{user.role}
|
|
|
|
""")
|
|
|
|
|
2021-07-20 18:22:18 +02:00
|
|
|
if is_nil(profile) do
|
|
|
|
shell_info("""
|
|
|
|
The user will be prompted to create a new profile after login for the first time.
|
|
|
|
""")
|
|
|
|
else
|
|
|
|
shell_info("""
|
|
|
|
A profile was added with the following information:
|
|
|
|
- username: #{profile.preferred_username}
|
|
|
|
- display name: #{profile.name}
|
|
|
|
""")
|
|
|
|
end
|
|
|
|
|
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 created 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 created 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
|
2021-07-20 18:22:18 +02:00
|
|
|
|
|
|
|
@spec maybe_create_profile(User.t(), Keyword.t()) :: Actor.t() | nil
|
|
|
|
defp maybe_create_profile(%User{} = user, options) do
|
|
|
|
profile_username = Keyword.get(options, :profile_username)
|
|
|
|
profile_name = Keyword.get(options, :profile_display_name)
|
|
|
|
|
|
|
|
if profile_name != nil || profile_username != nil do
|
|
|
|
create_profile(user, profile_username, profile_name)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2019-11-21 15:51:13 +01:00
|
|
|
end
|