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