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.AdminAPIController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper,
9 only: [json_response: 3, fetch_integer_param: 3]
13 alias Pleroma.ModerationLog
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.AdminAPI
18 alias Pleroma.Web.AdminAPI.AccountView
19 alias Pleroma.Web.AdminAPI.ModerationLogView
20 alias Pleroma.Web.Endpoint
21 alias Pleroma.Web.Plugs.OAuthScopesPlug
22 alias Pleroma.Web.Router
28 %{scopes: ["admin:read:accounts"]}
29 when action in [:right_get, :show_user_credentials, :create_backup]
34 %{scopes: ["admin:write:accounts"]}
37 :force_password_reset,
44 :right_delete_multiple,
45 :update_user_credentials
51 %{scopes: ["admin:read:statuses"]}
52 when action in [:list_user_statuses]
57 %{scopes: ["admin:read:chats"]}
58 when action in [:list_user_chats]
63 %{scopes: ["admin:read"]}
73 %{scopes: ["admin:write"]}
76 :resend_confirmation_email,
82 action_fallback(AdminAPI.FallbackController)
84 def list_user_statuses(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname} = params) do
85 with_reblogs = params["with_reblogs"] == "true" || params["with_reblogs"] == true
86 godmode = params["godmode"] == "true" || params["godmode"] == true
88 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
89 {page, page_size} = page_params(params)
92 ActivityPub.fetch_user_activities(user, nil, %{
94 offset: (page - 1) * page_size,
96 exclude_reblogs: not with_reblogs,
97 pagination_type: :offset,
102 |> put_view(AdminAPI.StatusView)
103 |> render("index.json", %{total: result[:total], activities: result[:items], as: :activity})
105 _ -> {:error, :not_found}
109 def list_user_chats(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname} = _params) do
110 with %User{id: user_id} <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
112 Pleroma.Chat.for_user_query(user_id)
113 |> Pleroma.Repo.all()
116 |> put_view(AdminAPI.ChatView)
117 |> render("index.json", chats: chats)
119 _ -> {:error, :not_found}
123 def tag_users(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames, "tags" => tags}) do
124 with {:ok, _} <- User.tag(nicknames, tags) do
125 ModerationLog.insert_log(%{
127 nicknames: nicknames,
132 json_response(conn, :no_content, "")
136 def untag_users(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames, "tags" => tags}) do
137 with {:ok, _} <- User.untag(nicknames, tags) do
138 ModerationLog.insert_log(%{
140 nicknames: nicknames,
145 json_response(conn, :no_content, "")
149 def right_add_multiple(%{assigns: %{user: admin}} = conn, %{
150 "permission_group" => permission_group,
151 "nicknames" => nicknames
153 when permission_group in ["moderator", "admin"] do
154 update = %{:"is_#{permission_group}" => true}
156 users = nicknames |> Enum.map(&User.get_cached_by_nickname/1)
158 for u <- users, do: User.admin_api_update(u, update)
160 ModerationLog.insert_log(%{
164 permission: permission_group
170 def right_add_multiple(conn, _) do
171 render_error(conn, :not_found, "No such permission_group")
174 def right_add(%{assigns: %{user: admin}} = conn, %{
175 "permission_group" => permission_group,
176 "nickname" => nickname
178 when permission_group in ["moderator", "admin"] do
179 fields = %{:"is_#{permission_group}" => true}
183 |> User.get_cached_by_nickname()
184 |> User.admin_api_update(fields)
186 ModerationLog.insert_log(%{
190 permission: permission_group
196 def right_add(conn, _) do
197 render_error(conn, :not_found, "No such permission_group")
200 def right_get(conn, %{"nickname" => nickname}) do
201 user = User.get_cached_by_nickname(nickname)
205 is_moderator: user.is_moderator,
206 is_admin: user.is_admin
210 def right_delete_multiple(
211 %{assigns: %{user: %{nickname: admin_nickname} = admin}} = conn,
213 "permission_group" => permission_group,
214 "nicknames" => nicknames
217 when permission_group in ["moderator", "admin"] do
218 with false <- Enum.member?(nicknames, admin_nickname) do
219 update = %{:"is_#{permission_group}" => false}
221 users = nicknames |> Enum.map(&User.get_cached_by_nickname/1)
223 for u <- users, do: User.admin_api_update(u, update)
225 ModerationLog.insert_log(%{
229 permission: permission_group
234 _ -> render_error(conn, :forbidden, "You can't revoke your own admin/moderator status.")
238 def right_delete_multiple(conn, _) do
239 render_error(conn, :not_found, "No such permission_group")
243 %{assigns: %{user: admin}} = conn,
245 "permission_group" => permission_group,
246 "nickname" => nickname
249 when permission_group in ["moderator", "admin"] do
250 fields = %{:"is_#{permission_group}" => false}
254 |> User.get_cached_by_nickname()
255 |> User.admin_api_update(fields)
257 ModerationLog.insert_log(%{
261 permission: permission_group
267 def right_delete(%{assigns: %{user: %{nickname: nickname}}} = conn, %{"nickname" => nickname}) do
268 render_error(conn, :forbidden, "You can't revoke your own admin status.")
271 @doc "Get a password reset token (base64 string) for given nickname"
272 def get_password_reset(conn, %{"nickname" => nickname}) do
273 (%User{local: true} = user) = User.get_cached_by_nickname(nickname)
274 {:ok, token} = Pleroma.PasswordResetToken.create_token(user)
279 link: Router.Helpers.reset_password_url(Endpoint, :reset, token.token)
283 @doc "Force password reset for a given user"
284 def force_password_reset(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
285 users = nicknames |> Enum.map(&User.get_cached_by_nickname/1)
287 Enum.each(users, &User.force_password_reset_async/1)
289 ModerationLog.insert_log(%{
292 action: "force_password_reset"
295 json_response(conn, :no_content, "")
298 @doc "Disable mfa for user's account."
299 def disable_mfa(conn, %{"nickname" => nickname}) do
300 case User.get_by_nickname(nickname) do
310 @doc "Show a given user's credentials"
311 def show_user_credentials(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
312 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
314 |> put_view(AccountView)
315 |> render("credentials.json", %{user: user, for: admin})
317 _ -> {:error, :not_found}
321 @doc "Updates a given user"
322 def update_user_credentials(
323 %{assigns: %{user: admin}} = conn,
324 %{"nickname" => nickname} = params
326 with {_, %User{} = user} <- {:user, User.get_cached_by_nickname(nickname)},
328 User.update_as_admin(user, params) do
329 ModerationLog.insert_log(%{
332 action: "updated_users"
335 if params["password"] do
336 User.force_password_reset_async(user)
339 ModerationLog.insert_log(%{
342 action: "force_password_reset"
345 json(conn, %{status: "success"})
347 {:error, changeset} ->
348 errors = Map.new(changeset.errors, fn {key, {error, _}} -> {key, error} end)
357 def list_log(conn, params) do
358 {page, page_size} = page_params(params)
361 ModerationLog.get_all(%{
363 page_size: page_size,
364 start_date: params["start_date"],
365 end_date: params["end_date"],
366 user_id: params["user_id"],
367 search: params["search"]
371 |> put_view(ModerationLogView)
372 |> render("index.json", %{log: log})
375 def restart(conn, _params) do
376 with :ok <- configurable_from_database() do
377 Restarter.Pleroma.restart(Config.get(:env), 50)
383 def need_reboot(conn, _params) do
384 json(conn, %{need_reboot: Restarter.Pleroma.need_reboot?()})
387 defp configurable_from_database do
388 if Config.get(:configurable_from_database) do
391 {:error, "You must enable configurable_from_database in your config file."}
395 def reload_emoji(conn, _params) do
396 Pleroma.Emoji.reload()
401 def confirm_email(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
402 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
406 ModerationLog.insert_log(%{actor: admin, subject: users, action: "confirm_email"})
411 def resend_confirmation_email(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
413 Enum.map(nicknames, fn nickname ->
415 |> User.get_cached_by_nickname()
416 |> User.send_confirmation_email()
419 ModerationLog.insert_log(%{actor: admin, subject: users, action: "resend_confirmation_email"})
424 def stats(conn, params) do
425 counters = Stats.get_status_visibility_count(params["instance"])
427 json(conn, %{"status_visibility" => counters})
430 def create_backup(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
431 with %User{} = user <- User.get_by_nickname(nickname),
432 {:ok, _} <- Pleroma.User.Backup.create(user, admin.id) do
433 ModerationLog.insert_log(%{actor: admin, subject: user, action: "create_backup"})
439 defp page_params(params) do
441 fetch_integer_param(params, "page", 1),
442 fetch_integer_param(params, "page_size", @users_page_size)