2018-12-23 20:05:55 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-06-29 00:24:51 +00:00
|
|
|
defmodule Mix.Tasks.Pleroma.User do
|
|
|
|
use Mix.Task
|
2018-12-05 16:58:26 +00:00
|
|
|
import Ecto.Changeset
|
2019-03-05 02:52:23 +00:00
|
|
|
alias Mix.Tasks.Pleroma.Common
|
2019-02-09 15:16:26 +00:00
|
|
|
alias Pleroma.User
|
2019-04-06 09:58:22 +00:00
|
|
|
alias Pleroma.UserInviteToken
|
2018-06-29 00:24:51 +00: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 15:55:52 +00:00
|
|
|
- `--admin`/`--no-admin` - whether the user is an admin
|
2019-04-02 10:01:26 +00:00
|
|
|
- `-y`, `--assume-yes`/`--no-assume-yes` - whether to assume yes to all questions
|
2018-12-02 19:27:49 +00:00
|
|
|
|
2018-12-02 19:26:15 +00:00
|
|
|
## Generate an invite link.
|
2018-12-09 09:12:48 +00:00
|
|
|
|
2019-04-06 09:58:22 +00:00
|
|
|
mix pleroma.user invite [OPTION...]
|
|
|
|
|
|
|
|
Options:
|
2019-04-06 15:38:35 +00:00
|
|
|
- `--expire_at DATE` - last day on which token is active (e.g. "2019-04-05")
|
|
|
|
- `--max_use NUMBER` - maximum numbers of token uses
|
2019-04-06 09:58:22 +00:00
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
## List generated invites
|
2019-04-06 09:58:22 +00:00
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
mix pleroma.user invites
|
2019-04-06 09:58:22 +00:00
|
|
|
|
|
|
|
## Revoke invite
|
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
mix pleroma.user revoke_invite TOKEN OR TOKEN_ID
|
2018-06-29 00:24:51 +00:00
|
|
|
|
|
|
|
## Delete the user's account.
|
|
|
|
|
|
|
|
mix pleroma.user rm NICKNAME
|
|
|
|
|
2019-04-02 09:30:11 +00:00
|
|
|
## Delete the user's activities.
|
|
|
|
|
|
|
|
mix pleroma.user delete_activities NICKNAME
|
|
|
|
|
2018-06-29 00:24:51 +00:00
|
|
|
## Deactivate or activate the user's account.
|
|
|
|
|
|
|
|
mix pleroma.user toggle_activated NICKNAME
|
2018-12-07 07:44:54 +00:00
|
|
|
|
2018-12-07 07:39:54 +00:00
|
|
|
## Unsubscribe local users from user's account and deactivate it
|
2018-12-09 09:12:48 +00:00
|
|
|
|
2018-12-07 07:39:54 +00:00
|
|
|
mix pleroma.user unsubscribe NICKNAME
|
2018-06-29 00:24:51 +00:00
|
|
|
|
|
|
|
## 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 15:55:52 +00:00
|
|
|
- `--admin`/`--no-admin` - whether the user is an admin
|
2019-02-04 02:33:11 +00:00
|
|
|
|
|
|
|
## Add tags to a user.
|
|
|
|
|
|
|
|
mix pleroma.user tag NICKNAME TAGS
|
|
|
|
|
|
|
|
## Delete tags from a user.
|
|
|
|
|
|
|
|
mix pleroma.user untag NICKNAME TAGS
|
2018-06-29 00:24:51 +00:00
|
|
|
"""
|
|
|
|
def run(["new", nickname, email | rest]) do
|
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
rest,
|
|
|
|
strict: [
|
|
|
|
name: :string,
|
|
|
|
bio: :string,
|
|
|
|
password: :string,
|
2018-12-02 08:36:31 +00:00
|
|
|
moderator: :boolean,
|
2019-01-04 21:11:46 +00:00
|
|
|
admin: :boolean,
|
|
|
|
assume_yes: :boolean
|
|
|
|
],
|
|
|
|
aliases: [
|
|
|
|
y: :assume_yes
|
2018-06-29 00:24:51 +00: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 08:36:31 +00:00
|
|
|
admin? = Keyword.get(options, :admin, false)
|
2019-01-04 21:11:46 +00:00
|
|
|
assume_yes? = Keyword.get(options, :assume_yes, false)
|
2018-06-29 00:24:51 +00: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 08:36:31 +00:00
|
|
|
- admin: #{if(admin?, do: "true", else: "false")}
|
2018-06-29 00:24:51 +00:00
|
|
|
""")
|
|
|
|
|
2019-01-04 21:11:46 +00:00
|
|
|
proceed? = assume_yes? or Mix.shell().yes?("Continue?")
|
2018-06-29 00:24:51 +00:00
|
|
|
|
|
|
|
unless not proceed? do
|
2018-12-06 17:00:24 +00:00
|
|
|
Common.start_pleroma()
|
2018-06-29 00:24:51 +00:00
|
|
|
|
2018-12-16 16:25:31 +00:00
|
|
|
params = %{
|
|
|
|
nickname: nickname,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
|
|
|
name: name,
|
|
|
|
bio: bio
|
|
|
|
}
|
2018-06-29 00:24:51 +00:00
|
|
|
|
2018-12-18 14:13:52 +00:00
|
|
|
changeset = User.register_changeset(%User{}, params, confirmed: true)
|
|
|
|
{:ok, _user} = User.register(changeset)
|
2018-06-29 00:24:51 +00:00
|
|
|
|
|
|
|
Mix.shell().info("User #{nickname} created")
|
|
|
|
|
|
|
|
if moderator? do
|
|
|
|
run(["set", nickname, "--moderator"])
|
|
|
|
end
|
|
|
|
|
2018-12-02 08:36:31 +00:00
|
|
|
if admin? do
|
|
|
|
run(["set", nickname, "--admin"])
|
|
|
|
end
|
|
|
|
|
2018-06-29 00:24:51 +00: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 17:00:24 +00:00
|
|
|
Common.start_pleroma()
|
2018-06-29 00:24:51 +00:00
|
|
|
|
|
|
|
with %User{local: true} = user <- User.get_by_nickname(nickname) do
|
|
|
|
User.delete(user)
|
2018-12-02 08:36:31 +00:00
|
|
|
Mix.shell().info("User #{nickname} deleted.")
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No local user #{nickname}")
|
2018-06-29 00:24:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(["toggle_activated", nickname]) do
|
2018-12-06 17:00:24 +00:00
|
|
|
Common.start_pleroma()
|
2018-06-29 00:24:51 +00:00
|
|
|
|
2018-12-02 17:05:59 +00:00
|
|
|
with %User{} = user <- User.get_by_nickname(nickname) do
|
2018-12-12 20:30:16 +00:00
|
|
|
{:ok, user} = User.deactivate(user, !user.info.deactivated)
|
|
|
|
|
|
|
|
Mix.shell().info(
|
|
|
|
"Activation status of #{nickname}: #{if(user.info.deactivated, do: "de", else: "")}activated"
|
|
|
|
)
|
2018-12-02 08:36:31 +00:00
|
|
|
else
|
|
|
|
_ ->
|
2018-12-02 17:05:59 +00:00
|
|
|
Mix.shell().error("No user #{nickname}")
|
2018-06-29 00:24:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(["reset_password", nickname]) do
|
2018-12-06 17:00:24 +00:00
|
|
|
Common.start_pleroma()
|
2018-06-29 00:24:51 +00: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 17:05:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run(["unsubscribe", nickname]) do
|
2018-12-06 17:00:24 +00:00
|
|
|
Common.start_pleroma()
|
2018-12-02 17:05:59 +00: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 ->
|
2019-04-02 10:01:26 +00:00
|
|
|
user = User.get_by_id(user.id)
|
2018-12-02 17:05:59 +00:00
|
|
|
|
|
|
|
Mix.shell().info("Unsubscribing #{friend.nickname} from #{user.nickname}")
|
|
|
|
User.unfollow(user, friend)
|
|
|
|
end)
|
|
|
|
|
|
|
|
:timer.sleep(500)
|
|
|
|
|
2019-04-02 10:01:26 +00:00
|
|
|
user = User.get_by_id(user.id)
|
2018-12-02 17:05:59 +00:00
|
|
|
|
2019-02-03 17:44:18 +00:00
|
|
|
if Enum.empty?(user.following) do
|
2018-12-02 17:05:59 +00:00
|
|
|
Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No user #{nickname}")
|
|
|
|
end
|
2018-06-29 00:24:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run(["set", nickname | rest]) do
|
2018-12-06 17:00:24 +00:00
|
|
|
Common.start_pleroma()
|
2018-12-04 18:00:45 +00:00
|
|
|
|
2018-06-29 00:24:51 +00:00
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
rest,
|
|
|
|
strict: [
|
|
|
|
moderator: :boolean,
|
2018-12-01 15:55:52 +00:00
|
|
|
admin: :boolean,
|
2018-06-29 00:24:51 +00:00
|
|
|
locked: :boolean
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
with %User{local: true} = user <- User.get_by_nickname(nickname) do
|
2018-12-12 20:32:32 +00:00
|
|
|
user =
|
|
|
|
case Keyword.get(options, :moderator) do
|
|
|
|
nil -> user
|
|
|
|
value -> set_moderator(user, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
user =
|
|
|
|
case Keyword.get(options, :locked) do
|
|
|
|
nil -> user
|
|
|
|
value -> set_locked(user, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
_user =
|
|
|
|
case Keyword.get(options, :admin) do
|
|
|
|
nil -> user
|
|
|
|
value -> set_admin(user, value)
|
|
|
|
end
|
2018-06-29 00:24:51 +00:00
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No local user #{nickname}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-04 02:33:11 +00:00
|
|
|
def run(["tag", nickname | tags]) do
|
|
|
|
Common.start_pleroma()
|
|
|
|
|
|
|
|
with %User{} = user <- User.get_by_nickname(nickname) do
|
|
|
|
user = user |> User.tag(tags)
|
|
|
|
|
|
|
|
Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("Could not change user tags for #{nickname}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(["untag", nickname | tags]) do
|
|
|
|
Common.start_pleroma()
|
|
|
|
|
|
|
|
with %User{} = user <- User.get_by_nickname(nickname) do
|
|
|
|
user = user |> User.untag(tags)
|
|
|
|
|
|
|
|
Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("Could not change user tags for #{nickname}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-06 09:58:22 +00:00
|
|
|
def run(["invite" | rest]) do
|
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(rest,
|
|
|
|
strict: [
|
2019-04-06 15:38:35 +00:00
|
|
|
expire_at: :string,
|
2019-04-06 09:58:22 +00:00
|
|
|
max_use: :integer
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
options =
|
|
|
|
options
|
|
|
|
|> Keyword.update(:expire_at, {:ok, nil}, fn
|
|
|
|
nil -> {:ok, nil}
|
|
|
|
val -> Date.from_iso8601(val)
|
|
|
|
end)
|
|
|
|
|> Enum.into(%{})
|
2019-04-06 09:58:22 +00:00
|
|
|
|
2018-12-09 09:12:48 +00:00
|
|
|
Common.start_pleroma()
|
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
with {:ok, val} <- options[:expire_at],
|
|
|
|
options = Map.put(options, :expire_at, val),
|
|
|
|
{:ok, invite} <- UserInviteToken.create_invite(options) do
|
2019-04-06 09:58:22 +00:00
|
|
|
Mix.shell().info(
|
2019-04-06 13:24:22 +00:00
|
|
|
"Generated user invite token " <> String.replace(invite.invite_type, "_", " ")
|
2019-04-06 09:58:22 +00:00
|
|
|
)
|
2018-12-09 09:12:48 +00:00
|
|
|
|
|
|
|
url =
|
|
|
|
Pleroma.Web.Router.Helpers.redirect_url(
|
|
|
|
Pleroma.Web.Endpoint,
|
|
|
|
:registration_page,
|
2019-04-06 13:24:22 +00:00
|
|
|
invite.token
|
2018-12-09 09:12:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
IO.puts(url)
|
|
|
|
else
|
2019-04-06 15:38:35 +00:00
|
|
|
error ->
|
|
|
|
Mix.shell().error("Could not create invite token: #{inspect(error)}")
|
2018-12-09 09:12:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
def run(["invites"]) do
|
2019-04-06 09:58:22 +00:00
|
|
|
Common.start_pleroma()
|
|
|
|
|
|
|
|
Mix.shell().info("Invites list:")
|
|
|
|
|
|
|
|
UserInviteToken.list_invites()
|
|
|
|
|> Enum.each(fn invite ->
|
2019-04-06 15:38:35 +00:00
|
|
|
expire_info =
|
|
|
|
with expire_at when not is_nil(expire_at) <- invite.expire_at do
|
|
|
|
" | Expire at: #{Date.to_string(expire_at)}"
|
2019-04-06 09:58:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
using_info =
|
2019-04-06 15:38:35 +00:00
|
|
|
with max_use when not is_nil(max_use) <- invite.max_use do
|
|
|
|
" | Max use: #{max_use} Left use: #{max_use - invite.uses}"
|
2019-04-06 09:58:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Mix.shell().info(
|
2019-04-06 15:38:35 +00:00
|
|
|
"ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
|
2019-04-06 09:58:22 +00:00
|
|
|
invite.used
|
2019-04-06 15:38:35 +00:00
|
|
|
}#{expire_info}#{using_info}"
|
2019-04-06 09:58:22 +00:00
|
|
|
)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
def run(["revoke_invite", token]) do
|
2019-04-06 09:58:22 +00:00
|
|
|
Common.start_pleroma()
|
|
|
|
|
2019-04-06 15:38:35 +00:00
|
|
|
with {:ok, invite} <- UserInviteToken.find_by_token(token),
|
|
|
|
{:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
|
2019-04-06 09:58:22 +00:00
|
|
|
Mix.shell().info("Invite for token #{token} was revoked.")
|
|
|
|
else
|
|
|
|
_ -> Mix.shell().error("No invite found with token #{token}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-12 15:04:08 +00:00
|
|
|
def run(["delete_activities", nickname]) do
|
|
|
|
Common.start_pleroma()
|
|
|
|
|
|
|
|
with %User{local: true} = user <- User.get_by_nickname(nickname) do
|
|
|
|
User.delete_user_activities(user)
|
2019-04-02 09:30:11 +00:00
|
|
|
Mix.shell().info("User #{nickname} statuses deleted.")
|
2019-03-12 15:04:08 +00:00
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Mix.shell().error("No local user #{nickname}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-04 18:00:45 +00:00
|
|
|
defp set_moderator(user, value) do
|
2018-12-05 16:58:26 +00:00
|
|
|
info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value})
|
2018-12-05 18:12:23 +00:00
|
|
|
|
|
|
|
user_cng =
|
|
|
|
Ecto.Changeset.change(user)
|
|
|
|
|> put_embed(:info, info_cng)
|
2018-12-01 15:55:52 +00:00
|
|
|
|
2018-12-05 16:58:26 +00:00
|
|
|
{:ok, user} = User.update_and_set_cache(user_cng)
|
|
|
|
|
|
|
|
Mix.shell().info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
|
2018-12-12 20:32:32 +00:00
|
|
|
user
|
2018-12-04 18:00:45 +00:00
|
|
|
end
|
2018-12-01 15:55:52 +00:00
|
|
|
|
2018-12-04 18:00:45 +00:00
|
|
|
defp set_admin(user, value) do
|
2018-12-05 16:58:26 +00:00
|
|
|
info_cng = User.Info.admin_api_update(user.info, %{is_admin: value})
|
2018-12-05 18:12:23 +00:00
|
|
|
|
|
|
|
user_cng =
|
|
|
|
Ecto.Changeset.change(user)
|
|
|
|
|> put_embed(:info, info_cng)
|
2018-12-05 16:58:26 +00:00
|
|
|
|
|
|
|
{:ok, user} = User.update_and_set_cache(user_cng)
|
2018-12-04 18:00:45 +00:00
|
|
|
|
2018-12-12 20:32:32 +00:00
|
|
|
Mix.shell().info("Admin status of #{user.nickname}: #{user.info.is_admin}")
|
|
|
|
user
|
2018-12-04 18:00:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp set_locked(user, value) do
|
2018-12-05 16:58:26 +00:00
|
|
|
info_cng = User.Info.user_upgrade(user.info, %{locked: value})
|
2018-12-05 18:12:23 +00:00
|
|
|
|
|
|
|
user_cng =
|
|
|
|
Ecto.Changeset.change(user)
|
|
|
|
|> put_embed(:info, info_cng)
|
2018-12-05 16:58:26 +00:00
|
|
|
|
|
|
|
{:ok, user} = User.update_and_set_cache(user_cng)
|
2018-12-04 18:00:45 +00:00
|
|
|
|
2018-12-05 16:58:26 +00:00
|
|
|
Mix.shell().info("Locked status of #{user.nickname}: #{user.info.locked}")
|
2018-12-12 20:32:32 +00:00
|
|
|
user
|
2018-12-01 15:55:52 +00:00
|
|
|
end
|
2018-06-29 00:24:51 +00:00
|
|
|
end
|