2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-03-17 17:09:58 +01:00
|
|
|
defmodule Pleroma.Web do
|
|
|
|
@moduledoc """
|
|
|
|
A module that keeps using definitions for controllers,
|
|
|
|
views and so on.
|
|
|
|
|
|
|
|
This can be used in your application as:
|
|
|
|
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
|
|
|
The definitions below will be executed for every view,
|
|
|
|
controller, etc, so keep them short and clean, focused
|
|
|
|
on imports, uses and aliases.
|
|
|
|
|
|
|
|
Do NOT define functions inside the quoted expressions
|
|
|
|
below.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def controller do
|
|
|
|
quote do
|
|
|
|
use Phoenix.Controller, namespace: Pleroma.Web
|
2019-07-10 09:52:41 +02:00
|
|
|
|
2017-03-17 17:09:58 +01:00
|
|
|
import Plug.Conn
|
2019-02-09 16:20:18 +01:00
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
import Pleroma.Web.Router.Helpers
|
2019-07-10 09:52:41 +02:00
|
|
|
import Pleroma.Web.TranslationHelpers
|
2019-02-21 16:55:19 +01:00
|
|
|
|
2020-04-06 09:20:44 +02:00
|
|
|
alias Pleroma.Plugs.PlugHelper
|
|
|
|
|
2019-02-28 11:00:54 +01:00
|
|
|
plug(:set_put_layout)
|
|
|
|
|
|
|
|
defp set_put_layout(conn, _) do
|
|
|
|
put_layout(conn, Pleroma.Config.get(:app_layout, "app.html"))
|
|
|
|
end
|
2020-04-06 09:20:44 +02:00
|
|
|
|
2020-04-15 20:19:16 +02:00
|
|
|
# Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain
|
2020-04-06 09:20:44 +02:00
|
|
|
defp skip_plug(conn, plug_module) do
|
2020-04-15 20:19:16 +02:00
|
|
|
try do
|
2020-04-17 20:21:10 +02:00
|
|
|
plug_module.skip_plug(conn)
|
2020-04-15 20:19:16 +02:00
|
|
|
rescue
|
|
|
|
UndefinedFunctionError ->
|
|
|
|
raise "#{plug_module} is not skippable. Append `use Pleroma.Web, :plug` to its code."
|
|
|
|
end
|
2020-04-06 09:20:44 +02:00
|
|
|
end
|
|
|
|
|
2020-04-17 20:21:10 +02:00
|
|
|
# Executed just before actual controller action, invokes before-action hooks (callbacks)
|
2020-04-06 09:20:44 +02:00
|
|
|
defp action(conn, params) do
|
2020-04-17 20:21:10 +02:00
|
|
|
with %Plug.Conn{halted: false} <- maybe_halt_on_missing_oauth_scopes_check(conn) do
|
|
|
|
super(conn, params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Halts if authenticated API action neither performs nor explicitly skips OAuth scopes check
|
|
|
|
defp maybe_halt_on_missing_oauth_scopes_check(conn) do
|
2020-04-15 20:19:16 +02:00
|
|
|
if Pleroma.Plugs.AuthExpectedPlug.auth_expected?(conn) &&
|
2020-04-06 09:20:44 +02:00
|
|
|
not PlugHelper.plug_called_or_skipped?(conn, Pleroma.Plugs.OAuthScopesPlug) do
|
|
|
|
conn
|
|
|
|
|> render_error(
|
|
|
|
:forbidden,
|
|
|
|
"Security violation: OAuth scopes check was neither handled nor explicitly skipped."
|
|
|
|
)
|
|
|
|
|> halt()
|
|
|
|
else
|
2020-04-17 20:21:10 +02:00
|
|
|
conn
|
2020-04-06 09:20:44 +02:00
|
|
|
end
|
|
|
|
end
|
2017-03-17 17:09:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def view do
|
|
|
|
quote do
|
2018-03-30 15:01:53 +02:00
|
|
|
use Phoenix.View,
|
|
|
|
root: "lib/pleroma/web/templates",
|
|
|
|
namespace: Pleroma.Web
|
2017-03-17 17:09:58 +01:00
|
|
|
|
|
|
|
# Import convenience functions from controllers
|
|
|
|
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
|
|
|
|
|
2019-02-09 16:20:18 +01:00
|
|
|
import Pleroma.Web.ErrorHelpers
|
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
import Pleroma.Web.Router.Helpers
|
2019-01-27 19:15:35 +01:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc "Same as `render/3` but wrapped in a rescue block"
|
|
|
|
def safe_render(view, template, assigns \\ %{}) do
|
|
|
|
Phoenix.View.render(view, template, assigns)
|
|
|
|
rescue
|
|
|
|
error ->
|
|
|
|
Logger.error(
|
2019-08-14 00:15:18 +02:00
|
|
|
"#{__MODULE__} failed to render #{inspect({view, template})}\n" <>
|
|
|
|
Exception.format(:error, error, __STACKTRACE__)
|
2019-01-27 19:15:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-10 21:01:45 +02:00
|
|
|
Same as `render_many/4` but wrapped in rescue block.
|
2019-01-27 19:15:35 +01:00
|
|
|
"""
|
2019-09-10 21:01:45 +02:00
|
|
|
def safe_render_many(collection, view, template, assigns \\ %{}) do
|
2019-01-27 19:15:35 +01:00
|
|
|
Enum.map(collection, fn resource ->
|
|
|
|
as = Map.get(assigns, :as) || view.__resource__
|
|
|
|
assigns = Map.put(assigns, as, resource)
|
|
|
|
safe_render(view, template, assigns)
|
|
|
|
end)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
end
|
2017-03-17 17:09:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def router do
|
|
|
|
quote do
|
|
|
|
use Phoenix.Router
|
2019-02-08 17:18:44 +01:00
|
|
|
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
2017-03-17 17:09:58 +01:00
|
|
|
import Plug.Conn
|
|
|
|
import Phoenix.Controller
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def channel do
|
|
|
|
quote do
|
2019-02-08 17:18:44 +01:00
|
|
|
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
2017-03-17 17:09:58 +01:00
|
|
|
use Phoenix.Channel
|
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-15 20:19:16 +02:00
|
|
|
def plug do
|
|
|
|
quote do
|
|
|
|
alias Pleroma.Plugs.PlugHelper
|
|
|
|
|
2020-04-17 20:21:10 +02:00
|
|
|
@doc """
|
|
|
|
Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain.
|
|
|
|
"""
|
|
|
|
def skip_plug(conn) do
|
|
|
|
PlugHelper.append_to_private_list(
|
|
|
|
conn,
|
|
|
|
PlugHelper.skipped_plugs_list_id(),
|
|
|
|
__MODULE__
|
|
|
|
)
|
|
|
|
end
|
2020-04-15 20:19:16 +02:00
|
|
|
|
|
|
|
@impl Plug
|
|
|
|
@doc "If marked as skipped, returns `conn`, and calls `perform/2` otherwise."
|
|
|
|
def call(%Plug.Conn{} = conn, options) do
|
|
|
|
if PlugHelper.plug_skipped?(conn, __MODULE__) do
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
2020-04-17 20:21:10 +02:00
|
|
|
|> PlugHelper.append_to_private_list(PlugHelper.called_plugs_list_id(), __MODULE__)
|
2020-04-15 20:19:16 +02:00
|
|
|
|> perform(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-17 17:09:58 +01:00
|
|
|
@doc """
|
|
|
|
When used, dispatch to the appropriate controller/view/etc.
|
|
|
|
"""
|
|
|
|
defmacro __using__(which) when is_atom(which) do
|
|
|
|
apply(__MODULE__, which, [])
|
|
|
|
end
|
2017-03-30 14:32:36 +02:00
|
|
|
|
|
|
|
def base_url do
|
2018-03-30 15:01:53 +02:00
|
|
|
Pleroma.Web.Endpoint.url()
|
2017-03-30 14:32:36 +02:00
|
|
|
end
|
2017-03-17 17:09:58 +01:00
|
|
|
end
|