4a0e43b003e805e182bc462e342bdce18434a18c
[akkoma] / lib / pleroma / plugs / user_is_admin_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.UserIsAdminPlug do
6 import Pleroma.Web.TranslationHelpers
7 import Plug.Conn
8
9 alias Pleroma.Web.OAuth
10
11 def init(options) do
12 options
13 end
14
15 def call(%Plug.Conn{assigns: assigns} = conn, _) do
16 token = assigns[:token]
17 user = assigns[:user]
18
19 cond do
20 token && OAuth.Scopes.contains_admin_scopes?(token.scopes) ->
21 # Note: checking for _any_ admin scope presence, not necessarily fitting requested action.
22 # Thus, controller must explicitly invoke OAuthScopesPlug to verify scope requirements.
23 conn
24
25 user && user.is_admin && !Pleroma.Config.enforce_oauth_admin_scope_usage?() ->
26 conn
27
28 true ->
29 conn
30 |> render_error(:forbidden, "User is not an admin or OAuth admin scope is not granted.")
31 |> halt()
32 end
33 end
34 end