SaltDns.Accounts.Invitations (Salt v1.0.0)

Invitations for people who have no Salt user yet.

An owner or admin invites an e-mail address with a role. Salt sends nothing (there is no mailer for member notices): the inviter tells the person. With SALT_DNS_OIDC_AUTO_CREATE=invited the person's first sign-in through the identity provider then creates their user, if the provider says the address is verified and a pending invitation exists for it (accept_and_create_user/3).

An invitation is pending while it is not accepted, not revoked and not expired (14 days). An address match never attaches a provider account to a user that already exists: users are recognised by the provider's sub only (SaltDns.Accounts.oidc_sign_in/2).

The invitation row is the record of who invited, who revoked and who accepted, and when. Each of the three also writes one log line (level info) with the ids, never the address. Membership changes of existing users are not audited anywhere today; the zone audit table is per zone.

Summary

Functions

Creates the user of a first sign-in from the pending invitations for the verified address email, gives them the invited memberships and marks every one of those invitations accepted, in one transaction. Several organisations may have invited the same address; all are accepted.

Creates the invitation without checking who asks (the operator command, SaltDns.Release.invite/3). invited_by is a user or nil.

Invites email to organisation with role, as the scope's user (an owner or admin of the organisation). Errors: :forbidden, :user_exists (the address already has a user: add that user as a member instead) and an Ecto.Changeset (bad address or role, or already invited).

The pending invitations of an organisation, newest first, with who invited.

Revokes a pending invitation of the organisation, as the scope's user (an owner or admin). {:error, :not_found} when there is no such open invitation in this organisation.

Functions

accept_and_create_user(sub, email, now)

@spec accept_and_create_user(String.t(), String.t(), DateTime.t()) ::
  {:ok, SaltDns.Accounts.User.t()}
  | {:error, :no_invitation | {:insert, Ecto.Changeset.t()}}

Creates the user of a first sign-in from the pending invitations for the verified address email, gives them the invited memberships and marks every one of those invitations accepted, in one transaction. Several organisations may have invited the same address; all are accepted.

Returns {:error, :no_invitation} when none is pending, and {:error, {:insert, changeset}} when the user cannot be inserted (the caller decides what that means; nothing was changed).

create(organisation, email, role, invited_by)

@spec create(
  SaltDns.Accounts.Organisation.t(),
  String.t(),
  atom() | String.t(),
  SaltDns.Accounts.User.t() | nil
) ::
  {:ok, SaltDns.Accounts.Invitation.t()}
  | {:error, :user_exists | Ecto.Changeset.t()}

Creates the invitation without checking who asks (the operator command, SaltDns.Release.invite/3). invited_by is a user or nil.

invite(scope, organisation, email, role)

@spec invite(
  SaltDns.Accounts.Scope.t(),
  SaltDns.Accounts.Organisation.t(),
  String.t(),
  atom() | String.t()
) ::
  {:ok, SaltDns.Accounts.Invitation.t()}
  | {:error, :forbidden | :user_exists | Ecto.Changeset.t()}

Invites email to organisation with role, as the scope's user (an owner or admin of the organisation). Errors: :forbidden, :user_exists (the address already has a user: add that user as a member instead) and an Ecto.Changeset (bad address or role, or already invited).

list_pending(organisation)

The pending invitations of an organisation, newest first, with who invited.

revoke(scope, organisation, id)

@spec revoke(SaltDns.Accounts.Scope.t(), SaltDns.Accounts.Organisation.t(), integer()) ::
  {:ok, SaltDns.Accounts.Invitation.t()} | {:error, :forbidden | :not_found}

Revokes a pending invitation of the organisation, as the scope's user (an owner or admin). {:error, :not_found} when there is no such open invitation in this organisation.