2018-06-29 02:24:51 +02:00
|
|
|
defmodule Mix.Tasks.Pleroma.User do
|
|
|
|
use Mix.Task
|
2018-12-05 17:58:26 +01:00
|
|
|
import Ecto.Changeset
|
2018-06-29 02:24:51 +02:00
|
|
|
alias Pleroma.{Repo, User}
|
2018-12-06 18:00:24 +01:00
|
|
|
alias Mix.Tasks.Pleroma.Common
|
2018-06-29 02:24:51 +02:00
|
|
|
|
|
|
|
@shortdoc "Manages Pleroma users"
|
|
|
|
@moduledoc """
|
|
|
|
Manages Pleroma users.
|
|
|
|
|
|
|
|
## Create a new user.
|
|
|
|
|
|
|
|
mix pleroma.user new NICKNAME EMAIL [OPTION...]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
- `--name NAME` - the user's name (i.e., "Lain Iwakura")
|
|
|
|
- `--bio BIO` - the user's bio
|
|
|
|
- `--password PASSWORD` - the user's password
|
|
|
|
- `--moderator`/`--no-moderator` - whether the user is a moderator
|
2018-12-01 16:55:52 +01:00
|
|
|
- `--admin`/`--no-admin` - whether the user is an admin
|
2018-12-02 20:27:49 +01:00
|
|
|
|
2018-12-02 20:26:15 +01:00
|
|
|
## Generate an invite link.
|
|
|
|
|
|
|
|
mix pleroma.user invite
|
2018-06-29 02:24:51 +02:00
|
|
|
|
|
|
|
## Delete the user's account.
|
|
|
|
|
|
|
|
mix pleroma.user rm NICKNAME
|
|
|
|
|
|
|
|
## Deactivate or activate the user's account.
|
|
|
|
|
|
|
|
mix pleroma.user toggle_activated NICKNAME
|
|
|
|
|
|
|
|
## Create a password reset link.
|
|
|
|
|
|
|
|
mix pleroma.user reset_password NICKNAME
|
|
|
|
|
|
|
|
## Set the value of the given user's settings.
|
|
|
|
|
|
|
|
mix pleroma.user set NICKNAME [OPTION...]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
- `--locked`/`--no-locked` - whether the user's account is locked
|
|
|
|
- `--moderator`/`--no-moderator` - whether the user is a moderator
|
2018-12-01 16:55:52 +01:00
|
|
|
- `--admin`/`--no-admin` - whether the user is an admin
|
2018-06-29 02:24:51 +02:00
|
|
|
"""
|
|
|
|
def run(["new", nickname, email | rest]) do
|
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
rest,
|
|
|
|
strict: [
|
|
|
|
name: :string,
|
|
|
|
bio: :string,
|
|
|
|
password: :string,
|
2018-12-02 09:36:31 +01:00
|
|
|
moderator: :boolean,
|
|
|
|
admin: :boolean
|
2018-06-29 02:24:51 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
name = Keyword.get(options, :name, nickname)
|
|
|
|
bio = Keyword.get(options, :bio, "")
|
|
|
|
|
|
|
|
{password, generated_password?} =
|
|
|
|
case Keyword.get(options, :password) do
|
|
|
|
nil ->
|
|
|
|
{:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
|
|
|
|
|
|
|
|
password ->
|
|
|
|
{password, false}
|
|
|
|
end
|
|
|
|
|
|
|
|
moderator? = Keyword.get(options, :moderator, false)
|
2018-12-02 09:36:31 +01:00
|
|
|
admin? = Keyword.get(options, :admin, false)
|
2018-06-29 02:24:51 +02:00
|
|
|
|
|
|
|
Mix.shell().info("""
|
|
|
|
A user will be created with the following information:
|
|
|
|
- nickname: #{nickname}
|
|
|
|
- email: #{email}
|
|
|
|
- password: #{
|
|
|
|
if(generated_password?, do: "[generated; a reset link will be created]", else: password)
|
|
|
|
}
|
|
|
|
- name: #{name}
|
|
|
|
- bio: #{bio}
|
|
|
|
- moderator: #{if(moderator?, do: "true", else: "false")}
|
2018-12-02 09:36:31 +01:00
|
|
|
- admin: #{if(admin?, do: "true", else: "false")}
|
2018-06-29 02:24:51 +02:00
|
|
|
""")
|
|
|
|
|
|
|
|
proceed? = Mix.shell().yes?("Continue?")
|
|
|
|
|
|
|
|
unless not proceed? do
|
2018-12-06 18:00:24 +01:00
|
|
|
Common.start_pleroma()
|
2018-06-29 02:24:51 +02:00
|
|
|
|
|
|
|
params =
|
|
|
|
%{
|
|
|
|
nickname: nickname,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
|
|
|
name: name,
|
|
|
|
bio: bio
|
|
|
|
}
|
|
|
|
|> IO.inspect()
|
|
|
|
|
|
|
|
user = User.register_changeset(%User{}, params)
|
|
|
|
Repo.insert!(user)
|
|
|
|
|
|
|
|
Mix.shell().info("User #{nickname} created")
|
|
|
|
|
|
|
|
if moderator? do
|
|
|
|
run(["set", nickname, "--moderator"])
|
|
|
|
end
|
|
|
|
|
2018-12-02 09:36:31 +01:00
|
|
|
if admin? do
|
|
|
|
run(["set", nickname, "--admin"])
|
|
|
|
end
|
|
|
|
|
2018-06-29 02:24:51 +02:00
|
|
|
if generated_password? do
|
|
|
|
run(["reset_password", nickname])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Mix.shell().info("User will not be created.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(["rm", nickname]) do
|
2018-12-06 18:00:24 +01:00
|
|
|
Common.start_pleroma()
|
2018-06-29 02:24:51 +02:00
|
|
|
|
|
|
|
with %User{local: true} = user <- User.get_by_nickname(nickname) do
|
|
|
|
User.delete(user)
|
2018-12-02 09:36:31 +01:00
|
|
|
Mix.shell().info("User #{nickname} deleted.")
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No local user #{nickname}")
|
2018-06-29 02:24:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(["toggle_activated", nickname]) do
|
2018-12-06 18:00:24 +01:00
|
|
|
Common.start_pleroma()
|
2018-06-29 02:24:51 +02:00
|
|
|
|
2018-12-02 18:05:59 +01:00
|
|
|
with %User{} = user <- User.get_by_nickname(nickname) do
|
2018-12-01 16:33:53 +01:00
|
|
|
User.deactivate(user, !user.info["deactivated"])
|
2018-12-02 09:36:31 +01:00
|
|
|
Mix.shell().info("Activation status of #{nickname}: #{user.info["deactivated"]}")
|
|
|
|
else
|
|
|
|
_ ->
|
2018-12-02 18:05:59 +01:00
|
|
|
Mix.shell().error("No user #{nickname}")
|
2018-06-29 02:24:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(["reset_password", nickname]) do
|
2018-12-06 18:00:24 +01:00
|
|
|
Common.start_pleroma()
|
2018-06-29 02:24:51 +02:00
|
|
|
|
|
|
|
with %User{local: true} = user <- User.get_by_nickname(nickname),
|
|
|
|
{:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
|
|
|
|
Mix.shell().info("Generated password reset token for #{user.nickname}")
|
|
|
|
|
|
|
|
IO.puts(
|
|
|
|
"URL: #{
|
|
|
|
Pleroma.Web.Router.Helpers.util_url(
|
|
|
|
Pleroma.Web.Endpoint,
|
|
|
|
:show_password_reset,
|
|
|
|
token.token
|
|
|
|
)
|
|
|
|
}"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No local user #{nickname}")
|
|
|
|
end
|
2018-12-02 18:05:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def run(["unsubscribe", nickname]) do
|
2018-12-06 18:00:24 +01:00
|
|
|
Common.start_pleroma()
|
2018-12-02 18:05:59 +01:00
|
|
|
|
|
|
|
with %User{} = user <- User.get_by_nickname(nickname) do
|
|
|
|
Mix.shell().info("Deactivating #{user.nickname}")
|
|
|
|
User.deactivate(user)
|
|
|
|
|
|
|
|
{:ok, friends} = User.get_friends(user)
|
|
|
|
|
|
|
|
Enum.each(friends, fn friend ->
|
|
|
|
user = Repo.get(User, user.id)
|
|
|
|
|
|
|
|
Mix.shell().info("Unsubscribing #{friend.nickname} from #{user.nickname}")
|
|
|
|
User.unfollow(user, friend)
|
|
|
|
end)
|
|
|
|
|
|
|
|
:timer.sleep(500)
|
|
|
|
|
|
|
|
user = Repo.get(User, user.id)
|
|
|
|
|
|
|
|
if length(user.following) == 0 do
|
|
|
|
Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No user #{nickname}")
|
|
|
|
end
|
2018-06-29 02:24:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def run(["set", nickname | rest]) do
|
2018-12-06 18:00:24 +01:00
|
|
|
Common.start_pleroma()
|
2018-12-04 19:00:45 +01:00
|
|
|
|
2018-06-29 02:24:51 +02:00
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
rest,
|
|
|
|
strict: [
|
|
|
|
moderator: :boolean,
|
2018-12-01 16:55:52 +01:00
|
|
|
admin: :boolean,
|
2018-06-29 02:24:51 +02:00
|
|
|
locked: :boolean
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
with %User{local: true} = user <- User.get_by_nickname(nickname) do
|
2018-12-04 19:00:45 +01:00
|
|
|
case Keyword.get(options, :moderator) do
|
|
|
|
nil -> nil
|
|
|
|
value -> set_moderator(user, value)
|
|
|
|
end
|
2018-06-29 02:24:51 +02:00
|
|
|
|
2018-12-04 19:00:45 +01:00
|
|
|
case Keyword.get(options, :locked) do
|
|
|
|
nil -> nil
|
|
|
|
value -> set_locked(user, value)
|
|
|
|
end
|
2018-06-29 02:24:51 +02:00
|
|
|
|
2018-12-04 19:00:45 +01:00
|
|
|
case Keyword.get(options, :admin) do
|
|
|
|
nil -> nil
|
|
|
|
value -> set_admin(user, value)
|
|
|
|
end
|
2018-06-29 02:24:51 +02:00
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No local user #{nickname}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-04 19:00:45 +01:00
|
|
|
defp set_moderator(user, value) do
|
2018-12-05 17:58:26 +01:00
|
|
|
info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value})
|
2018-12-05 19:12:23 +01:00
|
|
|
|
|
|
|
user_cng =
|
|
|
|
Ecto.Changeset.change(user)
|
|
|
|
|> put_embed(:info, info_cng)
|
2018-12-01 16:55:52 +01:00
|
|
|
|
2018-12-05 17:58:26 +01:00
|
|
|
{:ok, user} = User.update_and_set_cache(user_cng)
|
|
|
|
|
|
|
|
Mix.shell().info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
|
2018-12-04 19:00:45 +01:00
|
|
|
end
|
2018-12-01 16:55:52 +01:00
|
|
|
|
2018-12-04 19:00:45 +01:00
|
|
|
defp set_admin(user, value) do
|
2018-12-05 17:58:26 +01:00
|
|
|
info_cng = User.Info.admin_api_update(user.info, %{is_admin: value})
|
2018-12-05 19:12:23 +01:00
|
|
|
|
|
|
|
user_cng =
|
|
|
|
Ecto.Changeset.change(user)
|
|
|
|
|> put_embed(:info, info_cng)
|
2018-12-05 17:58:26 +01:00
|
|
|
|
|
|
|
{:ok, user} = User.update_and_set_cache(user_cng)
|
2018-12-04 19:00:45 +01:00
|
|
|
|
2018-12-05 17:58:26 +01:00
|
|
|
Mix.shell().info("Admin status of #{user.nickname}: #{user.info.is_moderator}")
|
2018-12-04 19:00:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp set_locked(user, value) do
|
2018-12-05 17:58:26 +01:00
|
|
|
info_cng = User.Info.user_upgrade(user.info, %{locked: value})
|
2018-12-05 19:12:23 +01:00
|
|
|
|
|
|
|
user_cng =
|
|
|
|
Ecto.Changeset.change(user)
|
|
|
|
|> put_embed(:info, info_cng)
|
2018-12-05 17:58:26 +01:00
|
|
|
|
|
|
|
{:ok, user} = User.update_and_set_cache(user_cng)
|
2018-12-04 19:00:45 +01:00
|
|
|
|
2018-12-05 17:58:26 +01:00
|
|
|
Mix.shell().info("Locked status of #{user.nickname}: #{user.info.locked}")
|
2018-12-01 16:55:52 +01:00
|
|
|
end
|
|
|
|
|
2018-12-02 20:26:15 +01:00
|
|
|
def run(["invite"]) do
|
2018-12-06 18:00:24 +01:00
|
|
|
Common.start_pleroma()
|
2018-12-02 20:26:15 +01:00
|
|
|
|
|
|
|
with {:ok, token} <- Pleroma.UserInviteToken.create_token() do
|
|
|
|
Mix.shell().info("Generated user invite token")
|
|
|
|
|
|
|
|
url =
|
|
|
|
Pleroma.Web.Router.Helpers.redirect_url(
|
|
|
|
Pleroma.Web.Endpoint,
|
|
|
|
:registration_page,
|
|
|
|
token.token
|
|
|
|
)
|
|
|
|
|
|
|
|
IO.puts(url)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("Could not create invite token.")
|
2018-12-02 20:27:49 +01:00
|
|
|
end
|
2018-12-02 20:26:15 +01:00
|
|
|
end
|
2018-06-29 02:24:51 +02:00
|
|
|
end
|