814029dce285c6598742523711da3206463fe278
[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 unless Pleroma.Config.enforce_oauth_admin_scope_usage?() do
16 # To do: once AdminFE makes use of "admin" scope, disable the following func definition
17 # (fail on no admin scope(s) in token even if `is_admin` is true)
18 def call(%Plug.Conn{assigns: %{user: %Pleroma.User{is_admin: true}}} = conn, _) do
19 conn
20 end
21 end
22
23 def call(%Plug.Conn{assigns: %{token: %OAuth.Token{scopes: oauth_scopes} = _token}} = conn, _) do
24 if OAuth.Scopes.contains_admin_scopes?(oauth_scopes) do
25 # Note: checking for _any_ admin scope presence, not necessarily fitting requested action.
26 # Thus, controller must explicitly invoke OAuthScopesPlug to verify scope requirements.
27 conn
28 else
29 fail(conn)
30 end
31 end
32
33 def call(conn, _) do
34 fail(conn)
35 end
36
37 defp fail(conn) do
38 conn
39 |> render_error(:forbidden, "User is not an admin or OAuth admin scope is not granted.")
40 |> halt()
41 end
42 end