lib/pleroma/web/admin_api/admin_api_controller.ex: An admin cannot un-admin themselves
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Sat, 10 Nov 2018 13:42:34 +0000 (14:42 +0100)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Sat, 17 Nov 2018 19:25:56 +0000 (20:25 +0100)
lib/pleroma/web/admin_api/admin_api_controller.ex

index cb9839324f6d8126eb9bb66ffe5a3c5933492092..c1df2d5703be116e19f2c5c0de17d1795700f400 100644 (file)
@@ -68,19 +68,31 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     |> json(%{error: "No such right"})
   end
 
-  def right_delete(conn, %{"right" => right, "nickname" => nickname})
+  def right_delete(
+        %{assigns: %{user: %User{:nickname => admin_nickname}}} = conn,
+        %{
+          "right" => right,
+          "nickname" => nickname
+        }
+      )
       when right in ["moderator", "admin"] do
-    user = User.get_by_nickname(nickname)
+    if admin_nickname == nickname do
+      conn
+      |> post_status(403)
+      |> json(%{error: "You can't revoke your own admin status."})
+    else
+      user = User.get_by_nickname(nickname)
 
-    info =
-      user.info
-      |> Map.put("is_" <> right, false)
+      info =
+        user.info
+        |> Map.put("is_" <> right, false)
 
-    cng = User.info_changeset(user, %{info: info})
-    {:ok, user} = User.update_and_set_cache(cng)
+      cng = User.info_changeset(user, %{info: info})
+      {:ok, user} = User.update_and_set_cache(cng)
 
-    conn
-    |> json(user.info)
+      conn
+      |> json(user.info)
+    end
   end
 
   def right_delete(conn, _) do