Merge branch 'moderators' into 'develop'
AdminAPI: let moderators actually do things See merge request soapbox-pub/soapbox!39
This commit is contained in:
commit
6dad0310f3
4 changed files with 129 additions and 46 deletions
23
lib/pleroma/web/plugs/user_is_staff_plug.ex
Normal file
23
lib/pleroma/web/plugs/user_is_staff_plug.ex
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.Plugs.UserIsStaffPlug do
|
||||||
|
import Pleroma.Web.TranslationHelpers
|
||||||
|
import Plug.Conn
|
||||||
|
|
||||||
|
alias Pleroma.User
|
||||||
|
|
||||||
|
def init(options) do
|
||||||
|
options
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(%{assigns: %{user: %User{is_admin: true}}} = conn, _), do: conn
|
||||||
|
def call(%{assigns: %{user: %User{is_moderator: true}}} = conn, _), do: conn
|
||||||
|
|
||||||
|
def call(conn, _) do
|
||||||
|
conn
|
||||||
|
|> render_error(:forbidden, "User is not a staff member.")
|
||||||
|
|> halt()
|
||||||
|
end
|
||||||
|
end
|
|
@ -96,10 +96,14 @@ defmodule Pleroma.Web.Router do
|
||||||
plug(Pleroma.Web.Plugs.AdminSecretAuthenticationPlug)
|
plug(Pleroma.Web.Plugs.AdminSecretAuthenticationPlug)
|
||||||
plug(:after_auth)
|
plug(:after_auth)
|
||||||
plug(Pleroma.Web.Plugs.EnsureAuthenticatedPlug)
|
plug(Pleroma.Web.Plugs.EnsureAuthenticatedPlug)
|
||||||
plug(Pleroma.Web.Plugs.UserIsAdminPlug)
|
plug(Pleroma.Web.Plugs.UserIsStaffPlug)
|
||||||
plug(Pleroma.Web.Plugs.IdempotencyPlug)
|
plug(Pleroma.Web.Plugs.IdempotencyPlug)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
pipeline :require_admin do
|
||||||
|
plug(Pleroma.Web.Plugs.UserIsAdminPlug)
|
||||||
|
end
|
||||||
|
|
||||||
pipeline :mastodon_html do
|
pipeline :mastodon_html do
|
||||||
plug(:browser)
|
plug(:browser)
|
||||||
plug(:authenticate)
|
plug(:authenticate)
|
||||||
|
@ -159,12 +163,11 @@ defmodule Pleroma.Web.Router do
|
||||||
post("/uploader_callback/:upload_path", UploaderController, :callback)
|
post("/uploader_callback/:upload_path", UploaderController, :callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# AdminAPI: only admins can perform these actions
|
||||||
scope "/api/v1/pleroma/admin", Pleroma.Web.AdminAPI do
|
scope "/api/v1/pleroma/admin", Pleroma.Web.AdminAPI do
|
||||||
pipe_through(:admin_api)
|
pipe_through([:admin_api, :require_admin])
|
||||||
|
|
||||||
put("/users/disable_mfa", AdminAPIController, :disable_mfa)
|
put("/users/disable_mfa", AdminAPIController, :disable_mfa)
|
||||||
put("/users/tag", AdminAPIController, :tag_users)
|
|
||||||
delete("/users/tag", AdminAPIController, :untag_users)
|
|
||||||
|
|
||||||
get("/users/:nickname/permission_group", AdminAPIController, :right_get)
|
get("/users/:nickname/permission_group", AdminAPIController, :right_get)
|
||||||
get("/users/:nickname/permission_group/:permission_group", AdminAPIController, :right_get)
|
get("/users/:nickname/permission_group/:permission_group", AdminAPIController, :right_get)
|
||||||
|
@ -187,34 +190,17 @@ defmodule Pleroma.Web.Router do
|
||||||
|
|
||||||
post("/users/follow", UserController, :follow)
|
post("/users/follow", UserController, :follow)
|
||||||
post("/users/unfollow", UserController, :unfollow)
|
post("/users/unfollow", UserController, :unfollow)
|
||||||
delete("/users", UserController, :delete)
|
|
||||||
post("/users", UserController, :create)
|
post("/users", UserController, :create)
|
||||||
patch("/users/:nickname/toggle_activation", UserController, :toggle_activation)
|
|
||||||
patch("/users/activate", UserController, :activate)
|
|
||||||
patch("/users/deactivate", UserController, :deactivate)
|
|
||||||
patch("/users/approve", UserController, :approve)
|
|
||||||
|
|
||||||
get("/relay", RelayController, :index)
|
get("/relay", RelayController, :index)
|
||||||
post("/relay", RelayController, :follow)
|
post("/relay", RelayController, :follow)
|
||||||
delete("/relay", RelayController, :unfollow)
|
delete("/relay", RelayController, :unfollow)
|
||||||
|
|
||||||
post("/users/invite_token", InviteController, :create)
|
|
||||||
get("/users/invites", InviteController, :index)
|
|
||||||
post("/users/revoke_invite", InviteController, :revoke)
|
|
||||||
post("/users/email_invite", InviteController, :email)
|
|
||||||
|
|
||||||
get("/users/:nickname/password_reset", AdminAPIController, :get_password_reset)
|
get("/users/:nickname/password_reset", AdminAPIController, :get_password_reset)
|
||||||
patch("/users/force_password_reset", AdminAPIController, :force_password_reset)
|
patch("/users/force_password_reset", AdminAPIController, :force_password_reset)
|
||||||
get("/users/:nickname/credentials", AdminAPIController, :show_user_credentials)
|
get("/users/:nickname/credentials", AdminAPIController, :show_user_credentials)
|
||||||
patch("/users/:nickname/credentials", AdminAPIController, :update_user_credentials)
|
patch("/users/:nickname/credentials", AdminAPIController, :update_user_credentials)
|
||||||
|
|
||||||
get("/users", UserController, :list)
|
|
||||||
get("/users/:nickname", UserController, :show)
|
|
||||||
get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses)
|
|
||||||
get("/users/:nickname/chats", AdminAPIController, :list_user_chats)
|
|
||||||
|
|
||||||
get("/instances/:instance/statuses", AdminAPIController, :list_instance_statuses)
|
|
||||||
|
|
||||||
get("/instance_document/:name", InstanceDocumentController, :show)
|
get("/instance_document/:name", InstanceDocumentController, :show)
|
||||||
patch("/instance_document/:name", InstanceDocumentController, :update)
|
patch("/instance_document/:name", InstanceDocumentController, :update)
|
||||||
delete("/instance_document/:name", InstanceDocumentController, :delete)
|
delete("/instance_document/:name", InstanceDocumentController, :delete)
|
||||||
|
@ -222,6 +208,57 @@ defmodule Pleroma.Web.Router do
|
||||||
patch("/users/confirm_email", AdminAPIController, :confirm_email)
|
patch("/users/confirm_email", AdminAPIController, :confirm_email)
|
||||||
patch("/users/resend_confirmation_email", AdminAPIController, :resend_confirmation_email)
|
patch("/users/resend_confirmation_email", AdminAPIController, :resend_confirmation_email)
|
||||||
|
|
||||||
|
get("/config", ConfigController, :show)
|
||||||
|
post("/config", ConfigController, :update)
|
||||||
|
get("/config/descriptions", ConfigController, :descriptions)
|
||||||
|
get("/need_reboot", AdminAPIController, :need_reboot)
|
||||||
|
get("/restart", AdminAPIController, :restart)
|
||||||
|
|
||||||
|
get("/oauth_app", OAuthAppController, :index)
|
||||||
|
post("/oauth_app", OAuthAppController, :create)
|
||||||
|
patch("/oauth_app/:id", OAuthAppController, :update)
|
||||||
|
delete("/oauth_app/:id", OAuthAppController, :delete)
|
||||||
|
|
||||||
|
get("/media_proxy_caches", MediaProxyCacheController, :index)
|
||||||
|
post("/media_proxy_caches/delete", MediaProxyCacheController, :delete)
|
||||||
|
post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
|
||||||
|
|
||||||
|
get("/frontends", FrontendController, :index)
|
||||||
|
post("/frontends/install", FrontendController, :install)
|
||||||
|
|
||||||
|
post("/backups", AdminAPIController, :create_backup)
|
||||||
|
|
||||||
|
get("/email_list/subscribers.csv", EmailListController, :subscribers)
|
||||||
|
get("/email_list/unsubscribers.csv", EmailListController, :unsubscribers)
|
||||||
|
get("/email_list/combined.csv", EmailListController, :combined)
|
||||||
|
end
|
||||||
|
|
||||||
|
# AdminAPI: admins and mods (staff) can perform these actions
|
||||||
|
scope "/api/v1/pleroma/admin", Pleroma.Web.AdminAPI do
|
||||||
|
pipe_through(:admin_api)
|
||||||
|
|
||||||
|
put("/users/tag", AdminAPIController, :tag_users)
|
||||||
|
delete("/users/tag", AdminAPIController, :untag_users)
|
||||||
|
|
||||||
|
patch("/users/:nickname/toggle_activation", UserController, :toggle_activation)
|
||||||
|
patch("/users/activate", UserController, :activate)
|
||||||
|
patch("/users/deactivate", UserController, :deactivate)
|
||||||
|
patch("/users/approve", UserController, :approve)
|
||||||
|
|
||||||
|
delete("/users", UserController, :delete)
|
||||||
|
|
||||||
|
post("/users/invite_token", InviteController, :create)
|
||||||
|
get("/users/invites", InviteController, :index)
|
||||||
|
post("/users/revoke_invite", InviteController, :revoke)
|
||||||
|
post("/users/email_invite", InviteController, :email)
|
||||||
|
|
||||||
|
get("/users", UserController, :list)
|
||||||
|
get("/users/:nickname", UserController, :show)
|
||||||
|
get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses)
|
||||||
|
get("/users/:nickname/chats", AdminAPIController, :list_user_chats)
|
||||||
|
|
||||||
|
get("/instances/:instance/statuses", AdminAPIController, :list_instance_statuses)
|
||||||
|
|
||||||
get("/reports", ReportController, :index)
|
get("/reports", ReportController, :index)
|
||||||
get("/reports/:id", ReportController, :show)
|
get("/reports/:id", ReportController, :show)
|
||||||
patch("/reports", ReportController, :update)
|
patch("/reports", ReportController, :update)
|
||||||
|
@ -233,38 +270,14 @@ defmodule Pleroma.Web.Router do
|
||||||
delete("/statuses/:id", StatusController, :delete)
|
delete("/statuses/:id", StatusController, :delete)
|
||||||
get("/statuses", StatusController, :index)
|
get("/statuses", StatusController, :index)
|
||||||
|
|
||||||
get("/config", ConfigController, :show)
|
|
||||||
post("/config", ConfigController, :update)
|
|
||||||
get("/config/descriptions", ConfigController, :descriptions)
|
|
||||||
get("/need_reboot", AdminAPIController, :need_reboot)
|
|
||||||
get("/restart", AdminAPIController, :restart)
|
|
||||||
|
|
||||||
get("/moderation_log", AdminAPIController, :list_log)
|
get("/moderation_log", AdminAPIController, :list_log)
|
||||||
|
|
||||||
post("/reload_emoji", AdminAPIController, :reload_emoji)
|
post("/reload_emoji", AdminAPIController, :reload_emoji)
|
||||||
get("/stats", AdminAPIController, :stats)
|
get("/stats", AdminAPIController, :stats)
|
||||||
|
|
||||||
get("/oauth_app", OAuthAppController, :index)
|
|
||||||
post("/oauth_app", OAuthAppController, :create)
|
|
||||||
patch("/oauth_app/:id", OAuthAppController, :update)
|
|
||||||
delete("/oauth_app/:id", OAuthAppController, :delete)
|
|
||||||
|
|
||||||
get("/media_proxy_caches", MediaProxyCacheController, :index)
|
|
||||||
post("/media_proxy_caches/delete", MediaProxyCacheController, :delete)
|
|
||||||
post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
|
|
||||||
|
|
||||||
get("/chats/:id", ChatController, :show)
|
get("/chats/:id", ChatController, :show)
|
||||||
get("/chats/:id/messages", ChatController, :messages)
|
get("/chats/:id/messages", ChatController, :messages)
|
||||||
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
|
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
|
||||||
|
|
||||||
get("/frontends", FrontendController, :index)
|
|
||||||
post("/frontends/install", FrontendController, :install)
|
|
||||||
|
|
||||||
post("/backups", AdminAPIController, :create_backup)
|
|
||||||
|
|
||||||
get("/email_list/subscribers.csv", EmailListController, :subscribers)
|
|
||||||
get("/email_list/unsubscribers.csv", EmailListController, :unsubscribers)
|
|
||||||
get("/email_list/combined.csv", EmailListController, :combined)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do
|
scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do
|
||||||
|
|
|
@ -305,7 +305,7 @@ test "returns 403 when requested by a non-admin" do
|
||||||
|> get("/api/pleroma/admin/reports")
|
|> get("/api/pleroma/admin/reports")
|
||||||
|
|
||||||
assert json_response(conn, :forbidden) ==
|
assert json_response(conn, :forbidden) ==
|
||||||
%{"error" => "User is not an admin."}
|
%{"error" => "User is not a staff member."}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns 403 when requested by anonymous" do
|
test "returns 403 when requested by anonymous" do
|
||||||
|
|
47
test/pleroma/web/plugs/user_is_staff_plug_test.exs
Normal file
47
test/pleroma/web/plugs/user_is_staff_plug_test.exs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.Plugs.UserIsStaffPlugTest do
|
||||||
|
use Pleroma.Web.ConnCase, async: true
|
||||||
|
|
||||||
|
alias Pleroma.Web.Plugs.UserIsStaffPlug
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
test "accepts a user that is an admin" do
|
||||||
|
user = insert(:user, is_admin: true)
|
||||||
|
|
||||||
|
conn = assign(build_conn(), :user, user)
|
||||||
|
|
||||||
|
ret_conn = UserIsStaffPlug.call(conn, %{})
|
||||||
|
|
||||||
|
assert conn == ret_conn
|
||||||
|
end
|
||||||
|
|
||||||
|
test "accepts a user that is a moderator" do
|
||||||
|
user = insert(:user, is_moderator: true)
|
||||||
|
|
||||||
|
conn = assign(build_conn(), :user, user)
|
||||||
|
|
||||||
|
ret_conn = UserIsStaffPlug.call(conn, %{})
|
||||||
|
|
||||||
|
assert conn == ret_conn
|
||||||
|
end
|
||||||
|
|
||||||
|
test "denies a user that isn't a staff member" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
build_conn()
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> UserIsStaffPlug.call(%{})
|
||||||
|
|
||||||
|
assert conn.status == 403
|
||||||
|
end
|
||||||
|
|
||||||
|
test "denies when a user isn't set" do
|
||||||
|
conn = UserIsStaffPlug.call(build_conn(), %{})
|
||||||
|
|
||||||
|
assert conn.status == 403
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue