Merge branch 'develop' into openapi/reports
[akkoma] / lib / pleroma / plugs / ensure_authenticated_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.EnsureAuthenticatedPlug do
6 import Plug.Conn
7 import Pleroma.Web.TranslationHelpers
8
9 alias Pleroma.User
10
11 use Pleroma.Web, :plug
12
13 def init(options) do
14 options
15 end
16
17 @impl true
18 def perform(%{assigns: %{user: %User{}}} = conn, _) do
19 conn
20 end
21
22 def perform(conn, options) do
23 perform =
24 cond do
25 options[:if_func] -> options[:if_func].()
26 options[:unless_func] -> !options[:unless_func].()
27 true -> true
28 end
29
30 if perform do
31 fail(conn)
32 else
33 conn
34 end
35 end
36
37 def fail(conn) do
38 conn
39 |> render_error(:forbidden, "Invalid credentials.")
40 |> halt()
41 end
42 end