2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-24 08:00:44 +02:00
|
|
|
defmodule Pleroma.Web.Plugs.UserFetcherPlug do
|
2020-10-31 11:38:35 +01:00
|
|
|
@moduledoc """
|
|
|
|
Assigns `:auth_user` basing on `:auth_credentials`.
|
|
|
|
|
|
|
|
NOTE: no checks are performed at this step, auth_credentials/username could be easily faked.
|
|
|
|
"""
|
|
|
|
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.User
|
2018-09-05 17:44:38 +02:00
|
|
|
import Plug.Conn
|
|
|
|
|
|
|
|
def init(options) do
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2018-12-09 10:12:48 +01:00
|
|
|
def call(conn, _options) do
|
2018-09-05 17:44:38 +02:00
|
|
|
with %{auth_credentials: %{username: username}} <- conn.assigns,
|
2019-04-02 12:47:02 +02:00
|
|
|
%User{} = user <- User.get_by_nickname_or_email(username) do
|
|
|
|
assign(conn, :auth_user, user)
|
2018-09-05 17:44:38 +02:00
|
|
|
else
|
|
|
|
_ -> conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|