From 1c67277c8031be2212d6faf84c143ac11462c87b Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 1 Dec 2018 09:03:16 +0100 Subject: [PATCH] Fix admin api. --- lib/pleroma/plugs/user_is_admin_plug.ex | 2 +- lib/pleroma/user/info.ex | 9 +++++ .../web/admin_api/admin_api_controller.ex | 27 +++++++++---- test/plugs/user_is_admin_plug_test.exs | 2 +- .../admin_api/admin_api_controller_test.exs | 39 +++++++++---------- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/lib/pleroma/plugs/user_is_admin_plug.ex b/lib/pleroma/plugs/user_is_admin_plug.ex index 5312f1499..cf22ce5d0 100644 --- a/lib/pleroma/plugs/user_is_admin_plug.ex +++ b/lib/pleroma/plugs/user_is_admin_plug.ex @@ -6,7 +6,7 @@ defmodule Pleroma.Plugs.UserIsAdminPlug do options end - def call(%{assigns: %{user: %User{info: %{"is_admin" => true}}}} = conn, _) do + def call(%{assigns: %{user: %User{info: %{is_admin: true}}}} = conn, _) do conn end diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 4e2c5472d..a5ce6010f 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -16,6 +16,7 @@ defmodule Pleroma.User.Info do field(:no_rich_text, :boolean, default: false) field(:ap_enabled, :boolean, default: false) field(:is_moderator, :boolean, default: false) + field(:is_admin, :boolean, default: false) field(:keys, :string, default: nil) field(:settings, :map, default: nil) field(:magic_key, :string, default: nil) @@ -134,4 +135,12 @@ defmodule Pleroma.User.Info do |> cast(params, [:source_data]) |> validate_required([:source_data]) end + + def admin_api_update(info, params) do + info + |> cast(params, [ + :is_moderator, + :is_admin + ]) + end end diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index bcdb4ba37..2c67d9cda 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -45,21 +45,29 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do user = User.get_by_nickname(nickname) info = - user.info + %{} |> 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) conn - |> json(user.info) + |> json(info) end def right_get(conn, %{"nickname" => nickname}) do user = User.get_by_nickname(nickname) conn - |> json(user.info) + |> json(%{ + is_moderator: user.info.is_moderator, + is_admin: user.info.is_admin + }) end def right_add(conn, _) do @@ -84,14 +92,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do user = User.get_by_nickname(nickname) info = - user.info + %{} |> 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) conn - |> json(user.info) + |> json(info) end end diff --git a/test/plugs/user_is_admin_plug_test.exs b/test/plugs/user_is_admin_plug_test.exs index ddf9eb139..031b2f466 100644 --- a/test/plugs/user_is_admin_plug_test.exs +++ b/test/plugs/user_is_admin_plug_test.exs @@ -5,7 +5,7 @@ defmodule Pleroma.Plugs.UserIsAdminPlugTest do import Pleroma.Factory 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 = build_conn() diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index fa0cb71bf..9634ad7c5 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -8,7 +8,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do describe "/api/pleroma/admin/user" do test "Delete" do - admin = insert(:user, info: %{"is_admin" => true}) + admin = insert(:user, info: %{is_admin: true}) user = insert(:user) conn = @@ -21,7 +21,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end test "Create" do - admin = insert(:user, info: %{"is_admin" => true}) + admin = insert(:user, info: %{is_admin: true}) conn = build_conn() @@ -39,7 +39,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do describe "/api/pleroma/admin/permission_group" do test "GET is giving user_info" do - admin = insert(:user, info: %{"is_admin" => true}) + admin = insert(:user, info: %{is_admin: true}) conn = build_conn() @@ -47,33 +47,30 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do |> put_req_header("accept", "application/json") |> 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 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_info = - user.info - |> Map.put("is_admin", true) - conn = build_conn() |> assign(:user, admin) |> put_req_header("accept", "application/json") |> 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 test "/:right DELETE, can remove from a permission group" do - admin = insert(:user, info: %{"is_admin" => true}) - user = insert(:user, info: %{"is_admin" => true}) - - user_info = - user.info - |> Map.put("is_admin", false) + admin = insert(:user, info: %{is_admin: true}) + user = insert(:user, info: %{is_admin: true}) conn = build_conn() @@ -81,12 +78,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do |> put_req_header("accept", "application/json") |> 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 test "/api/pleroma/admin/invite_token" do - admin = insert(:user, info: %{"is_admin" => true}) + admin = insert(:user, info: %{is_admin: true}) conn = build_conn() @@ -98,8 +97,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end test "/api/pleroma/admin/password_reset" do - admin = insert(:user, info: %{"is_admin" => true}) - user = insert(:user, info: %{"is_admin" => true}) + admin = insert(:user, info: %{is_admin: true}) + user = insert(:user) conn = build_conn()