2019-11-17 19:18:21 +01:00
|
|
|
defmodule PhilomenaWeb.ReloadUserPlug do
|
2019-12-17 03:49:17 +01:00
|
|
|
alias Plug.Conn
|
2019-10-31 18:57:39 +01:00
|
|
|
alias Pow.Plug
|
2019-12-16 23:50:15 +01:00
|
|
|
alias Philomena.Users
|
2019-10-31 18:57:39 +01:00
|
|
|
|
2019-12-18 20:32:21 +01:00
|
|
|
alias Philomena.Servers.UserIpUpdater
|
|
|
|
alias Philomena.Servers.UserFingerprintUpdater
|
2019-12-17 03:49:17 +01:00
|
|
|
|
2019-10-31 18:57:39 +01:00
|
|
|
def init(opts), do: opts
|
|
|
|
|
|
|
|
def call(conn, _opts) do
|
|
|
|
config = Plug.fetch_config(conn)
|
|
|
|
|
|
|
|
case Plug.current_user(conn, config) do
|
|
|
|
nil ->
|
|
|
|
conn
|
|
|
|
|
|
|
|
user ->
|
2019-12-18 20:32:21 +01:00
|
|
|
update_usages(conn, user)
|
2019-12-16 23:50:15 +01:00
|
|
|
reloaded_user = Users.get_by(id: user.id)
|
2019-10-31 18:57:39 +01:00
|
|
|
|
|
|
|
Plug.assign_current_user(conn, reloaded_user, config)
|
|
|
|
end
|
|
|
|
end
|
2019-12-17 03:49:17 +01:00
|
|
|
|
|
|
|
defp update_usages(conn, user) do
|
2019-12-18 20:32:21 +01:00
|
|
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
2019-12-17 03:49:17 +01:00
|
|
|
conn = Conn.fetch_cookies(conn)
|
|
|
|
|
2019-12-18 20:32:21 +01:00
|
|
|
UserIpUpdater.cast(user.id, conn.remote_ip, now)
|
|
|
|
UserFingerprintUpdater.cast(user.id, conn.cookies["_ses"], now)
|
2019-12-17 03:49:17 +01:00
|
|
|
end
|
2019-12-16 23:50:15 +01:00
|
|
|
end
|