2020-09-21 14:01:03 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-09-21 14:01:03 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.AdminAPI.UserController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper,
|
|
|
|
only: [fetch_integer_param: 3]
|
|
|
|
|
|
|
|
alias Pleroma.ModerationLog
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.Builder
|
|
|
|
alias Pleroma.Web.ActivityPub.Pipeline
|
|
|
|
alias Pleroma.Web.AdminAPI
|
|
|
|
alias Pleroma.Web.AdminAPI.Search
|
2020-10-22 11:04:23 +02:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
@users_page_size 50
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2020-09-21 14:01:03 +02:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2021-02-17 19:37:23 +01:00
|
|
|
%{scopes: ["admin:read:accounts"]}
|
2021-03-02 17:49:17 +01:00
|
|
|
when action in [:index, :show]
|
2020-09-21 14:01:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2021-02-17 19:37:23 +01:00
|
|
|
%{scopes: ["admin:write:accounts"]}
|
2020-09-21 14:01:03 +02:00
|
|
|
when action in [
|
|
|
|
:delete,
|
|
|
|
:create,
|
|
|
|
:toggle_activation,
|
|
|
|
:activate,
|
|
|
|
:deactivate,
|
|
|
|
:approve
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2021-02-17 19:37:23 +01:00
|
|
|
%{scopes: ["admin:write:follows"]}
|
2020-09-21 14:01:03 +02:00
|
|
|
when action in [:follow, :unfollow]
|
|
|
|
)
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
plug(:put_view, Pleroma.Web.AdminAPI.AccountView)
|
|
|
|
|
2020-09-21 14:01:03 +02:00
|
|
|
action_fallback(AdminAPI.FallbackController)
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.UserOperation
|
|
|
|
|
|
|
|
def delete(conn, %{nickname: nickname}) do
|
|
|
|
conn
|
|
|
|
|> Map.put(:body_params, %{nicknames: [nickname]})
|
|
|
|
|> delete(%{})
|
2020-09-21 14:01:03 +02:00
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def delete(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
|
2020-09-21 14:01:03 +02:00
|
|
|
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
|
|
|
|
|
|
|
|
Enum.each(users, fn user ->
|
|
|
|
{:ok, delete_data, _} = Builder.delete(admin, user.ap_id)
|
|
|
|
Pipeline.common_pipeline(delete_data, local: true)
|
|
|
|
end)
|
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
|
|
|
subject: users,
|
|
|
|
action: "delete"
|
|
|
|
})
|
|
|
|
|
|
|
|
json(conn, nicknames)
|
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def follow(
|
|
|
|
%{
|
|
|
|
assigns: %{user: admin},
|
|
|
|
body_params: %{
|
|
|
|
follower: follower_nick,
|
|
|
|
followed: followed_nick
|
|
|
|
}
|
|
|
|
} = conn,
|
|
|
|
_
|
|
|
|
) do
|
2020-09-21 14:01:03 +02:00
|
|
|
with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
|
|
|
|
%User{} = followed <- User.get_cached_by_nickname(followed_nick) do
|
|
|
|
User.follow(follower, followed)
|
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
|
|
|
followed: followed,
|
|
|
|
follower: follower,
|
|
|
|
action: "follow"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
json(conn, "ok")
|
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def unfollow(
|
|
|
|
%{
|
|
|
|
assigns: %{user: admin},
|
|
|
|
body_params: %{
|
|
|
|
follower: follower_nick,
|
|
|
|
followed: followed_nick
|
|
|
|
}
|
|
|
|
} = conn,
|
|
|
|
_
|
|
|
|
) do
|
2020-09-21 14:01:03 +02:00
|
|
|
with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
|
|
|
|
%User{} = followed <- User.get_cached_by_nickname(followed_nick) do
|
|
|
|
User.unfollow(follower, followed)
|
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
|
|
|
followed: followed,
|
|
|
|
follower: follower,
|
|
|
|
action: "unfollow"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
json(conn, "ok")
|
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def create(%{assigns: %{user: admin}, body_params: %{users: users}} = conn, _) do
|
2020-09-21 14:01:03 +02:00
|
|
|
changesets =
|
2021-03-02 17:49:17 +01:00
|
|
|
users
|
|
|
|
|> Enum.map(fn %{nickname: nickname, email: email, password: password} ->
|
2020-09-21 14:01:03 +02:00
|
|
|
user_data = %{
|
|
|
|
nickname: nickname,
|
|
|
|
name: nickname,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
|
|
|
bio: "."
|
|
|
|
}
|
|
|
|
|
|
|
|
User.register_changeset(%User{}, user_data, need_confirmation: false)
|
|
|
|
end)
|
|
|
|
|> Enum.reduce(Ecto.Multi.new(), fn changeset, multi ->
|
|
|
|
Ecto.Multi.insert(multi, Ecto.UUID.generate(), changeset)
|
|
|
|
end)
|
|
|
|
|
|
|
|
case Pleroma.Repo.transaction(changesets) do
|
2021-03-02 17:49:17 +01:00
|
|
|
{:ok, users_map} ->
|
|
|
|
users =
|
|
|
|
users_map
|
2020-09-21 14:01:03 +02:00
|
|
|
|> Map.values()
|
|
|
|
|> Enum.map(fn user ->
|
|
|
|
{:ok, user} = User.post_register_action(user)
|
|
|
|
|
|
|
|
user
|
|
|
|
end)
|
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
2021-03-02 17:49:17 +01:00
|
|
|
subjects: users,
|
2020-09-21 14:01:03 +02:00
|
|
|
action: "create"
|
|
|
|
})
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
render(conn, "created_many.json", users: users)
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
{:error, id, changeset, _} ->
|
2021-03-02 17:49:17 +01:00
|
|
|
changesets =
|
2020-09-21 14:01:03 +02:00
|
|
|
Enum.map(changesets.operations, fn
|
2021-03-02 17:49:17 +01:00
|
|
|
{^id, {:changeset, _current_changeset, _}} ->
|
|
|
|
changeset
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
{_, {:changeset, current_changeset, _}} ->
|
2021-03-02 17:49:17 +01:00
|
|
|
current_changeset
|
2020-09-21 14:01:03 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
2021-03-02 17:49:17 +01:00
|
|
|
|> render("create_errors.json", changesets: changesets)
|
2020-09-21 14:01:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def show(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
|
2020-09-21 14:01:03 +02:00
|
|
|
with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
|
2021-03-02 17:49:17 +01:00
|
|
|
render(conn, "show.json", %{user: user})
|
2020-09-21 14:01:03 +02:00
|
|
|
else
|
|
|
|
_ -> {:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def toggle_activation(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
|
2020-09-21 14:01:03 +02:00
|
|
|
user = User.get_cached_by_nickname(nickname)
|
|
|
|
|
2020-10-14 00:16:03 +02:00
|
|
|
{:ok, updated_user} = User.set_activation(user, !user.is_active)
|
2020-09-21 14:01:03 +02:00
|
|
|
|
2020-10-13 00:42:27 +02:00
|
|
|
action = if !user.is_active, do: "activate", else: "deactivate"
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
|
|
|
subject: [user],
|
|
|
|
action: action
|
|
|
|
})
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
render(conn, "show.json", user: updated_user)
|
2020-09-21 14:01:03 +02:00
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def activate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
|
2020-09-21 14:01:03 +02:00
|
|
|
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
|
2020-10-14 00:16:03 +02:00
|
|
|
{:ok, updated_users} = User.set_activation(users, true)
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
|
|
|
subject: users,
|
|
|
|
action: "activate"
|
|
|
|
})
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
render(conn, "index.json", users: Keyword.values(updated_users))
|
2020-09-21 14:01:03 +02:00
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def deactivate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
|
2020-09-21 14:01:03 +02:00
|
|
|
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
|
2020-10-14 00:16:03 +02:00
|
|
|
{:ok, updated_users} = User.set_activation(users, false)
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
|
|
|
subject: users,
|
|
|
|
action: "deactivate"
|
|
|
|
})
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
render(conn, "index.json", users: Keyword.values(updated_users))
|
2020-09-21 14:01:03 +02:00
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def approve(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
|
2020-09-21 14:01:03 +02:00
|
|
|
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
|
|
|
|
{:ok, updated_users} = User.approve(users)
|
|
|
|
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
actor: admin,
|
|
|
|
subject: users,
|
|
|
|
action: "approve"
|
|
|
|
})
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
render(conn, "index.json", users: updated_users)
|
2020-09-21 14:01:03 +02:00
|
|
|
end
|
|
|
|
|
2021-03-02 17:49:17 +01:00
|
|
|
def index(conn, params) do
|
2020-09-21 14:01:03 +02:00
|
|
|
{page, page_size} = page_params(params)
|
2021-03-02 17:49:17 +01:00
|
|
|
filters = maybe_parse_filters(params[:filters])
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
search_params =
|
|
|
|
%{
|
2021-03-02 17:49:17 +01:00
|
|
|
query: params[:query],
|
2020-09-21 14:01:03 +02:00
|
|
|
page: page,
|
|
|
|
page_size: page_size,
|
2021-03-02 17:49:17 +01:00
|
|
|
tags: params[:tags],
|
|
|
|
name: params[:name],
|
|
|
|
email: params[:email],
|
|
|
|
actor_types: params[:actor_types]
|
2020-09-21 14:01:03 +02:00
|
|
|
}
|
|
|
|
|> Map.merge(filters)
|
|
|
|
|
|
|
|
with {:ok, users, count} <- Search.user(search_params) do
|
2021-03-02 17:49:17 +01:00
|
|
|
render(conn, "index.json", users: users, count: count, page_size: page_size)
|
2020-09-21 14:01:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-25 08:39:49 +02:00
|
|
|
@filters ~w(local external active deactivated need_approval unconfirmed is_admin is_moderator)
|
2020-09-21 14:01:03 +02:00
|
|
|
|
|
|
|
@spec maybe_parse_filters(String.t()) :: %{required(String.t()) => true} | %{}
|
|
|
|
defp maybe_parse_filters(filters) when is_nil(filters) or filters == "", do: %{}
|
|
|
|
|
|
|
|
defp maybe_parse_filters(filters) do
|
|
|
|
filters
|
|
|
|
|> String.split(",")
|
|
|
|
|> Enum.filter(&Enum.member?(@filters, &1))
|
|
|
|
|> Map.new(&{String.to_existing_atom(&1), true})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp page_params(params) do
|
|
|
|
{
|
2021-03-02 17:49:17 +01:00
|
|
|
fetch_integer_param(params, :page, 1),
|
|
|
|
fetch_integer_param(params, :page_size, @users_page_size)
|
2020-09-21 14:01:03 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|