Kantoor. Admin
(Kantoor v1.0.0)
Administration of this Kantoor instance: domains, mailboxes, users, held mail
and the audit log. Backs the /admin LiveViews.
Every function takes a Kantoor.Accounts.Scope first and re-checks, against
the database, that its user is still an enabled admin (authorize/1), so a
demoted or disabled admin loses access even in an already open LiveView.
Every mutation writes a Kantoor.Audit event. Failures are
{:error, :unauthorized} or a reason described per function.
Karta calls go through Kantoor.Karta.Client (PROPOSED paths, see its
docs). {:error, :not_configured} from the client means KARTA_* is unset:
Kantoor then works on its own index only.
Summary
Functions
Mailbox targets for aliases, including verified domains of the same organisation.
Attaches a domain: creates the Kantoor domains row and, when Karta is
configured, the Karta endpoint and route. attrs has "name" and optionally
"karta_domain_id".
Re-reads the scope's user and returns a fresh admin scope, or :unauthorized.
Domains Karta offers (proposed endpoint). {:ok, [map]} or {:error, reason}.
A schemaless changeset for the create-mailbox form (validation only, no database writes).
Creates an alias in the domain. attrs: "address" (the full alias
address), "mailbox_id" (a target in this domain or a verified domain of the same organisation) and optional
"note". Returns {:ok, alias, :ok | :skipped | :failed}.
Creates an operator API key (Kantoor.ApiKeys). Returns {:ok, key, token}; the
token is shown once and cannot be recovered. The audit event holds the key's name,
prefix, operator and scopes, never the token.
Creates a mailbox (with its default folders) on an attached, enabled domain.
params: "local_part", "domain_id", optional "quota_amount"/"quota_unit"
(blank = domain default). Returns {:ok, mailbox} or {:error, changeset}.
Deletes the alias and the identities that send as its address; the Karta route
at that address stops delivering. Returns {:ok, address, :ok | :skipped | :failed}.
Deletes a mailbox with its folders, messages, filters, vacation reply, grants and identities.
The domain's stored Salt DNS publication row (or nil). Admin only, no HTTP.
The DNS records for a domain of this instance (Kantoor.DnsRecords), with the
domain row. {:error, :not_found} for an unknown id.
What Salt DNS says about this domain's client-DNS records: the zone it found, every record's status and the publication state. Read-only and admin only; a LiveView calls it from an async task because it makes HTTP calls.
The domain's own mailboxes as {id, address}, for the catch-all and alias pickers.
Mailboxes that hold over-quota mail, each with usage, quota and the oldest 50 held messages (envelope fields only, never bodies).
Karta connectivity: :not_configured, {:ok, info} or {:error, reason}. The
client may be slow, so LiveViews call this from an async task.
The domain and its aliases, for the domain settings screen.
Every operator API key, newest first, revoked ones included.
One page of the audit log. See Kantoor.Audit.list/2.
Domains attached in Kantoor with their settings and catch-all mailbox.
Mailbox rows with usage, effective quota and held counts. Filters: :q
(substring of the address, matched literally), :domain_id, :state
("active" / "disabled"). Returns {:ok, %{rows, total}}; at most 100 rows.
Users with their active app password count and attached mailbox ids.
One mailbox with what the admin screen shows: the domain, usage against the effective quota, the held count, the message count and the members with their roles (oldest grant first).
Every mailbox as {id, address}, for the access pickers.
Last run of every maintenance job on this node (admin only).
Converts a single-target forwarding alias to a mailbox alias without removing its address.
"Publish to Salt": enables automatic syncing for the domain and writes the
missing records now. {:error, reason} when Salt refused; the domain stays
enabled so the nightly sync retries.
Adds a person to a mailbox with a role, or changes the role of a person who is
already a member. Returns {:ok, grant}.
Registers a domain's endpoint and route in Karta again (idempotent there). When
Karta shows a new signing secret (a new endpoint, e.g. after it was deleted in
Karta) it replaces the stored one at once, and the old secret stops verifying
pushes. Returns {:ok, domain, :attached | :attached_stored | {:attached, secret}}.
Releases held mail of a mailbox now (as far as the quota allows). {:ok, count}.
Removes a person's access to a mailbox. Idempotent. Returns :ok.
Revokes an operator API key (idempotent).
Revokes every active app password of a user. Returns {:ok, count}.
Turns the domain's catch-all off (nil) or points it at a mailbox of the
domain. The setting is saved first and the Karta * route follows; a route
failure is reported (:failed) and stored on the domain, never hidden.
Enables or disables a domain. A disabled domain stops ingest (Kantoor answers
the push with a retryable 503) and its Karta route is put on hold; mail is
never deleted. The local change always happens; karta in the result is
:ok, :skipped (no route recorded or Karta not configured) or
{:error, reason}. A domain suspended through the provisioning API cannot be
enabled here ({:error, :suspended}): resume it there.
Disables or enables a mailbox (a disabled mailbox gets no new mail; nothing is deleted).
Marks a mailbox as a person's own (:personal) or a shared mailbox
(:shared). The kind changes what the interface says and how the mailbox is
presented; permissions stay with the member roles.
Disables or enables a user. See Kantoor.Accounts.set_user_disabled/3 for the rules.
Makes the user's mailbox grants exactly mailbox_ids. Returns
{:ok, %{added: n, removed: n}}.
Promotes or demotes a user. See Kantoor.Accounts.set_user_role/3 for the rules.
Counts for the dashboard.
Stops publishing: removes the values Kantoor wrote and clears the yes. The records of whoever else is at those names stay.
Sync now: repairs missing or different records without changing the operator's yes.
Brings the domain's Karta routes in step with its mailboxes, aliases and catch-all now (the admin screen's "re-sync routes" action, and what an older domain needs after the release that made exact routes).
Updates a domain's default quota and retention. attrs: default_quota_bytes
(nil = unlimited) and retention_days (nil = keep). Raising the quota
releases held mail where it now fits. Returns {:ok, settings} or
{:error, changeset}.
Sets (or with nil clears) a mailbox's quota override. Raising it releases
held mail that now fits. Returns {:ok, mailbox}.
Functions
Mailbox targets for aliases, including verified domains of the same organisation.
Attaches a domain: creates the Kantoor domains row and, when Karta is
configured, the Karta endpoint and route. attrs has "name" and optionally
"karta_domain_id".
Returns {:ok, domain, karta} where karta is :attached,
{:attached, secret} (Karta made a new endpoint and showed its signing secret,
once) or :not_configured (row created without a Karta route), {:error, changeset}
or {:error, {:karta, reason}} (Karta refused; nothing was created).
@spec authorize(Kantoor.Accounts.Scope.t() | nil) :: {:ok, Kantoor.Accounts.Scope.t()} | {:error, :unauthorized}
Re-reads the scope's user and returns a fresh admin scope, or :unauthorized.
Domains Karta offers (proposed endpoint). {:ok, [map]} or {:error, reason}.
A schemaless changeset for the create-mailbox form (validation only, no database writes).
Creates an alias in the domain. attrs: "address" (the full alias
address), "mailbox_id" (a target in this domain or a verified domain of the same organisation) and optional
"note". Returns {:ok, alias, :ok | :skipped | :failed}.
Creates an operator API key (Kantoor.ApiKeys). Returns {:ok, key, token}; the
token is shown once and cannot be recovered. The audit event holds the key's name,
prefix, operator and scopes, never the token.
Creates a mailbox (with its default folders) on an attached, enabled domain.
params: "local_part", "domain_id", optional "quota_amount"/"quota_unit"
(blank = domain default). Returns {:ok, mailbox} or {:error, changeset}.
Deletes the alias and the identities that send as its address; the Karta route
at that address stops delivering. Returns {:ok, address, :ok | :skipped | :failed}.
Deletes a mailbox with its folders, messages, filters, vacation reply, grants and identities.
Rules:
- a mailbox that still holds mail is refused with
{:error, {:not_empty, count}}unless the caller confirms that exact count withconfirm_messages: count: deleting mail is never a side effect; - a mailbox that is its domain's catch-all is refused with
{:error, :catch_all_target}— point the catch-all elsewhere (or off) first, so the catch-all can never point at a mailbox that is gone; - the mailbox's exact Karta route stops delivering (the routes are synced after the delete), and Karta is asked to forget this instance's copies in the background.
Returns {:ok, %{address, messages, routes}}.
@spec dns_publication(Kantoor.Accounts.Scope.t(), String.t()) :: {:ok, Kantoor.DnsRecords.Publication.t() | nil}
The domain's stored Salt DNS publication row (or nil). Admin only, no HTTP.
The DNS records for a domain of this instance (Kantoor.DnsRecords), with the
domain row. {:error, :not_found} for an unknown id.
@spec dns_salt_status(Kantoor.Accounts.Scope.t(), String.t()) :: {:ok, map()} | {:error, term()}
What Salt DNS says about this domain's client-DNS records: the zone it found, every record's status and the publication state. Read-only and admin only; a LiveView calls it from an async task because it makes HTTP calls.
The domain's own mailboxes as {id, address}, for the catch-all and alias pickers.
Mailboxes that hold over-quota mail, each with usage, quota and the oldest 50 held messages (envelope fields only, never bodies).
Karta connectivity: :not_configured, {:ok, info} or {:error, reason}. The
client may be slow, so LiveViews call this from an async task.
The domain and its aliases, for the domain settings screen.
Every operator API key, newest first, revoked ones included.
One page of the audit log. See Kantoor.Audit.list/2.
Domains attached in Kantoor with their settings and catch-all mailbox.
Mailbox rows with usage, effective quota and held counts. Filters: :q
(substring of the address, matched literally), :domain_id, :state
("active" / "disabled"). Returns {:ok, %{rows, total}}; at most 100 rows.
Users with their active app password count and attached mailbox ids.
@spec mailbox_detail(Kantoor.Accounts.Scope.t() | nil, term()) :: {:ok, map()} | {:error, term()}
One mailbox with what the admin screen shows: the domain, usage against the effective quota, the held count, the message count and the members with their roles (oldest grant first).
Every mailbox as {id, address}, for the access pickers.
Last run of every maintenance job on this node (admin only).
Converts a single-target forwarding alias to a mailbox alias without removing its address.
@spec publish_dns(Kantoor.Accounts.Scope.t(), String.t()) :: {:ok, map()} | {:error, term()}
"Publish to Salt": enables automatic syncing for the domain and writes the
missing records now. {:error, reason} when Salt refused; the domain stays
enabled so the nightly sync retries.
@spec put_mailbox_member( Kantoor.Accounts.Scope.t() | nil, term(), term(), Kantoor.Accounts.UserMailbox.role() ) :: {:ok, Kantoor.Accounts.UserMailbox.t()} | {:error, term()}
Adds a person to a mailbox with a role, or changes the role of a person who is
already a member. Returns {:ok, grant}.
Registers a domain's endpoint and route in Karta again (idempotent there). When
Karta shows a new signing secret (a new endpoint, e.g. after it was deleted in
Karta) it replaces the stored one at once, and the old secret stops verifying
pushes. Returns {:ok, domain, :attached | :attached_stored | {:attached, secret}}.
Releases held mail of a mailbox now (as far as the quota allows). {:ok, count}.
@spec remove_mailbox_member(Kantoor.Accounts.Scope.t() | nil, term(), term()) :: :ok | {:error, term()}
Removes a person's access to a mailbox. Idempotent. Returns :ok.
Revokes an operator API key (idempotent).
Revokes every active app password of a user. Returns {:ok, count}.
Turns the domain's catch-all off (nil) or points it at a mailbox of the
domain. The setting is saved first and the Karta * route follows; a route
failure is reported (:failed) and stored on the domain, never hidden.
Returns {:ok, settings, :ok | :skipped | :failed}.
Enables or disables a domain. A disabled domain stops ingest (Kantoor answers
the push with a retryable 503) and its Karta route is put on hold; mail is
never deleted. The local change always happens; karta in the result is
:ok, :skipped (no route recorded or Karta not configured) or
{:error, reason}. A domain suspended through the provisioning API cannot be
enabled here ({:error, :suspended}): resume it there.
Disables or enables a mailbox (a disabled mailbox gets no new mail; nothing is deleted).
@spec set_mailbox_kind(Kantoor.Accounts.Scope.t() | nil, term(), :personal | :shared) :: {:ok, Kantoor.Mailboxes.Mailbox.t()} | {:error, term()}
Marks a mailbox as a person's own (:personal) or a shared mailbox
(:shared). The kind changes what the interface says and how the mailbox is
presented; permissions stay with the member roles.
Disables or enables a user. See Kantoor.Accounts.set_user_disabled/3 for the rules.
Makes the user's mailbox grants exactly mailbox_ids. Returns
{:ok, %{added: n, removed: n}}.
Promotes or demotes a user. See Kantoor.Accounts.set_user_role/3 for the rules.
Counts for the dashboard.
@spec stop_dns(Kantoor.Accounts.Scope.t(), String.t()) :: {:ok, map()} | {:error, term()}
Stops publishing: removes the values Kantoor wrote and clears the yes. The records of whoever else is at those names stay.
@spec sync_dns(Kantoor.Accounts.Scope.t(), String.t()) :: {:ok, map()} | {:error, term()}
Sync now: repairs missing or different records without changing the operator's yes.
Brings the domain's Karta routes in step with its mailboxes, aliases and catch-all now (the admin screen's "re-sync routes" action, and what an older domain needs after the release that made exact routes).
Returns {:ok, domain, summary}, {:error, :not_configured},
{:error, :not_attached} or {:error, {:routes, summary}}.
Updates a domain's default quota and retention. attrs: default_quota_bytes
(nil = unlimited) and retention_days (nil = keep). Raising the quota
releases held mail where it now fits. Returns {:ok, settings} or
{:error, changeset}.
Sets (or with nil clears) a mailbox's quota override. Raising it releases
held mail that now fits. Returns {:ok, mailbox}.