Karta.Kantoor.Restrictions (Karta v1.0.0)

The optional restrictions of a Kantoor key (allowed_domains, allowed_mailboxes; nil means none) and the check of an address or a domain against them. They are enforced by the /kantoor/v1 controllers.

Grammar (case-insensitive, stored in lower case; no other use of *):

  • a domain pattern is * (every domain), example.com (that domain only) or *.example.com (its subdomains, not the domain itself);
  • a mailbox pattern is * (every address), ann@example.com, or *@example.com (every local part); the part after the @ is a domain pattern.

An address is allowed when it matches allowed_domains (when set) and allowed_mailboxes (when set). A domain is allowed when it matches allowed_domains and, when allowed_mailboxes is set, at least one mailbox pattern could match an address at it. Karta does not interpret the local part in any other way: no + tags, no partial wildcards.

Summary

Functions

May the key touch this address? (An address without an @ never.)

May the key touch every recipient and sender of this message? (nil addresses are ignored.)

May the key touch (list, attach to) this domain?

Does domain match the domain pattern?

The restrictions as LIKE patterns for lower(address) in SQL, for listings (Karta.Kantoor.Messages.list/2): nil for a restriction that is not set, otherwise a list of which an address must match one. They select the same addresses as address_allowed?/2.

True when the key has any restriction.

True when pattern is a valid pattern of kind.

Validates (and lower-cases) the patterns in field; kind is :domain or :mailbox.

Functions

address_allowed?(credential, address)

@spec address_allowed?(map(), String.t()) :: boolean()

May the key touch this address? (An address without an @ never.)

all_allowed?(credential, addresses)

@spec all_allowed?(map(), [String.t() | nil]) :: boolean()

May the key touch every recipient and sender of this message? (nil addresses are ignored.)

domain_allowed?(credential, domain)

@spec domain_allowed?(map(), String.t()) :: boolean()

May the key touch (list, attach to) this domain?

domain_match?(pattern, domain)

@spec domain_match?(String.t(), String.t()) :: boolean()

Does domain match the domain pattern?

like_patterns(credential)

@spec like_patterns(map()) :: %{
  domains: [String.t()] | nil,
  mailboxes: [String.t()] | nil
}

The restrictions as LIKE patterns for lower(address) in SQL, for listings (Karta.Kantoor.Messages.list/2): nil for a restriction that is not set, otherwise a list of which an address must match one. They select the same addresses as address_allowed?/2.

restricted?(credential)

@spec restricted?(map()) :: boolean()

True when the key has any restriction.

valid?(atom, domain)

@spec valid?(:domain | :mailbox, String.t()) :: boolean()

True when pattern is a valid pattern of kind.

validate(changeset, field, kind)

@spec validate(Ecto.Changeset.t(), atom(), :domain | :mailbox) :: Ecto.Changeset.t()

Validates (and lower-cases) the patterns in field; kind is :domain or :mailbox.