Merge branch 'develop' into refactor/discoverable_user_field
[akkoma] / lib / pleroma / web / plugs / ensure_public_or_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.Web.Plugs.EnsurePublicOrAuthenticatedPlug do
6 import Pleroma.Web.TranslationHelpers
7 import Plug.Conn
8
9 alias Pleroma.Config
10 alias Pleroma.User
11
12 use Pleroma.Web, :plug
13
14 def init(options) do
15 options
16 end
17
18 @impl true
19 def perform(conn, _) do
20 public? = Config.get!([:instance, :public])
21
22 case {public?, conn} do
23 {true, _} ->
24 conn
25
26 {false, %{assigns: %{user: %User{}}}} ->
27 conn
28
29 {false, _} ->
30 conn
31 |> render_error(:forbidden, "This resource requires authentication.")
32 |> halt
33 end
34 end
35 end