An Ash extension which generates the default linked account resource.
The linked account resource tracks links between user accounts within a browser session. Each link connects a "primary user" (the account that initiated the session) with a "linked user" (an additional account), scoped to a session token.
Usage
There is no need to define attributes, relationships, or actions manually. The extension generates them all for you.
defmodule MyApp.Accounts.LinkedAccount do
use Ash.Resource,
domain: MyApp.Accounts,
data_layer: ..., # any Ash data layer (AshPostgres, AshSqlite, ETS, etc.)
extensions: [AshMultiAccount.LinkedAccount]
multi_account do
user_resource MyApp.Accounts.User
end
endIf using a database-backed data layer, add the appropriate data layer config
(e.g. postgres do ... end for AshPostgres).
Generated Schema
The extension adds:
- Primary Key:
id(UUID, auto-generated if not already defined) - Attributes:
session_token(UUID),status(:active/:inactive),inserted_at,updated_at - Relationships:
primary_userandlinked_user(bothbelongs_toUser) - Identity: unique constraint on
[primary_user_id, linked_user_id, session_token] - Actions:
create_linked_account,get_linked_accounts,activate,deactivate,read,destroy - Calculation:
is_active?(module-based, checks status attribute)
Policies
No policies are added by default. You should add your own authorization policies. Recommended:
policies do
policy action_type(:read) do
authorize_if always()
end
policy action_type(:create) do
authorize_if relates_to_actor_via(:primary_user)
end
policy action_type([:update, :destroy]) do
authorize_if expr(primary_user_id == ^actor(:id))
end
endmulti_account
Configure multi-account options for the LinkedAccount resource.
Options
| Name | Type | Default | Docs |
|---|---|---|---|
user_resource | module | The User resource module that linked accounts belong to. | |
session_token_attribute_name | atom | :session_token | The name of the session token attribute. |
status_attribute_name | atom | :status | The name of the status attribute. |
primary_user_relationship_name | atom | :primary_user | The name of the primary_user belongs_to relationship. |
linked_user_relationship_name | atom | :linked_user | The name of the linked_user belongs_to relationship. |
create_action_name | atom | :create_linked_account | The name of the create linked account action. |
get_linked_accounts_action_name | atom | :get_linked_accounts | The name of the get linked accounts read action. |
activate_action_name | atom | :activate | The name of the activate action. |
deactivate_action_name | atom | :deactivate | The name of the deactivate action. |
read_action_name | atom | :read | The name of the default read action. |
destroy_action_name | atom | :destroy | The name of the default destroy action. |