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 action_fallback(AdminAPI.FallbackController)
50 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.UserOperation
52 def delete(conn, %{nickname: nickname}) do
54 |> Map.put(:body_params, %{nicknames: [nickname]})
58 def delete(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
59 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
61 Enum.each(users, fn user ->
62 {:ok, delete_data, _} = Builder.delete(admin, user.ap_id)
63 Pipeline.common_pipeline(delete_data, local: true)
66 ModerationLog.insert_log(%{
77 assigns: %{user: admin},
79 follower: follower_nick,
80 followed: followed_nick
85 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
86 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
87 User.follow(follower, followed)
89 ModerationLog.insert_log(%{
102 assigns: %{user: admin},
104 follower: follower_nick,
105 followed: followed_nick
110 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
111 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
112 User.unfollow(follower, followed)
114 ModerationLog.insert_log(%{
125 def create(%{assigns: %{user: admin}, body_params: %{users: users}} = conn, _) do
128 |> Enum.map(fn %{nickname: nickname, email: email, password: password} ->
134 password_confirmation: password,
138 User.register_changeset(%User{}, user_data, need_confirmation: false)
140 |> Enum.reduce(Ecto.Multi.new(), fn changeset, multi ->
141 Ecto.Multi.insert(multi, Ecto.UUID.generate(), changeset)
144 case Pleroma.Repo.transaction(changesets) do
149 |> Enum.map(fn user ->
150 {:ok, user} = User.post_register_action(user)
155 ModerationLog.insert_log(%{
161 render(conn, "created_many.json", users: users)
163 {:error, id, changeset, _} ->
165 Enum.map(changesets.operations, fn
166 {^id, {:changeset, _current_changeset, _}} ->
169 {_, {:changeset, current_changeset, _}} ->
174 |> put_status(:conflict)
175 |> render("create_errors.json", changesets: changesets)
179 def show(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
180 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
181 render(conn, "show.json", %{user: user})
183 _ -> {:error, :not_found}
187 def toggle_activation(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
188 user = User.get_cached_by_nickname(nickname)
190 {:ok, updated_user} = User.set_activation(user, !user.is_active)
192 action = if !user.is_active, do: "activate", else: "deactivate"
194 ModerationLog.insert_log(%{
200 render(conn, "show.json", user: updated_user)
203 def activate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
204 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
205 {:ok, updated_users} = User.set_activation(users, true)
207 ModerationLog.insert_log(%{
213 render(conn, "index.json", users: Keyword.values(updated_users))
216 def deactivate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
217 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
218 {:ok, updated_users} = User.set_activation(users, false)
220 ModerationLog.insert_log(%{
226 render(conn, "index.json", users: Keyword.values(updated_users))
229 def approve(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
230 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
231 {:ok, updated_users} = User.approve(users)
233 ModerationLog.insert_log(%{
239 render(conn, "index.json", users: updated_users)
242 def index(conn, params) do
243 {page, page_size} = page_params(params)
244 filters = maybe_parse_filters(params[:filters])
248 query: params[:query],
250 page_size: page_size,
253 email: params[:email],
254 actor_types: params[:actor_types]
256 |> Map.merge(filters)
258 with {:ok, users, count} <- Search.user(search_params) do
259 render(conn, "index.json", users: users, count: count, page_size: page_size)
263 @filters ~w(local external active deactivated need_approval unconfirmed is_admin is_moderator)
265 @spec maybe_parse_filters(String.t()) :: %{required(String.t()) => true} | %{}
266 defp maybe_parse_filters(filters) when is_nil(filters) or filters == "", do: %{}
268 defp maybe_parse_filters(filters) do
271 |> Enum.filter(&Enum.member?(@filters, &1))
272 |> Map.new(&{String.to_existing_atom(&1), true})
275 defp page_params(params) do
277 fetch_integer_param(params, :page, 1),
278 fetch_integer_param(params, :page_size, @users_page_size)