carlgleisner
AshAuthentication multitenancy org uses attribute, user uses context
Hey ![]()
I’ve read through the posts on using AshAuthentication and multitenancy and couldn’t find an appropriate fit for this.
The title of this post may need massaging if this cannot be written off immediately.
Scenario
Using a clean (generated) slate, I’ve created a new resource Organization which uses the :attribute strategy whereas the User and Token resources have been adapted to use the :context strategy. This is further explained below and also published as a preproduction on GitHub.
Running the registration actions on User with the correct tenant set in AshAdmin, I get an error from Myapp.Accounts.Token.store_confirmation_changes saying:
Queries against the Myapp.Accounts.Token resource require a tenant to be specified
I get the same result from running the code in an iex session (please see the README of the reproduction).
The changes from the generated code are set out in this commit:
This is all set out in the README of reproduction repo, but I paste the pertinent sections of the resources below.
Asking humbly, is there something manifestly wrong with this setup?
Thankful for any input ![]()
The resources
User and Token
multitenancy do
strategy :context
end
Organization
postgres do
table "organizations"
repo Myapp.Repo
manage_tenant do
template ["org_", :subdomain]
create? true
update? false
end
end
multitenancy do
strategy :attribute
attribute :subdomain
global? true
end
To juggle resources using different multitenancy strategies, the Ash.ToTenant protocol introspects the resources and returns either the org_subdomain form or the attrubute subdomain.
defimpl Ash.ToTenant do
def to_tenant(%{subdomain: subdomain}, resource) do
if Ash.Resource.Info.data_layer(resource) == AshPostgres.DataLayer &&
Ash.Resource.Info.multitenancy_strategy(resource) == :context do
"org_#{subdomain}"
else
subdomain
end
end
end
Perhaps it is worth noting that the :subdomain attribute on Organization is of the type :ci_string.
attribute :subdomain, :ci_string do
public? true
allow_nil? false
end
Marked As Solved
zachdaniel
I feel bad, I just didn’t push the fix up properly last night
Only had a few and didn’t realize that the push failed. Anyway, fixed in main, will be released next week ![]()
Also Liked
zachdaniel
zachdaniel
zachdaniel
Last Post!
carlgleisner
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









