Kantoor. Accounts
(Kantoor v1.0.0)
Users, their roles and which mailboxes they may open.
Users are created and updated from identity-provider (OIDC) claims. The
email claim is stored but never used to link a user to a mailbox: an admin
attaches mailboxes explicitly with attach_mailbox/3.
Functions that act for a user take a Kantoor.Accounts.Scope first.
Summary
Functions
Attaches a mailbox to a user with a role (admin only). Idempotent: an existing grant is left as it is, including its role.
Whether the scope's user may open the mailbox (a struct or an id). Every role may read.
Whether the scope may change the mailbox's own settings (rules, away reply, forwarding).
Whether the scope may send with the mailbox's address as From (send as).
Whether the scope may send with the mailbox as Sender (send on behalf).
Whether the scope may change the mailbox's mail (flags, folders, moves, deletions). Every sending role may.
The profile changeset for the form.
Removes a mailbox grant (admin only).
Gets a user by id; nil when missing.
The mailboxes the scope may send on behalf of (an identity of their own is still required).
The mailboxes the scope's user may open: every mailbox for an admin (their own attached ones first), the attached ones for a user, none without a (valid) scope.
The mailboxes whose rules, away reply and forwarding the scope may change.
The mailboxes in which the scope may send as the mailbox's own address.
Lists all users (admin only).
The scope's role in a mailbox: :manage, :send_as, :send_on_behalf,
:read, or nil when the mailbox is not the scope's. An admin has :manage in
every mailbox.
True when the identity provider said the session with this sid ended.
Remembers whether the calendar page shows the user's tasks (the "Show tasks" toggle). Default on.
Stores only the language (the language switch of a signed-in user).
Remembers which view the Tasks page opens in: list (the default) or board.
The scope of a session: the user it names, if enabled and if the session started after the
user's sessions_valid_after (see sign_out_everywhere/3), and, when the session carries the
identity provider's session id (sid), if that session has not ended there. nil otherwise.
Builds the scope for a user id: nil unless the user exists and is enabled.
Sets the role of an existing mailbox grant (admin only). Returns
{:error, :not_found} when the person has no grant on that mailbox.
Disables or re-enables a user (admin only). A disabled user cannot sign in,
their sessions stop working on the next request and all their app passwords
are rejected immediately. An admin cannot disable themselves, and the last
enabled admin can never be disabled ({:error, :last_admin}).
Sets a user's role to :admin or :user (admin only). The last enabled admin
cannot be demoted ({:error, :last_admin}). With OIDC_ADMIN_GROUP set, the
identity provider's group membership decides the role again at the next login.
Creates or updates the user for a successful OIDC login.
The identity provider says a session of the person ended (back-channel logout). An unknown subject is ignored, as the provider tells every application it knows.
Ends every session of the user at once: the ones that started before now stop working
(the next request or LiveView mount finds no scope) and open pages are disconnected.
reason goes to the audit log. Returns {:ok, user}.
Updates the scope's own display name, language and time zone. Returns the updated user.
Functions
@spec attach_mailbox( Kantoor.Accounts.Scope.t() | nil, Ecto.UUID.t(), Ecto.UUID.t(), Kantoor.Accounts.UserMailbox.role() ) :: {:ok, Kantoor.Accounts.UserMailbox.t()} | {:error, :unauthorized | Ecto.Changeset.t()}
Attaches a mailbox to a user with a role (admin only). Idempotent: an existing grant is left as it is, including its role.
@spec can_access_mailbox?( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t() | Ecto.UUID.t() | term() ) :: boolean()
Whether the scope's user may open the mailbox (a struct or an id). Every role may read.
@spec can_manage_mailbox?( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t() | Ecto.UUID.t() | term() ) :: boolean()
Whether the scope may change the mailbox's own settings (rules, away reply, forwarding).
@spec can_send_as?( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t() | Ecto.UUID.t() | term() ) :: boolean()
Whether the scope may send with the mailbox's address as From (send as).
@spec can_send_on_behalf?( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t() | Ecto.UUID.t() | term() ) :: boolean()
Whether the scope may send with the mailbox as Sender (send on behalf).
@spec can_write_mailbox?( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t() | Ecto.UUID.t() | term() ) :: boolean()
Whether the scope may change the mailbox's mail (flags, folders, moves, deletions). Every sending role may.
@spec change_profile(Kantoor.Accounts.User.t(), map()) :: Ecto.Changeset.t()
The profile changeset for the form.
@spec detach_mailbox(Kantoor.Accounts.Scope.t() | nil, Ecto.UUID.t(), Ecto.UUID.t()) :: :ok | {:error, :unauthorized}
Removes a mailbox grant (admin only).
@spec get_user(Ecto.UUID.t() | term()) :: Kantoor.Accounts.User.t() | nil
Gets a user by id; nil when missing.
@spec list_behalf_mailboxes(Kantoor.Accounts.Scope.t() | nil) :: [ Kantoor.Mailboxes.Mailbox.t() ]
The mailboxes the scope may send on behalf of (an identity of their own is still required).
@spec list_mailboxes(Kantoor.Accounts.Scope.t() | nil) :: [ Kantoor.Mailboxes.Mailbox.t() ]
The mailboxes the scope's user may open: every mailbox for an admin (their own attached ones first), the attached ones for a user, none without a (valid) scope.
@spec list_manageable_mailboxes(Kantoor.Accounts.Scope.t() | nil) :: [ Kantoor.Mailboxes.Mailbox.t() ]
The mailboxes whose rules, away reply and forwarding the scope may change.
@spec list_sendable_mailboxes(Kantoor.Accounts.Scope.t() | nil) :: [ Kantoor.Mailboxes.Mailbox.t() ]
The mailboxes in which the scope may send as the mailbox's own address.
@spec list_users(Kantoor.Accounts.Scope.t() | nil) :: {:ok, [Kantoor.Accounts.User.t()]} | {:error, :unauthorized}
Lists all users (admin only).
@spec mailbox_role( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t() | Ecto.UUID.t() | term() ) :: Kantoor.Accounts.UserMailbox.role() | nil
The scope's role in a mailbox: :manage, :send_as, :send_on_behalf,
:read, or nil when the mailbox is not the scope's. An admin has :manage in
every mailbox.
True when the identity provider said the session with this sid ended.
@spec put_calendar_show_tasks(Kantoor.Accounts.Scope.t() | nil, boolean()) :: :ok
Remembers whether the calendar page shows the user's tasks (the "Show tasks" toggle). Default on.
@spec put_locale(Kantoor.Accounts.Scope.t() | nil, String.t()) :: :ok
Stores only the language (the language switch of a signed-in user).
@spec put_tasks_view(Kantoor.Accounts.Scope.t() | nil, String.t()) :: :ok
Remembers which view the Tasks page opens in: list (the default) or board.
@spec scope_for_session(term(), integer() | nil, String.t() | nil) :: Kantoor.Accounts.Scope.t() | nil
The scope of a session: the user it names, if enabled and if the session started after the
user's sessions_valid_after (see sign_out_everywhere/3), and, when the session carries the
identity provider's session id (sid), if that session has not ended there. nil otherwise.
@spec scope_for_user_id(term()) :: Kantoor.Accounts.Scope.t() | nil
Builds the scope for a user id: nil unless the user exists and is enabled.
@spec set_mailbox_role( Kantoor.Accounts.Scope.t() | nil, Ecto.UUID.t(), Ecto.UUID.t(), Kantoor.Accounts.UserMailbox.role() ) :: {:ok, Kantoor.Accounts.UserMailbox.t()} | {:error, :unauthorized | :not_found | Ecto.Changeset.t()}
Sets the role of an existing mailbox grant (admin only). Returns
{:error, :not_found} when the person has no grant on that mailbox.
@spec set_user_disabled(Kantoor.Accounts.Scope.t() | nil, Ecto.UUID.t(), boolean()) :: {:ok, Kantoor.Accounts.User.t()} | {:error, :unauthorized | :not_found | :self | :last_admin}
Disables or re-enables a user (admin only). A disabled user cannot sign in,
their sessions stop working on the next request and all their app passwords
are rejected immediately. An admin cannot disable themselves, and the last
enabled admin can never be disabled ({:error, :last_admin}).
@spec set_user_role(Kantoor.Accounts.Scope.t() | nil, Ecto.UUID.t(), :admin | :user) :: {:ok, Kantoor.Accounts.User.t()} | {:error, :unauthorized | :not_found | :last_admin}
Sets a user's role to :admin or :user (admin only). The last enabled admin
cannot be demoted ({:error, :last_admin}). With OIDC_ADMIN_GROUP set, the
identity provider's group membership decides the role again at the next login.
@spec sign_in_from_claims(map(), keyword()) :: {:ok, Kantoor.Accounts.User.t()} | {:error, :disabled | :suspended | :invalid_claims | Ecto.Changeset.t()}
Creates or updates the user for a successful OIDC login.
claims are the verified ID token / userinfo claims. The user is found by
sub only. When :admin_group is given, the role follows membership of that
group in the groups claim on every login (so removal demotes); otherwise the
role of an existing user is left alone and new users are plain users.
A user the provisioning API invited (no sub yet) is linked by the first sign-in
whose verified email (email_verified is true) is theirs; nothing else links a
sub to an existing user by email.
Returns {:error, :disabled} for a disabled user, {:error, :suspended} for one
suspended through the provisioning API or with its domain, and
{:error, :invalid_claims} when sub or email is missing.
The identity provider says a session of the person ended (back-channel logout). An unknown subject is ignored, as the provider tells every application it knows.
With the provider's session id (sid) only that session ends: the sid is recorded and every
Kantoor session that carries it is refused and its LiveViews are sent to the login; the person's
other sessions (another browser, a session that started after) stay. A session that carries no
sid (the provider gave none, or it started before sessions carried it) cannot be named, so any
notice ends it (legacy_sessions_valid_after). Without a sid, every session of the person ends. App passwords (mail clients) are not sessions and are
not touched.
@spec sign_out_everywhere(Kantoor.Accounts.User.t(), String.t()) :: {:ok, Kantoor.Accounts.User.t()}
Ends every session of the user at once: the ones that started before now stop working
(the next request or LiveView mount finds no scope) and open pages are disconnected.
reason goes to the audit log. Returns {:ok, user}.
@spec update_profile(Kantoor.Accounts.Scope.t() | nil, map()) :: {:ok, Kantoor.Accounts.User.t()} | {:error, Ecto.Changeset.t()} | {:error, :unauthorized}
Updates the scope's own display name, language and time zone. Returns the updated user.