akkoma/lib/pleroma/user_invite_token.ex

123 lines
3.1 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 15:41:47 +00:00
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2018-06-12 11:52:54 +00:00
defmodule Pleroma.UserInviteToken do
use Ecto.Schema
import Ecto.Changeset
2019-04-06 09:58:22 +00:00
import Ecto.Query
2018-12-09 09:12:48 +00:00
alias Pleroma.Repo
alias Pleroma.UserInviteToken
2018-06-12 11:52:54 +00:00
2019-04-06 09:58:22 +00:00
@type token :: String.t()
2018-06-12 11:52:54 +00:00
schema "user_invite_tokens" do
field(:token, :string)
field(:used, :boolean, default: false)
2019-04-06 09:58:22 +00:00
field(:max_use, :integer)
field(:expire_at, :date)
field(:uses, :integer)
field(:token_type)
2018-06-12 11:52:54 +00:00
timestamps()
end
2019-04-06 09:58:22 +00:00
def create_token(options \\ []) do
2018-06-12 11:52:54 +00:00
token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
2019-04-06 09:58:22 +00:00
max_use = options[:max_use]
expire_at = options[:expire_at]
token =
%UserInviteToken{
used: false,
token: token,
max_use: max_use,
expire_at: expire_at,
uses: 0
}
|> token_type()
2018-06-12 11:52:54 +00:00
Repo.insert(token)
end
2019-04-06 09:58:22 +00:00
def list_invites do
query = from(u in UserInviteToken, order_by: u.id)
Repo.all(query)
end
2018-06-12 11:52:54 +00:00
def used_changeset(struct) do
struct
|> cast(%{}, [])
|> put_change(:used, true)
end
2019-04-06 09:58:22 +00:00
@spec mark_as_used(token()) :: {:ok, UserInviteToken.t()} | {:error, token()}
2018-06-12 11:52:54 +00:00
def mark_as_used(token) do
with %{used: false} = token <- Repo.get_by(UserInviteToken, %{token: token}),
{:ok, token} <- Repo.update(used_changeset(token)) do
{:ok, token}
else
_e -> {:error, token}
end
end
2019-04-06 09:58:22 +00:00
defp token_type(%{expire_at: nil, max_use: nil} = token), do: %{token | token_type: "one_time"}
defp token_type(%{expire_at: _expire_at, max_use: nil} = token),
do: %{token | token_type: "date_limited"}
defp token_type(%{expire_at: nil, max_use: _max_use} = token),
do: %{token | token_type: "reusable"}
defp token_type(%{expire_at: _expire_at, max_use: _max_use} = token),
do: %{token | token_type: "reusable_date_limited"}
@spec valid_token?(UserInviteToken.t()) :: boolean()
def valid_token?(%{token_type: "one_time"} = token) do
not token.used
end
def valid_token?(%{token_type: "date_limited"} = token) do
not_overdue_date?(token) and not token.used
end
def valid_token?(%{token_type: "reusable"} = token) do
token.uses < token.max_use and not token.used
end
def valid_token?(%{token_type: "reusable_date_limited"} = token) do
not_overdue_date?(token) and token.uses < token.max_use and not token.used
end
defp not_overdue_date?(%{expire_at: expire_at} = token) do
Date.compare(Date.utc_today(), expire_at) in [:lt, :eq] ||
(Repo.update!(change(token, used: true)) && false)
end
def update_usage(%{token_type: "date_limited"}), do: nil
def update_usage(%{token_type: "one_time"} = token) do
UserInviteToken.mark_as_used(token.token)
end
def update_usage(%{token_type: token_type} = token)
when token_type == "reusable" or token_type == "reusable_date_limited" do
new_uses = token.uses + 1
changes = %{
uses: new_uses
}
changes =
if new_uses >= token.max_use do
Map.put(changes, :used, true)
else
changes
end
change(token, changes) |> Repo.update!()
end
2018-06-12 11:52:54 +00:00
end