admin_api_controller.ex 4.08 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)

Maksim's avatar
Maksim committed
69
    {:ok, _user} = User.update_and_set_cache(cng)
70

Maksim's avatar
Maksim committed
71
72
73
74
    json(conn, info)
  end

  def right_add(conn, _) do
75
    conn
Maksim's avatar
Maksim committed
76
77
    |> put_status(404)
    |> json(%{error: "No such permission_group"})
78
79
  end

80
81
82
83
  def right_get(conn, %{"nickname" => nickname}) do
    user = User.get_by_nickname(nickname)

    conn
lain's avatar
lain committed
84
85
86
87
    |> json(%{
      is_moderator: user.info.is_moderator,
      is_admin: user.info.is_admin
    })
88
89
  end

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

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

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

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

Maksim's avatar
Maksim committed
115
      {:ok, _user} = User.update_and_set_cache(cng)
116

Maksim's avatar
Maksim committed
117
      json(conn, info)
118
    end
119
120
121
122
123
  end

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

127
  def relay_follow(conn, %{"relay_url" => target}) do
Maksim's avatar
Maksim committed
128
129
    with {:ok, _message} <- Relay.follow(target) do
      json(conn, target)
130
    else
Maksim's avatar
Maksim committed
131
132
133
134
      _ ->
        conn
        |> put_status(500)
        |> json(target)
135
    end
Haelwenn's avatar
Haelwenn committed
136
137
  end

138
  def relay_unfollow(conn, %{"relay_url" => target}) do
Maksim's avatar
Maksim committed
139
140
    with {:ok, _message} <- Relay.unfollow(target) do
      json(conn, target)
141
    else
Maksim's avatar
Maksim committed
142
143
144
145
      _ ->
        conn
        |> put_status(500)
        |> json(target)
146
    end
Haelwenn's avatar
Haelwenn committed
147
148
  end

Maksim's avatar
Maksim committed
149
  @doc "Get a account registeration invite token (base64 string)"
150
  def get_invite_token(conn, _params) do
151
    {:ok, token} = Pleroma.UserInviteToken.create_token()
152
153

    conn
154
    |> json(token.token)
Haelwenn's avatar
Haelwenn committed
155
156
  end

Maksim's avatar
Maksim committed
157
  @doc "Get a password reset token (base64 string) for given nickname"
158
  def get_password_reset(conn, %{"nickname" => nickname}) do
159
160
161
162
    (%User{local: true} = user) = User.get_by_nickname(nickname)
    {:ok, token} = Pleroma.PasswordResetToken.create_token(user)

    conn
163
164
165
166
167
168
169
170
171
172
173
174
175
    |> 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
176
177
  end
end