Fix admin api.

This commit is contained in:
lain 2018-12-01 09:03:16 +01:00
parent d0ec2812bd
commit 1c67277c80
5 changed files with 50 additions and 29 deletions

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Plugs.UserIsAdminPlug do
options options
end end
def call(%{assigns: %{user: %User{info: %{"is_admin" => true}}}} = conn, _) do def call(%{assigns: %{user: %User{info: %{is_admin: true}}}} = conn, _) do
conn conn
end end

View file

@ -16,6 +16,7 @@ defmodule Pleroma.User.Info do
field(:no_rich_text, :boolean, default: false) field(:no_rich_text, :boolean, default: false)
field(:ap_enabled, :boolean, default: false) field(:ap_enabled, :boolean, default: false)
field(:is_moderator, :boolean, default: false) field(:is_moderator, :boolean, default: false)
field(:is_admin, :boolean, default: false)
field(:keys, :string, default: nil) field(:keys, :string, default: nil)
field(:settings, :map, default: nil) field(:settings, :map, default: nil)
field(:magic_key, :string, default: nil) field(:magic_key, :string, default: nil)
@ -134,4 +135,12 @@ defmodule Pleroma.User.Info do
|> cast(params, [:source_data]) |> cast(params, [:source_data])
|> validate_required([:source_data]) |> validate_required([:source_data])
end end
def admin_api_update(info, params) do
info
|> cast(params, [
:is_moderator,
:is_admin
])
end
end end

View file

@ -45,21 +45,29 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
user = User.get_by_nickname(nickname) user = User.get_by_nickname(nickname)
info = info =
user.info %{}
|> Map.put("is_" <> permission_group, true) |> Map.put("is_" <> permission_group, true)
cng = User.info_changeset(user, %{info: info}) info_cng = User.Info.admin_api_update(user.info, info)
cng =
Ecto.Changeset.change(user)
|> Ecto.Changeset.put_embed(:info, info_cng)
{:ok, user} = User.update_and_set_cache(cng) {:ok, user} = User.update_and_set_cache(cng)
conn conn
|> json(user.info) |> json(info)
end end
def right_get(conn, %{"nickname" => nickname}) do def right_get(conn, %{"nickname" => nickname}) do
user = User.get_by_nickname(nickname) user = User.get_by_nickname(nickname)
conn conn
|> json(user.info) |> json(%{
is_moderator: user.info.is_moderator,
is_admin: user.info.is_admin
})
end end
def right_add(conn, _) do def right_add(conn, _) do
@ -84,14 +92,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
user = User.get_by_nickname(nickname) user = User.get_by_nickname(nickname)
info = info =
user.info %{}
|> Map.put("is_" <> permission_group, false) |> Map.put("is_" <> permission_group, false)
cng = User.info_changeset(user, %{info: info}) info_cng = User.Info.admin_api_update(user.info, info)
cng =
Ecto.Changeset.change(user)
|> Ecto.Changeset.put_embed(:info, info_cng)
{:ok, user} = User.update_and_set_cache(cng) {:ok, user} = User.update_and_set_cache(cng)
conn conn
|> json(user.info) |> json(info)
end end
end end

View file

@ -5,7 +5,7 @@ defmodule Pleroma.Plugs.UserIsAdminPlugTest do
import Pleroma.Factory import Pleroma.Factory
test "accepts a user that is admin", %{conn: conn} do test "accepts a user that is admin", %{conn: conn} do
user = insert(:user, info: %{"is_admin" => true}) user = insert(:user, info: %{is_admin: true})
conn = conn =
build_conn() build_conn()

View file

@ -8,7 +8,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
describe "/api/pleroma/admin/user" do describe "/api/pleroma/admin/user" do
test "Delete" do test "Delete" do
admin = insert(:user, info: %{"is_admin" => true}) admin = insert(:user, info: %{is_admin: true})
user = insert(:user) user = insert(:user)
conn = conn =
@ -21,7 +21,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end end
test "Create" do test "Create" do
admin = insert(:user, info: %{"is_admin" => true}) admin = insert(:user, info: %{is_admin: true})
conn = conn =
build_conn() build_conn()
@ -39,7 +39,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
describe "/api/pleroma/admin/permission_group" do describe "/api/pleroma/admin/permission_group" do
test "GET is giving user_info" do test "GET is giving user_info" do
admin = insert(:user, info: %{"is_admin" => true}) admin = insert(:user, info: %{is_admin: true})
conn = conn =
build_conn() build_conn()
@ -47,33 +47,30 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|> put_req_header("accept", "application/json") |> put_req_header("accept", "application/json")
|> get("/api/pleroma/admin/permission_group/#{admin.nickname}") |> get("/api/pleroma/admin/permission_group/#{admin.nickname}")
assert json_response(conn, 200) == admin.info assert json_response(conn, 200) == %{
"is_admin" => true,
"is_moderator" => false
}
end end
test "/:right POST, can add to a permission group" do test "/:right POST, can add to a permission group" do
admin = insert(:user, info: %{"is_admin" => true}) admin = insert(:user, info: %{is_admin: true})
user = insert(:user) user = insert(:user)
user_info =
user.info
|> Map.put("is_admin", true)
conn = conn =
build_conn() build_conn()
|> assign(:user, admin) |> assign(:user, admin)
|> put_req_header("accept", "application/json") |> put_req_header("accept", "application/json")
|> post("/api/pleroma/admin/permission_group/#{user.nickname}/admin") |> post("/api/pleroma/admin/permission_group/#{user.nickname}/admin")
assert json_response(conn, 200) == user_info assert json_response(conn, 200) == %{
"is_admin" => true
}
end end
test "/:right DELETE, can remove from a permission group" do test "/:right DELETE, can remove from a permission group" do
admin = insert(:user, info: %{"is_admin" => true}) admin = insert(:user, info: %{is_admin: true})
user = insert(:user, info: %{"is_admin" => true}) user = insert(:user, info: %{is_admin: true})
user_info =
user.info
|> Map.put("is_admin", false)
conn = conn =
build_conn() build_conn()
@ -81,12 +78,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|> put_req_header("accept", "application/json") |> put_req_header("accept", "application/json")
|> delete("/api/pleroma/admin/permission_group/#{user.nickname}/admin") |> delete("/api/pleroma/admin/permission_group/#{user.nickname}/admin")
assert json_response(conn, 200) == user_info assert json_response(conn, 200) == %{
"is_admin" => false
}
end end
end end
test "/api/pleroma/admin/invite_token" do test "/api/pleroma/admin/invite_token" do
admin = insert(:user, info: %{"is_admin" => true}) admin = insert(:user, info: %{is_admin: true})
conn = conn =
build_conn() build_conn()
@ -98,8 +97,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end end
test "/api/pleroma/admin/password_reset" do test "/api/pleroma/admin/password_reset" do
admin = insert(:user, info: %{"is_admin" => true}) admin = insert(:user, info: %{is_admin: true})
user = insert(:user, info: %{"is_admin" => true}) user = insert(:user)
conn = conn =
build_conn() build_conn()