1 defmodule Pleroma.Web.AdminAPI.AdminAPIController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{User, Repo}
4 alias Pleroma.Web.ActivityPub.Relay
6 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
10 action_fallback(:errors)
12 def user_delete(conn, %{"nickname" => nickname}) do
13 user = User.get_by_nickname(nickname)
15 if user.local == true do
27 %{"nickname" => nickname, "email" => email, "password" => password}
34 password_confirmation: password,
38 User.register_changeset(%User{}, new_user)
42 |> json(new_user.nickname)
45 def tag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
46 with {:ok, _} <- User.tag(nicknames, tags),
47 do: json_response(conn, :no_content, "")
50 def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
51 with {:ok, _} <- User.untag(nicknames, tags),
52 do: json_response(conn, :no_content, "")
55 def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname})
56 when permission_group in ["moderator", "admin"] do
57 user = User.get_by_nickname(nickname)
61 |> Map.put("is_" <> permission_group, true)
63 info_cng = User.Info.admin_api_update(user.info, info)
67 |> Ecto.Changeset.change()
68 |> Ecto.Changeset.put_embed(:info, info_cng)
70 {:ok, _user} = User.update_and_set_cache(cng)
75 def right_add(conn, _) do
78 |> json(%{error: "No such permission_group"})
81 def right_get(conn, %{"nickname" => nickname}) do
82 user = User.get_by_nickname(nickname)
86 is_moderator: user.info.is_moderator,
87 is_admin: user.info.is_admin
92 %{assigns: %{user: %User{:nickname => admin_nickname}}} = conn,
94 "permission_group" => permission_group,
95 "nickname" => nickname
98 when permission_group in ["moderator", "admin"] do
99 if admin_nickname == nickname do
102 |> json(%{error: "You can't revoke your own admin status."})
104 user = User.get_by_nickname(nickname)
108 |> Map.put("is_" <> permission_group, false)
110 info_cng = User.Info.admin_api_update(user.info, info)
113 Ecto.Changeset.change(user)
114 |> Ecto.Changeset.put_embed(:info, info_cng)
116 {:ok, _user} = User.update_and_set_cache(cng)
122 def right_delete(conn, _) do
125 |> json(%{error: "No such permission_group"})
128 def relay_follow(conn, %{"relay_url" => target}) do
129 with {:ok, _message} <- Relay.follow(target) do
139 def relay_unfollow(conn, %{"relay_url" => target}) do
140 with {:ok, _message} <- Relay.unfollow(target) do
150 @doc "Sends registration invite via email"
151 def email_invite(%{assigns: %{user: user}} = conn, %{"email" => email} = params) do
153 Pleroma.Config.get([:instance, :invites_enabled]) &&
154 !Pleroma.Config.get([:instance, :registrations_open]),
155 {:ok, invite_token} <- Pleroma.UserInviteToken.create_token(),
157 Pleroma.UserEmail.user_invitation_email(user, invite_token, email, params["name"]),
158 {:ok, _} <- Pleroma.Mailer.deliver(email) do
159 json_response(conn, :no_content, "")
163 @doc "Get a account registeration invite token (base64 string)"
164 def get_invite_token(conn, _params) do
165 {:ok, token} = Pleroma.UserInviteToken.create_token()
171 @doc "Get a password reset token (base64 string) for given nickname"
172 def get_password_reset(conn, %{"nickname" => nickname}) do
173 (%User{local: true} = user) = User.get_by_nickname(nickname)
174 {:ok, token} = Pleroma.PasswordResetToken.create_token(user)
180 def errors(conn, {:param_cast, _}) do
183 |> json("Invalid parameters")
186 def errors(conn, _) do
189 |> json("Something went wrong")