2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-10-02 17:13:21 +00:00
|
|
|
defmodule Pleroma.Plugs.UserIsAdminPlug do
|
2019-07-10 09:25:58 +00:00
|
|
|
import Pleroma.Web.TranslationHelpers
|
2018-10-02 17:13:21 +00:00
|
|
|
import Plug.Conn
|
2019-12-05 21:25:44 +00:00
|
|
|
|
2018-10-02 17:13:21 +00:00
|
|
|
alias Pleroma.User
|
2019-12-05 21:25:44 +00:00
|
|
|
alias Pleroma.Web.OAuth
|
2018-10-02 17:13:21 +00:00
|
|
|
|
|
|
|
def init(options) do
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2019-12-05 21:25:44 +00:00
|
|
|
def call(%Plug.Conn{assigns: %{token: %OAuth.Token{scopes: oauth_scopes} = _token}} = conn, _) do
|
|
|
|
if OAuth.Scopes.contains_admin_scopes?(oauth_scopes) do
|
|
|
|
# Note: checking for _any_ admin scope presence, not necessarily fitting requested action.
|
|
|
|
# Thus, controller must explicitly invoke OAuthScopesPlug to verify scope requirements.
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
fail(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless Pleroma.Config.enforce_oauth_admin_scope_usage?() do
|
|
|
|
# To do: once AdminFE makes use of "admin" scope, disable the following func definition
|
|
|
|
# (fail on no admin scope(s) in token even if `is_admin` is true)
|
|
|
|
def call(%Plug.Conn{assigns: %{user: %User{is_admin: true}}} = conn, _) do
|
|
|
|
conn
|
|
|
|
end
|
2018-10-02 17:13:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _) do
|
2019-12-05 21:25:44 +00:00
|
|
|
fail(conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fail(conn) do
|
2018-10-02 17:13:21 +00:00
|
|
|
conn
|
2019-12-05 21:25:44 +00:00
|
|
|
|> render_error(:forbidden, "User is not an admin or OAuth admin scope is not granted.")
|
|
|
|
|> halt()
|
2018-10-02 17:13:21 +00:00
|
|
|
end
|
|
|
|
end
|