File size: 6,349 Bytes
3e21b19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | defmodule Plausible.Auth.UserSessions do
@moduledoc """
Functions for interacting with user sessions.
"""
use Plausible
import Ecto.Query
alias Plausible.Auth
alias Plausible.Repo
@socket_id_prefix "user_sessions:"
@spec list_for_user(Auth.User.t(), NaiveDateTime.t()) :: [Auth.UserSession.t()]
def list_for_user(user, now \\ NaiveDateTime.utc_now(:second)) do
Repo.all(
from us in Auth.UserSession,
where: us.user_id == ^user.id,
where: us.timeout_at >= ^now,
order_by: [desc: us.last_used_at, desc: us.id]
)
end
@spec count_for_users([Auth.User.t()], NaiveDateTime.t()) :: list()
def count_for_users(users, now \\ NaiveDateTime.utc_now(:second)) when is_list(users) do
Repo.all(
from(us in Auth.UserSession,
where: us.user_id in ^Enum.map(users, & &1.id),
where: us.timeout_at >= ^now,
group_by: us.user_id,
select: {us.user_id, count(us.id)},
order_by: [asc: us.user_id]
)
)
end
on_ee do
alias Plausible.Teams
@spec list_sso_for_team(Teams.Team.t(), NaiveDateTime.t()) :: [Auth.UserSession.t()]
def list_sso_for_team(team, now \\ NaiveDateTime.utc_now(:second)) do
user_ids =
Repo.all(
from t in Teams.Team,
inner_join: tm in assoc(t, :team_memberships),
inner_join: u in assoc(tm, :user),
where: t.id == ^team.id,
where: tm.role != :guest,
where: u.type == :sso,
select: u.id
)
Repo.all(
from us in Auth.UserSession,
inner_join: u in assoc(us, :user),
where: us.user_id in ^user_ids,
where: us.timeout_at >= ^now,
order_by: [desc: us.last_used_at, desc: us.id],
preload: [user: u]
)
end
@spec revoke_sso_by_id(Teams.Team.t(), pos_integer()) :: :ok
def revoke_sso_by_id(team, session_id) do
{_, tokens} =
Repo.delete_all(
from us in Auth.UserSession,
inner_join: u in assoc(us, :user),
inner_join: tm in assoc(u, :team_memberships),
where: u.type == :sso,
where: us.id == ^session_id,
where: tm.role != :guest,
where: tm.team_id == ^team.id,
select: us.token
)
case tokens do
[token] ->
disconnect_by_token(token)
_ ->
:pass
end
:ok
end
end
@spec last_used_humanize(Auth.UserSession.t(), NaiveDateTime.t()) :: String.t()
def last_used_humanize(user_session, now \\ NaiveDateTime.utc_now(:second)) do
diff = NaiveDateTime.diff(now, user_session.last_used_at, :hour)
diff_days = NaiveDateTime.diff(now, user_session.last_used_at, :day)
cond do
diff < 1 -> "Just recently"
diff == 1 -> "1 hour ago"
diff < 24 -> "#{diff} hours ago"
diff < 2 * 24 -> "Yesterday"
true -> "#{diff_days} days ago"
end
end
@spec get_by_token(String.t()) ::
{:ok, Auth.UserSession.t()}
| {:error, :not_found}
| {:error, :expired, Auth.UserSession.t()}
def get_by_token(token) do
now = NaiveDateTime.utc_now(:second)
last_team_subscription_query = Plausible.Teams.last_subscription_join_query()
token_query =
from(us in Auth.UserSession,
inner_join: u in assoc(us, :user),
as: :user,
left_join: tm in assoc(u, :team_memberships),
on: tm.role != :guest,
left_join: t in assoc(tm, :team),
as: :team,
left_join: o in assoc(t, :owners),
left_lateral_join: ts in subquery(last_team_subscription_query),
on: true,
where: us.token == ^token,
order_by: t.id,
preload: [user: {u, team_memberships: {tm, team: {t, subscription: ts, owners: o}}}]
)
case Repo.one(token_query) do
%Auth.UserSession{} = user_session ->
if NaiveDateTime.compare(user_session.timeout_at, now) == :gt do
{:ok, user_session}
else
{:error, :expired, user_session}
end
nil ->
{:error, :not_found}
end
end
@spec create!(Auth.User.t(), String.t(), Keyword.t()) :: Auth.UserSession.t()
def create!(user, device_name, opts \\ []) do
user
|> Auth.UserSession.new_session(device_name, opts)
|> Repo.insert!()
end
@spec remove_by_token(String.t()) :: :ok
def remove_by_token(token) do
Repo.delete_all(from us in Auth.UserSession, where: us.token == ^token)
:ok
end
@spec touch(Auth.UserSession.t(), NaiveDateTime.t()) :: Auth.UserSession.t()
def touch(user_session, now \\ NaiveDateTime.utc_now(:second)) do
if NaiveDateTime.diff(now, user_session.last_used_at, :hour) >= 1 do
Plausible.Users.bump_last_seen(user_session.user_id, now)
user_session
|> Repo.preload(:user)
|> Auth.UserSession.touch_session(now)
|> Repo.update!(allow_stale: true)
else
user_session
end
end
@spec revoke_by_id(Auth.User.t(), pos_integer()) :: :ok
def revoke_by_id(user, session_id) do
{_, tokens} =
Repo.delete_all(
from us in Auth.UserSession,
where: us.user_id == ^user.id and us.id == ^session_id,
select: us.token
)
case tokens do
[token] ->
disconnect_by_token(token)
_ ->
:pass
end
:ok
end
@spec revoke_all(Auth.User.t(), Keyword.t()) :: :ok
def revoke_all(user, opts \\ []) do
except = Keyword.get(opts, :except)
delete_query = from us in Auth.UserSession, where: us.user_id == ^user.id, select: us.token
delete_query =
if except do
where(delete_query, [us], us.id != ^except.id)
else
delete_query
end
{_count, tokens} = Repo.delete_all(delete_query)
Enum.each(tokens, &disconnect_by_token/1)
end
@spec disconnect_by_token(String.t()) :: :ok
def disconnect_by_token(token_or_socket_id) do
socket_id =
if String.starts_with?(token_or_socket_id, @socket_id_prefix) do
token_or_socket_id
else
socket_id(token_or_socket_id)
end
PlausibleWeb.Endpoint.broadcast(socket_id, "disconnect", %{})
:ok
end
@spec socket_id(String.t()) :: String.t()
def socket_id(token) do
@socket_id_prefix <> Base.url_encode64(token)
end
end
|