Add the possibility to create profiles and groups from CLI

- Create an actor at the same time when creating an user
- or create either a profile and attach it to an existing user
- or create a group and set the admin to an existing profile

Closes #785

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-07-20 18:22:18 +02:00
parent debcdaeed8
commit 11e75eaf66
7 changed files with 479 additions and 7 deletions

View File

@@ -4,6 +4,8 @@ defmodule Mix.Tasks.Mobilizon.Users.New do
"""
use Mix.Task
import Mix.Tasks.Mobilizon.Common
import Mix.Tasks.Mobilizon.Actors.Utils
alias Mobilizon.Actors.Actor
alias Mobilizon.Users
alias Mobilizon.Users.User
@@ -17,7 +19,9 @@ defmodule Mix.Tasks.Mobilizon.Users.New do
strict: [
password: :string,
moderator: :boolean,
admin: :boolean
admin: :boolean,
profile_username: :string,
profile_display_name: :string
],
aliases: [
p: :password
@@ -52,14 +56,27 @@ defmodule Mix.Tasks.Mobilizon.Users.New do
confirmation_token: nil
}) do
{:ok, %User{} = user} ->
profile = maybe_create_profile(user, options)
shell_info("""
An user has been created with the following information:
- email: #{user.email}
- password: #{password}
- Role: #{user.role}
The user will be prompted to create a new profile after login for the first time.
""")
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
{:error, %Ecto.Changeset{errors: errors}} ->
shell_error(inspect(errors))
shell_error("User has not been created because of the above reason.")
@@ -73,4 +90,16 @@ defmodule Mix.Tasks.Mobilizon.Users.New do
def run(_) do
shell_error("mobilizon.users.new requires an email as argument")
end
@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
end