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