admin_api_controller.ex 4.12 KB
Newer Older
1
defmodule Pleroma.Web.AdminAPI.AdminAPIController do
Haelwenn's avatar
Haelwenn committed
2
  use Pleroma.Web, :controller
3 4
  alias Pleroma.{User, Repo}
  alias Pleroma.Web.ActivityPub.Relay
Haelwenn's avatar
Haelwenn committed
5

6 7
  import Pleroma.Web.ControllerHelper, only: [json_response: 3]

Haelwenn's avatar
Haelwenn committed
8 9 10 11
  require Logger

  action_fallback(:errors)

12
  def user_delete(conn, %{"nickname" => nickname}) do
13 14
    user = User.get_by_nickname(nickname)

15
    if user.local == true do
16 17 18 19 20 21
      User.delete(user)
    else
      User.delete(user)
    end

    conn
22
    |> json(nickname)
Haelwenn's avatar
Haelwenn committed
23 24
  end

25 26 27 28 29
  def user_create(
        conn,
        %{"nickname" => nickname, "email" => email, "password" => password}
      ) do
    new_user = %{
30
      nickname: nickname,
31
      name: nickname,
32 33 34
      email: email,
      password: password,
      password_confirmation: password,
35
      bio: "."
36 37 38
    }

    User.register_changeset(%User{}, new_user)
39
    |> Repo.insert!()
40 41

    conn
42 43 44
    |> json(new_user.nickname)
  end

45 46 47 48 49 50 51 52 53 54
  def tag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
    with {:ok, _} <- User.tag(nicknames, tags),
         do: json_response(conn, :no_content, "")
  end

  def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
    with {:ok, _} <- User.untag(nicknames, tags),
         do: json_response(conn, :no_content, "")
  end

55 56
  def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname})
      when permission_group in ["moderator", "admin"] do
57 58 59
    user = User.get_by_nickname(nickname)

    info =
lain's avatar
lain committed
60
      %{}
61
      |> Map.put("is_" <> permission_group, true)
62

lain's avatar
lain committed
63 64 65 66 67 68
    info_cng = User.Info.admin_api_update(user.info, info)

    cng =
      Ecto.Changeset.change(user)
      |> Ecto.Changeset.put_embed(:info, info_cng)

69 70 71
    {:ok, user} = User.update_and_set_cache(cng)

    conn
lain's avatar
lain committed
72
    |> json(info)
73 74
  end

75 76 77 78
  def right_get(conn, %{"nickname" => nickname}) do
    user = User.get_by_nickname(nickname)

    conn
lain's avatar
lain committed
79 80 81 82
    |> json(%{
      is_moderator: user.info.is_moderator,
      is_admin: user.info.is_admin
    })
83 84
  end

85 86 87
  def right_add(conn, _) do
    conn
    |> put_status(404)
88
    |> json(%{error: "No such permission_group"})
89 90
  end

91 92 93
  def right_delete(
        %{assigns: %{user: %User{:nickname => admin_nickname}}} = conn,
        %{
94
          "permission_group" => permission_group,
95 96 97
          "nickname" => nickname
        }
      )
98
      when permission_group in ["moderator", "admin"] do
99 100
    if admin_nickname == nickname do
      conn
101
      |> put_status(403)
102 103 104
      |> json(%{error: "You can't revoke your own admin status."})
    else
      user = User.get_by_nickname(nickname)
105

106
      info =
lain's avatar
lain committed
107
        %{}
108
        |> Map.put("is_" <> permission_group, false)
109

lain's avatar
lain committed
110 111 112 113 114 115
      info_cng = User.Info.admin_api_update(user.info, info)

      cng =
        Ecto.Changeset.change(user)
        |> Ecto.Changeset.put_embed(:info, info_cng)

116
      {:ok, user} = User.update_and_set_cache(cng)
117

118
      conn
lain's avatar
lain committed
119
      |> json(info)
120
    end
121 122 123 124 125
  end

  def right_delete(conn, _) do
    conn
    |> put_status(404)
126
    |> json(%{error: "No such permission_group"})
Haelwenn's avatar
Haelwenn committed
127 128
  end

129
  def relay_follow(conn, %{"relay_url" => target}) do
130
    {status, message} = Relay.follow(target)
131

132 133 134 135 136 137 138 139
    if status == :ok do
      conn
      |> json(target)
    else
      conn
      |> put_status(500)
      |> json(target)
    end
Haelwenn's avatar
Haelwenn committed
140 141
  end

142
  def relay_unfollow(conn, %{"relay_url" => target}) do
143
    {status, message} = Relay.unfollow(target)
144

145 146 147 148 149 150 151 152
    if status == :ok do
      conn
      |> json(target)
    else
      conn
      |> put_status(500)
      |> json(target)
    end
Haelwenn's avatar
Haelwenn committed
153 154
  end

155
  @shortdoc "Get a account registeration invite token (base64 string)"
156
  def get_invite_token(conn, _params) do
157
    {:ok, token} = Pleroma.UserInviteToken.create_token()
158 159

    conn
160
    |> json(token.token)
Haelwenn's avatar
Haelwenn committed
161 162
  end

163
  @shortdoc "Get a password reset token (base64 string) for given nickname"
164
  def get_password_reset(conn, %{"nickname" => nickname}) do
165 166 167 168
    (%User{local: true} = user) = User.get_by_nickname(nickname)
    {:ok, token} = Pleroma.PasswordResetToken.create_token(user)

    conn
169 170 171 172 173 174 175 176 177 178 179 180 181
    |> json(token.token)
  end

  def errors(conn, {:param_cast, _}) do
    conn
    |> put_status(400)
    |> json("Invalid parameters")
  end

  def errors(conn, _) do
    conn
    |> put_status(500)
    |> json("Something went wrong")
Haelwenn's avatar
Haelwenn committed
182 183
  end
end