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

@@ -0,0 +1,102 @@
defmodule Mix.Tasks.Mobilizon.Actors.New do
@moduledoc """
Task to create a new user
"""
use Mix.Task
import Mix.Tasks.Mobilizon.Actors.Utils
import Mix.Tasks.Mobilizon.Common
alias Mobilizon.Actors.Actor
alias Mobilizon.{Actors, Users}
alias Mobilizon.Users.User
@shortdoc "Manages Mobilizon users"
@impl Mix.Task
def run(rest) do
{options, [], []} =
OptionParser.parse(
rest,
strict: [
email: :string,
username: :string,
display_name: :string,
group_admin: :string,
type: :string
],
aliases: [
e: :email,
u: :username,
d: :display_name,
t: :type,
a: :group_admin
]
)
start_mobilizon()
profile_username = Keyword.get(options, :username)
profile_name = Keyword.get(options, :display_name)
if profile_name != nil || profile_username != nil do
else
shell_error("You need to provide at least --username or --display-name.")
end
case Keyword.get(options, :type, "profile") do
"profile" ->
do_create_profile(options, profile_username, profile_name)
"group" ->
do_create_group(options, profile_username, profile_name)
end
end
@spec do_create_profile(Keyword.t(), String.t(), String.t()) :: Actor.t() | nil
defp do_create_profile(options, profile_username, profile_name) do
with {:email, email} when is_binary(email) <- {:email, Keyword.get(options, :email)},
{:ok, %User{} = user} <- Users.get_user_by_email(email),
%Actor{preferred_username: preferred_username, name: name} <-
create_profile(user, profile_username, profile_name, default: false) do
shell_info("""
A profile was created for user #{email} with the following information:
- username: #{preferred_username}
- display name: #{name}
""")
else
{:email, nil} ->
shell_error("You need to provide an email for creating a new profile.")
{:error, :user_not_found} ->
shell_error("No user with this email was found.")
nil ->
nil
end
end
defp do_create_group(options, profile_username, profile_name) do
with {:option, admin_name} when is_binary(admin_name) <-
{:option, Keyword.get(options, :group_admin)},
{:admin, %Actor{} = admin} <- {:admin, Actors.get_local_actor_by_name(admin_name)},
{:ok, %Actor{preferred_username: preferred_username, name: name}} <-
create_group(admin, profile_username, profile_name) do
shell_info("""
A group was created with profile #{admin_name} as the admin and with the following information:
- username: #{preferred_username}
- display name: #{name}
""")
else
{:option, nil} ->
shell_error(
"You need to provide --group-admin with the username of the admin to create a group."
)
{:admin, nil} ->
shell_error("Profile with username #{Keyword.get(options, :group_admin)} wasn't found")
{:error, :insert_group, %Ecto.Changeset{errors: errors}, _} ->
shell_error(inspect(errors))
shell_error("Error while creating group because of the above reason")
end
end
end

View File

@@ -0,0 +1,58 @@
defmodule Mix.Tasks.Mobilizon.Actors.Utils do
@moduledoc """
Tools for generating usernames from display names
"""
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Users.User
@doc """
Removes all spaces, accents, special characters and diacritics from a string to create a plain ascii username (a-z0-9_)
See https://stackoverflow.com/a/37511463
"""
@spec generate_username(String.t()) :: String.t()
def generate_username(""), do: ""
def generate_username(name) do
name
|> String.downcase()
|> String.normalize(:nfd)
|> String.replace(~r/[\x{0300}-\x{036f}]/u, "")
|> String.replace(~r/ /, "_")
|> String.replace(~r/[^a-z0-9_]/, "")
end
# Profile from name
@spec username_and_name(String.t() | nil, String.t() | nil) :: String.t()
def username_and_name(nil, profile_name) do
{generate_username(profile_name), profile_name}
end
def username_and_name(profile_username, nil) do
{profile_username, profile_username}
end
def username_and_name(profile_username, profile_name) do
{profile_username, profile_name}
end
def create_profile(%User{id: user_id}, username, name, options \\ []) do
{username, name} = username_and_name(username, name)
{:ok, %Actor{} = new_person} =
Actors.new_person(
%{preferred_username: username, user_id: user_id, name: name},
Keyword.get(options, :default, true)
)
new_person
end
def create_group(%Actor{id: admin_id}, username, name, _options \\ []) do
{username, name} = username_and_name(username, name)
Actors.create_group(%{creator_actor_id: admin_id, preferred_username: username, name: name})
end
end

View File

@@ -62,10 +62,16 @@ defmodule Mix.Tasks.Mobilizon.Common do
end
@spec shell_error(String.t()) :: :ok
def shell_error(message) do
if mix_shell?(),
do: Mix.shell().error(message),
else: IO.puts(:stderr, message)
def shell_error(message, options \\ []) do
if mix_shell?() do
Mix.shell().error(message)
else
IO.puts(:stderr, message)
end
if Application.fetch_env!(:mobilizon, :env) != :test do
exit({:shutdown, Keyword.get(options, :error_code, 1)})
end
end
@doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"

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