1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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)
25 %{scopes: ["admin:read:accounts"]}
26 when action in [:index, :show]
31 %{scopes: ["admin:write:accounts"]}
44 %{scopes: ["admin:write:follows"]}
45 when action in [:follow, :unfollow]
48 plug(:put_view, Pleroma.Web.AdminAPI.AccountView)
50 action_fallback(AdminAPI.FallbackController)
52 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.UserOperation
54 def delete(conn, %{nickname: nickname}) do
56 |> Map.put(:body_params, %{nicknames: [nickname]})
60 def delete(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
61 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
63 Enum.each(users, fn user ->
64 {:ok, delete_data, _} = Builder.delete(admin, user.ap_id)
65 Pipeline.common_pipeline(delete_data, local: true)
68 ModerationLog.insert_log(%{
79 assigns: %{user: admin},
81 follower: follower_nick,
82 followed: followed_nick
87 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
88 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
89 User.follow(follower, followed)
91 ModerationLog.insert_log(%{
104 assigns: %{user: admin},
106 follower: follower_nick,
107 followed: followed_nick
112 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
113 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
114 User.unfollow(follower, followed)
116 ModerationLog.insert_log(%{
127 def create(%{assigns: %{user: admin}, body_params: %{users: users}} = conn, _) do
130 |> Enum.map(fn %{nickname: nickname, email: email, password: password} ->
136 password_confirmation: password,
140 User.register_changeset(%User{}, user_data, need_confirmation: false)
142 |> Enum.reduce(Ecto.Multi.new(), fn changeset, multi ->
143 Ecto.Multi.insert(multi, Ecto.UUID.generate(), changeset)
146 case Pleroma.Repo.transaction(changesets) do
151 |> Enum.map(fn user ->
152 {:ok, user} = User.post_register_action(user)
157 ModerationLog.insert_log(%{
163 render(conn, "created_many.json", users: users)
165 {:error, id, changeset, _} ->
167 Enum.map(changesets.operations, fn
168 {^id, {:changeset, _current_changeset, _}} ->
171 {_, {:changeset, current_changeset, _}} ->
176 |> put_status(:conflict)
177 |> render("create_errors.json", changesets: changesets)
181 def show(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
182 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
183 render(conn, "show.json", %{user: user})
185 _ -> {:error, :not_found}
189 def toggle_activation(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
190 user = User.get_cached_by_nickname(nickname)
192 {:ok, updated_user} = User.set_activation(user, !user.is_active)
194 action = if !user.is_active, do: "activate", else: "deactivate"
196 ModerationLog.insert_log(%{
202 render(conn, "show.json", user: updated_user)
205 def activate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
206 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
207 {:ok, updated_users} = User.set_activation(users, true)
209 ModerationLog.insert_log(%{
215 render(conn, "index.json", users: Keyword.values(updated_users))
218 def deactivate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
219 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
220 {:ok, updated_users} = User.set_activation(users, false)
222 ModerationLog.insert_log(%{
228 render(conn, "index.json", users: Keyword.values(updated_users))
231 def approve(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
232 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
233 {:ok, updated_users} = User.approve(users)
235 ModerationLog.insert_log(%{
241 render(conn, "index.json", users: updated_users)
244 def index(conn, params) do
245 {page, page_size} = page_params(params)
246 filters = maybe_parse_filters(params[:filters])
250 query: params[:query],
252 page_size: page_size,
255 email: params[:email],
256 actor_types: params[:actor_types]
258 |> Map.merge(filters)
260 with {:ok, users, count} <- Search.user(search_params) do
261 render(conn, "index.json", users: users, count: count, page_size: page_size)
265 @filters ~w(local external active deactivated need_approval unconfirmed is_admin is_moderator)
267 @spec maybe_parse_filters(String.t()) :: %{required(String.t()) => true} | %{}
268 defp maybe_parse_filters(filters) when is_nil(filters) or filters == "", do: %{}
270 defp maybe_parse_filters(filters) do
273 |> Enum.filter(&Enum.member?(@filters, &1))
274 |> Map.new(&{String.to_existing_atom(&1), true})
277 defp page_params(params) do
279 fetch_integer_param(params, :page, 1),
280 fetch_integer_param(params, :page_size, @users_page_size)