2018-12-23 15:04:54 -05:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 10:41:47 -05:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 15:04:54 -05:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-06-12 07:52:54 -04:00
|
|
|
defmodule Pleroma.UserInviteToken do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
2018-12-09 04:12:48 -05:00
|
|
|
alias Pleroma.Repo
|
2019-03-04 21:52:23 -05:00
|
|
|
alias Pleroma.UserInviteToken
|
2018-06-12 07:52:54 -04:00
|
|
|
|
|
|
|
schema "user_invite_tokens" do
|
|
|
|
field(:token, :string)
|
|
|
|
field(:used, :boolean, default: false)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_token do
|
|
|
|
token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
|
|
|
|
|
|
|
|
token = %UserInviteToken{
|
|
|
|
used: false,
|
|
|
|
token: token
|
|
|
|
}
|
|
|
|
|
|
|
|
Repo.insert(token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def used_changeset(struct) do
|
|
|
|
struct
|
|
|
|
|> cast(%{}, [])
|
|
|
|
|> put_change(:used, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|