Assign :user whenever :user_id is in session, pass to OAuth form
This commit is contained in:
parent
0fc2f5346d
commit
b829226cbf
@ -3,6 +3,7 @@
|
|||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Plugs.EnsureUserKeyPlug do
|
defmodule Pleroma.Plugs.EnsureUserKeyPlug do
|
||||||
|
alias Pleroma.User
|
||||||
import Plug.Conn
|
import Plug.Conn
|
||||||
|
|
||||||
def init(opts) do
|
def init(opts) do
|
||||||
@ -12,7 +13,12 @@ defmodule Pleroma.Plugs.EnsureUserKeyPlug do
|
|||||||
def call(%{assigns: %{user: _}} = conn, _), do: conn
|
def call(%{assigns: %{user: _}} = conn, _), do: conn
|
||||||
|
|
||||||
def call(conn, _) do
|
def call(conn, _) do
|
||||||
conn
|
with user_id <- get_session(conn, :user_id),
|
||||||
|> assign(:user, nil)
|
true <- is_binary(user_id),
|
||||||
|
%User{} = user <- User.get_by_id(user_id) do
|
||||||
|
assign(conn, :user, user)
|
||||||
|
else
|
||||||
|
_ -> assign(conn, :user, nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -276,7 +276,7 @@ defmodule Pleroma.Web.Router do
|
|||||||
|
|
||||||
scope "/oauth", Pleroma.Web.OAuth do
|
scope "/oauth", Pleroma.Web.OAuth do
|
||||||
scope [] do
|
scope [] do
|
||||||
pipe_through(:oauth)
|
pipe_through([:oauth, :after_auth])
|
||||||
get("/authorize", OAuthController, :authorize)
|
get("/authorize", OAuthController, :authorize)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -4,8 +4,23 @@
|
|||||||
|
|
||||||
defmodule Pleroma.Plugs.EnsureUserKeyPlugTest do
|
defmodule Pleroma.Plugs.EnsureUserKeyPlugTest do
|
||||||
use Pleroma.Web.ConnCase, async: true
|
use Pleroma.Web.ConnCase, async: true
|
||||||
|
|
||||||
alias Pleroma.Plugs.EnsureUserKeyPlug
|
alias Pleroma.Plugs.EnsureUserKeyPlug
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
@session_opts [
|
||||||
|
store: :cookie,
|
||||||
|
key: "_test",
|
||||||
|
signing_salt: "cooldude"
|
||||||
|
]
|
||||||
|
|
||||||
|
setup %{conn: conn} do
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> Plug.Session.call(Plug.Session.init(@session_opts))
|
||||||
|
|> fetch_session()
|
||||||
|
|
||||||
|
%{conn: conn}
|
||||||
|
end
|
||||||
|
|
||||||
test "if the conn has a user key set, it does nothing", %{conn: conn} do
|
test "if the conn has a user key set, it does nothing", %{conn: conn} do
|
||||||
conn =
|
conn =
|
||||||
@ -19,6 +34,17 @@ defmodule Pleroma.Plugs.EnsureUserKeyPlugTest do
|
|||||||
assert conn == ret_conn
|
assert conn == ret_conn
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "if the session has a user_id, it sets the user", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> put_session(:user_id, user.id)
|
||||||
|
|> EnsureUserKeyPlug.call(%{})
|
||||||
|
|
||||||
|
assert conn.assigns[:user] == user
|
||||||
|
end
|
||||||
|
|
||||||
test "if the conn has no key set, it sets it to nil", %{conn: conn} do
|
test "if the conn has no key set, it sets it to nil", %{conn: conn} do
|
||||||
conn =
|
conn =
|
||||||
conn
|
conn
|
||||||
|
Loading…
Reference in New Issue
Block a user