1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.UserController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper,
9 only: [fetch_integer_param: 3]
11 alias Pleroma.ModerationLog
13 alias Pleroma.Web.ActivityPub.Builder
14 alias Pleroma.Web.ActivityPub.Pipeline
15 alias Pleroma.Web.AdminAPI
16 alias Pleroma.Web.AdminAPI.Search
17 alias Pleroma.Web.Plugs.OAuthScopesPlug
21 plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
25 %{scopes: ["admin:read:accounts"]}
26 when action in [:index, :show]
31 %{scopes: ["admin:write:accounts"]}
46 %{scopes: ["admin:write:follows"]}
47 when action in [:follow, :unfollow]
50 action_fallback(AdminAPI.FallbackController)
52 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.UserOperation
54 def delete(%{private: %{open_api_spex: %{params: %{nickname: nickname}}}} = conn, _) do
56 |> do_deletes([nickname])
61 private: %{open_api_spex: %{body_params: %{nicknames: nicknames}}}
66 |> do_deletes(nicknames)
69 defp do_deletes(%{assigns: %{user: admin}} = conn, nicknames) when is_list(nicknames) do
70 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
72 Enum.each(users, fn user ->
73 {:ok, delete_data, _} = Builder.delete(admin, user.ap_id)
74 Pipeline.common_pipeline(delete_data, local: true)
77 ModerationLog.insert_log(%{
88 assigns: %{user: admin},
92 follower: follower_nick,
93 followed: followed_nick
100 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
101 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
102 User.follow(follower, followed)
104 ModerationLog.insert_log(%{
117 assigns: %{user: admin},
121 follower: follower_nick,
122 followed: followed_nick
129 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
130 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
131 User.unfollow(follower, followed)
133 ModerationLog.insert_log(%{
146 assigns: %{user: admin},
147 private: %{open_api_spex: %{body_params: %{users: users}}}
153 |> Enum.map(fn %{nickname: nickname, email: email, password: password} ->
159 password_confirmation: password,
163 User.register_changeset(%User{}, user_data, need_confirmation: false)
165 |> Enum.reduce(Ecto.Multi.new(), fn changeset, multi ->
166 Ecto.Multi.insert(multi, Ecto.UUID.generate(), changeset)
169 case Pleroma.Repo.transaction(changesets) do
174 |> Enum.map(fn user ->
175 {:ok, user} = User.post_register_action(user)
180 ModerationLog.insert_log(%{
186 render(conn, "created_many.json", users: users)
188 {:error, id, changeset, _} ->
190 Enum.map(changesets.operations, fn
191 {^id, {:changeset, _current_changeset, _}} ->
194 {_, {:changeset, current_changeset, _}} ->
199 |> put_status(:conflict)
200 |> render("create_errors.json", changesets: changesets)
206 assigns: %{user: admin},
207 private: %{open_api_spex: %{params: %{nickname: nickname}}}
211 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
212 render(conn, "show.json", %{user: user})
214 _ -> {:error, :not_found}
218 def toggle_activation(
219 %{assigns: %{user: admin}, private: %{open_api_spex: %{params: %{nickname: nickname}}}} =
223 user = User.get_cached_by_nickname(nickname)
225 {:ok, updated_user} = User.set_activation(user, !user.is_active)
227 action = if !user.is_active, do: "activate", else: "deactivate"
229 ModerationLog.insert_log(%{
235 render(conn, "show.json", user: updated_user)
240 assigns: %{user: admin},
241 private: %{open_api_spex: %{body_params: %{nicknames: nicknames}}}
245 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
246 {:ok, updated_users} = User.set_activation(users, true)
248 ModerationLog.insert_log(%{
254 render(conn, "index.json", users: updated_users)
259 assigns: %{user: admin},
260 private: %{open_api_spex: %{body_params: %{nicknames: nicknames}}}
264 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
265 {:ok, updated_users} = User.set_activation(users, false)
267 ModerationLog.insert_log(%{
273 render(conn, "index.json", users: updated_users)
278 assigns: %{user: admin},
279 private: %{open_api_spex: %{body_params: %{nicknames: nicknames}}}
283 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
284 {:ok, updated_users} = User.approve(users)
286 ModerationLog.insert_log(%{
292 render(conn, "index.json", users: updated_users)
297 assigns: %{user: admin},
298 private: %{open_api_spex: %{body_params: %{nicknames: nicknames}}}
302 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
303 {:ok, updated_users} = User.set_suggestion(users, true)
305 ModerationLog.insert_log(%{
308 action: "add_suggestion"
311 render(conn, "index.json", users: updated_users)
316 assigns: %{user: admin},
317 private: %{open_api_spex: %{body_params: %{nicknames: nicknames}}}
321 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
322 {:ok, updated_users} = User.set_suggestion(users, false)
324 ModerationLog.insert_log(%{
327 action: "remove_suggestion"
330 render(conn, "index.json", users: updated_users)
333 def index(%{private: %{open_api_spex: %{params: params}}} = conn, _) do
334 {page, page_size} = page_params(params)
335 filters = maybe_parse_filters(params[:filters])
339 query: params[:query],
341 page_size: page_size,
344 email: params[:email],
345 actor_types: params[:actor_types]
347 |> Map.merge(filters)
349 with {:ok, users, count} <- Search.user(search_params) do
350 render(conn, "index.json", users: users, count: count, page_size: page_size)
354 @filters ~w(local external active deactivated need_approval unconfirmed is_admin is_moderator)
356 @spec maybe_parse_filters(String.t()) :: %{required(String.t()) => true} | %{}
357 defp maybe_parse_filters(filters) when is_nil(filters) or filters == "", do: %{}
359 defp maybe_parse_filters(filters) do
362 |> Enum.filter(&Enum.member?(@filters, &1))
363 |> Map.new(&{String.to_existing_atom(&1), true})
366 defp page_params(params) do
368 fetch_integer_param(params, :page, 1),
369 fetch_integer_param(params, :page_size, @users_page_size)