1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.AdminAPIController do
8 use Pleroma.Web, :controller
10 alias Pleroma.Web.ActivityPub.Relay
11 alias Pleroma.Web.MastodonAPI.Admin.AccountView
13 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
17 action_fallback(:errors)
19 def user_delete(conn, %{"nickname" => nickname}) do
20 User.get_by_nickname(nickname)
29 %{"nickname" => nickname, "email" => email, "password" => password}
36 password_confirmation: password,
40 changeset = User.register_changeset(%User{}, user_data, confirmed: true)
41 {:ok, user} = User.register(changeset)
44 |> json(user.nickname)
47 def user_toggle_disabled(conn, %{"nickname" => nickname}) do
48 user = User.get_by_nickname(nickname)
50 {:ok, updated_user} = User.disable(user, !user.info.disabled)
53 |> put_view(AccountView)
54 |> render("show.json", %{user: updated_user})
57 def user_toggle_activation(conn, %{"nickname" => nickname}) do
58 user = User.get_by_nickname(nickname)
60 {:ok, updated_user} = User.deactivate(user, !user.info.deactivated)
63 |> json(AccountView.render("show.json", %{user: updated_user}))
66 def tag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
67 with {:ok, _} <- User.tag(nicknames, tags),
68 do: json_response(conn, :no_content, "")
71 def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
72 with {:ok, _} <- User.untag(nicknames, tags),
73 do: json_response(conn, :no_content, "")
76 def list_users(conn, params) do
77 {page, page_size} = page_params(params)
79 with {:ok, users, count} <- User.all_for_admin(page, page_size),
83 AccountView.render("index.json",
91 def search_users(%{assigns: %{user: admin}} = conn, %{"query" => query} = params) do
92 {page, page_size} = page_params(params)
94 with {:ok, users, count} <-
95 User.search_for_admin(query, %{
97 local: params["local"] == "true",
104 AccountView.render("index.json",
112 def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname})
113 when permission_group in ["moderator", "admin"] do
114 user = User.get_by_nickname(nickname)
118 |> Map.put("is_" <> permission_group, true)
120 info_cng = User.Info.admin_api_update(user.info, info)
124 |> Ecto.Changeset.change()
125 |> Ecto.Changeset.put_embed(:info, info_cng)
127 {:ok, _user} = User.update_and_set_cache(cng)
132 def right_add(conn, _) do
135 |> json(%{error: "No such permission_group"})
138 def right_get(conn, %{"nickname" => nickname}) do
139 user = User.get_by_nickname(nickname)
143 is_moderator: user.info.is_moderator,
144 is_admin: user.info.is_admin
149 %{assigns: %{user: %User{:nickname => admin_nickname}}} = conn,
151 "permission_group" => permission_group,
152 "nickname" => nickname
155 when permission_group in ["moderator", "admin"] do
156 if admin_nickname == nickname do
159 |> json(%{error: "You can't revoke your own admin status."})
161 user = User.get_by_nickname(nickname)
165 |> Map.put("is_" <> permission_group, false)
167 info_cng = User.Info.admin_api_update(user.info, info)
170 Ecto.Changeset.change(user)
171 |> Ecto.Changeset.put_embed(:info, info_cng)
173 {:ok, _user} = User.update_and_set_cache(cng)
179 def right_delete(conn, _) do
182 |> json(%{error: "No such permission_group"})
185 def set_activation_status(conn, %{"nickname" => nickname, "status" => status}) do
186 with {:ok, status} <- Ecto.Type.cast(:boolean, status),
187 %User{} = user <- User.get_by_nickname(nickname),
188 {:ok, _} <- User.deactivate(user, !status),
189 do: json_response(conn, :no_content, "")
192 def relay_follow(conn, %{"relay_url" => target}) do
193 with {:ok, _message} <- Relay.follow(target) do
203 def relay_unfollow(conn, %{"relay_url" => target}) do
204 with {:ok, _message} <- Relay.unfollow(target) do
214 @doc "Sends registration invite via email"
215 def email_invite(%{assigns: %{user: user}} = conn, %{"email" => email} = params) do
217 Pleroma.Config.get([:instance, :invites_enabled]) &&
218 !Pleroma.Config.get([:instance, :registrations_open]),
219 {:ok, invite_token} <- Pleroma.UserInviteToken.create_token(),
221 Pleroma.UserEmail.user_invitation_email(user, invite_token, email, params["name"]),
222 {:ok, _} <- Pleroma.Mailer.deliver(email) do
223 json_response(conn, :no_content, "")
227 @doc "Get a account registeration invite token (base64 string)"
228 def get_invite_token(conn, _params) do
229 {:ok, token} = Pleroma.UserInviteToken.create_token()
235 @doc "Get a password reset token (base64 string) for given nickname"
236 def get_password_reset(conn, %{"nickname" => nickname}) do
237 (%User{local: true} = user) = User.get_by_nickname(nickname)
238 {:ok, token} = Pleroma.PasswordResetToken.create_token(user)
244 def errors(conn, {:param_cast, _}) do
247 |> json("Invalid parameters")
250 def errors(conn, _) do
253 |> json("Something went wrong")
256 defp page_params(params) do
257 {get_page(params["page"]), get_page_size(params["page_size"])}
260 defp get_page(page_string) when is_nil(page_string), do: 1
262 defp get_page(page_string) do
263 case Integer.parse(page_string) do
269 defp get_page_size(page_size_string) when is_nil(page_size_string), do: @users_page_size
271 defp get_page_size(page_size_string) do
272 case Integer.parse(page_size_string) do
273 {page_size, _} -> page_size
274 :error -> @users_page_size