X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fadmin_api%2Fcontrollers%2Fuser_controller.ex;h=50208a8b76a1b4ac634632a29d69f58f0326c80f;hb=dc9f66749c85352e0d7761ae2b9b8663a3aa3e3a;hp=a2a1c875de6de3848e0f8b5f64ceefae9f86ccd7;hpb=329aa19c9f1175f915441c84689860155949bb08;p=akkoma
diff --git a/lib/pleroma/web/admin_api/controllers/user_controller.ex b/lib/pleroma/web/admin_api/controllers/user_controller.ex
index a2a1c875d..50208a8b7 100644
--- a/lib/pleroma/web/admin_api/controllers/user_controller.ex
+++ b/lib/pleroma/web/admin_api/controllers/user_controller.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.UserController do
@@ -13,44 +13,51 @@ defmodule Pleroma.Web.AdminAPI.UserController do
alias Pleroma.Web.ActivityPub.Builder
alias Pleroma.Web.ActivityPub.Pipeline
alias Pleroma.Web.AdminAPI
- alias Pleroma.Web.AdminAPI.AccountView
alias Pleroma.Web.AdminAPI.Search
alias Pleroma.Web.Plugs.OAuthScopesPlug
@users_page_size 50
+ plug(Pleroma.Web.ApiSpec.CastAndValidate)
+
plug(
OAuthScopesPlug,
- %{scopes: ["read:accounts"], admin: true}
- when action in [:list, :show]
+ %{scopes: ["admin:read:accounts"]}
+ when action in [:index, :show]
)
plug(
OAuthScopesPlug,
- %{scopes: ["write:accounts"], admin: true}
+ %{scopes: ["admin:write:accounts"]}
when action in [
:delete,
:create,
:toggle_activation,
:activate,
:deactivate,
- :approve
+ :approve,
+ :suggest,
+ :unsuggest
]
)
plug(
OAuthScopesPlug,
- %{scopes: ["write:follows"], admin: true}
+ %{scopes: ["admin:write:follows"]}
when action in [:follow, :unfollow]
)
action_fallback(AdminAPI.FallbackController)
- def delete(conn, %{"nickname" => nickname}) do
- delete(conn, %{"nicknames" => [nickname]})
+ 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(%{})
end
- def delete(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
+ def delete(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
Enum.each(users, fn user ->
@@ -67,10 +74,16 @@ defmodule Pleroma.Web.AdminAPI.UserController do
json(conn, nicknames)
end
- def follow(%{assigns: %{user: admin}} = conn, %{
- "follower" => follower_nick,
- "followed" => followed_nick
- }) do
+ def follow(
+ %{
+ assigns: %{user: admin},
+ body_params: %{
+ follower: follower_nick,
+ followed: followed_nick
+ }
+ } = conn,
+ _
+ ) do
with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
%User{} = followed <- User.get_cached_by_nickname(followed_nick) do
User.follow(follower, followed)
@@ -86,10 +99,16 @@ defmodule Pleroma.Web.AdminAPI.UserController do
json(conn, "ok")
end
- def unfollow(%{assigns: %{user: admin}} = conn, %{
- "follower" => follower_nick,
- "followed" => followed_nick
- }) do
+ def unfollow(
+ %{
+ assigns: %{user: admin},
+ body_params: %{
+ follower: follower_nick,
+ followed: followed_nick
+ }
+ } = conn,
+ _
+ ) do
with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
%User{} = followed <- User.get_cached_by_nickname(followed_nick) do
User.unfollow(follower, followed)
@@ -105,9 +124,10 @@ defmodule Pleroma.Web.AdminAPI.UserController do
json(conn, "ok")
end
- def create(%{assigns: %{user: admin}} = conn, %{"users" => users}) do
+ def create(%{assigns: %{user: admin}, body_params: %{users: users}} = conn, _) do
changesets =
- Enum.map(users, fn %{"nickname" => nickname, "email" => email, "password" => password} ->
+ users
+ |> Enum.map(fn %{nickname: nickname, email: email, password: password} ->
user_data = %{
nickname: nickname,
name: nickname,
@@ -124,57 +144,54 @@ defmodule Pleroma.Web.AdminAPI.UserController do
end)
case Pleroma.Repo.transaction(changesets) do
- {:ok, users} ->
- res =
- users
+ {:ok, users_map} ->
+ users =
+ users_map
|> Map.values()
|> Enum.map(fn user ->
{:ok, user} = User.post_register_action(user)
user
end)
- |> Enum.map(&AccountView.render("created.json", %{user: &1}))
ModerationLog.insert_log(%{
actor: admin,
- subjects: Map.values(users),
+ subjects: users,
action: "create"
})
- json(conn, res)
+ render(conn, "created_many.json", users: users)
{:error, id, changeset, _} ->
- res =
+ changesets =
Enum.map(changesets.operations, fn
- {current_id, {:changeset, _current_changeset, _}} when current_id == id ->
- AccountView.render("create-error.json", %{changeset: changeset})
+ {^id, {:changeset, _current_changeset, _}} ->
+ changeset
{_, {:changeset, current_changeset, _}} ->
- AccountView.render("create-error.json", %{changeset: current_changeset})
+ current_changeset
end)
conn
|> put_status(:conflict)
- |> json(res)
+ |> render("create_errors.json", changesets: changesets)
end
end
- def show(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
+ def show(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
- conn
- |> put_view(AccountView)
- |> render("show.json", %{user: user})
+ render(conn, "show.json", %{user: user})
else
_ -> {:error, :not_found}
end
end
- def toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
+ def toggle_activation(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
user = User.get_cached_by_nickname(nickname)
- {:ok, updated_user} = User.deactivate(user, !user.deactivated)
+ {:ok, updated_user} = User.set_activation(user, !user.is_active)
- action = if user.deactivated, do: "activate", else: "deactivate"
+ action = if !user.is_active, do: "activate", else: "deactivate"
ModerationLog.insert_log(%{
actor: admin,
@@ -182,14 +199,12 @@ defmodule Pleroma.Web.AdminAPI.UserController do
action: action
})
- conn
- |> put_view(AccountView)
- |> render("show.json", %{user: updated_user})
+ render(conn, "show.json", user: updated_user)
end
- def activate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
+ def activate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
- {:ok, updated_users} = User.deactivate(users, false)
+ {:ok, updated_users} = User.set_activation(users, true)
ModerationLog.insert_log(%{
actor: admin,
@@ -197,14 +212,12 @@ defmodule Pleroma.Web.AdminAPI.UserController do
action: "activate"
})
- conn
- |> put_view(AccountView)
- |> render("index.json", %{users: Keyword.values(updated_users)})
+ render(conn, "index.json", users: Keyword.values(updated_users))
end
- def deactivate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
+ def deactivate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
- {:ok, updated_users} = User.deactivate(users, true)
+ {:ok, updated_users} = User.set_activation(users, false)
ModerationLog.insert_log(%{
actor: admin,
@@ -212,12 +225,10 @@ defmodule Pleroma.Web.AdminAPI.UserController do
action: "deactivate"
})
- conn
- |> put_view(AccountView)
- |> render("index.json", %{users: Keyword.values(updated_users)})
+ render(conn, "index.json", users: Keyword.values(updated_users))
end
- def approve(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
+ def approve(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
{:ok, updated_users} = User.approve(users)
@@ -227,36 +238,53 @@ defmodule Pleroma.Web.AdminAPI.UserController do
action: "approve"
})
- conn
- |> put_view(AccountView)
- |> render("index.json", %{users: updated_users})
+ render(conn, "index.json", users: updated_users)
+ end
+
+ def suggest(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
+ users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
+ {:ok, updated_users} = User.set_suggestion(users, true)
+
+ ModerationLog.insert_log(%{
+ actor: admin,
+ subject: users,
+ action: "add_suggestion"
+ })
+
+ render(conn, "index.json", users: updated_users)
+ end
+
+ def unsuggest(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
+ users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
+ {:ok, updated_users} = User.set_suggestion(users, false)
+
+ ModerationLog.insert_log(%{
+ actor: admin,
+ subject: users,
+ action: "remove_suggestion"
+ })
+
+ render(conn, "index.json", users: updated_users)
end
- def list(conn, params) do
+ def index(conn, params) do
{page, page_size} = page_params(params)
- filters = maybe_parse_filters(params["filters"])
+ filters = maybe_parse_filters(params[:filters])
search_params =
%{
- query: params["query"],
+ query: params[:query],
page: page,
page_size: page_size,
- tags: params["tags"],
- name: params["name"],
- email: params["email"],
- actor_types: params["actor_types"]
+ tags: params[:tags],
+ name: params[:name],
+ email: params[:email],
+ actor_types: params[:actor_types]
}
|> Map.merge(filters)
with {:ok, users, count} <- Search.user(search_params) do
- json(
- conn,
- AccountView.render("index.json",
- users: users,
- count: count,
- page_size: page_size
- )
- )
+ render(conn, "index.json", users: users, count: count, page_size: page_size)
end
end
@@ -274,8 +302,8 @@ defmodule Pleroma.Web.AdminAPI.UserController do
defp page_params(params) do
{
- fetch_integer_param(params, "page", 1),
- fetch_integer_param(params, "page_size", @users_page_size)
+ fetch_integer_param(params, :page, 1),
+ fetch_integer_param(params, :page_size, @users_page_size)
}
end
end