From 59ce7fedce51cff39165ff1bb4ce1fbd8b53f530 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Fri, 2 Nov 2018 08:15:09 +0100 Subject: [PATCH] Fix connection returns make generic right endpoint [AdminAPI] --- .../web/admin_api/admin_api_controller.ex | 54 ++++++++++++++++--- lib/pleroma/web/router.ex | 7 +-- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index 70c1caaec..a7be3611f 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -17,7 +17,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do end conn - |> send(200) + |> json(nickname) end def user_create( @@ -35,29 +35,71 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do User.register_changeset(%User{}, new_user) - Repo.insert!(user) + Repo.insert!(new_user) conn - |> send(200) + |> json(new_user.nickname) + end + + def right_add(conn, %{"right" => right, "nickname" => nickname}) + when right in ["moderator", "admin"] do + user = User.get_by_nickname(nickname) + + info = + user.info + |> Map.put("is_" <> right, true) + + cng = User.info_changeset(user, %{info: info}) + {:ok, user} = User.update_and_set_cache(cng) + + conn + |> json(user.info) + end + + def right_add(conn, _) do + conn + |> put_status(404) + |> json(%{error: "No such right"}) + end + + def right_delete(conn, %{"right" => right, "nickname" => nickname}) + when right in ["moderator", "admin"] do + user = User.get_by_nickname(nickname) + + info = + user.info + |> Map.put("is_" <> right, false) + + cng = User.info_changeset(user, %{info: info}) + {:ok, user} = User.update_and_set_cache(cng) + + conn + |> json(user.info) + end + + def right_delete(conn, _) do + conn + |> put_status(404) + |> json(%{error: "No such right"}) end def relay_follow(conn, %{"relay_url" => target}) do :ok = Relay.follow(target) conn - |> send(200) + |> json(target) end def relay_unfollow(conn, %{"relay_url" => target}) do :ok = Relay.unfollow(target) conn - |> send(200) + |> json(target) end @shortdoc "Get a account registeration invite token (base64 string)" def get_invite_token(conn, _params) do - {:ok, token} <- Pleroma.UserInviteToken.create_token() + {:ok, token} = Pleroma.UserInviteToken.create_token() conn |> json(token.token) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 1fa2625d0..4d9422970 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -99,11 +99,8 @@ defmodule Pleroma.Web.Router do delete("/user", AdminAPIController, :user_delete) post("/user", AdminAPIController, :user_create) - # Maybe put a "rights" endpoint instead? - post("/moderator", AdminAPIController, :moderator_make) - delete("/moderator", AdminAPIController, :moderator_unmake) - post("/admin", AdminAPIController, :admin_make) - delete("/admin", AdminAPIController, :admin_unmake) + post("/rights/:right/:nickname", AdminAPIController, :right_add) + delete("/rights/:right/:nickname", AdminAPIController, :right_delete) post("/relay", AdminAPIController, :relay_follow) delete("/relay", AdminAPIController, :relay_unfollow) -- 2.45.2