How to implement multi tenancy in user with unique email per site and global

Hi, sorry. I need to have an email global and email which is assigned to a site in my system For example:
email1@g.com no-site
email1@g.com site1
email1@g.com site2
email1@g.com no-site
email1@g.com site1
email1@g.com site2
So i set multitenancy like this in my user resource
multitenancy do
strategy :attribute
attribute :site_id
global? true
# parse_attribute {MishkaCms.Runtime.Resources.Helpers, :parse_tenant, []}
end
multitenancy do
strategy :attribute
attribute :site_id
global? true
# parse_attribute {MishkaCms.Runtime.Resources.Helpers, :parse_tenant, []}
end
And the relationships in user
relationships do
belongs_to :site, MishkaCms.Runtime.Site do
allow_nil? true
description "The user belongs to (nil for global user)"
end
...
end
relationships do
belongs_to :site, MishkaCms.Runtime.Site do
allow_nil? true
description "The user belongs to (nil for global user)"
end
...
end
And the last part, I change the unique_email to this, the nils_distinct help me to have nil here
identities do
identity :unique_email, [:email, :site_id] do
description "The email must be unique for the site"
all_tenants? true
nils_distinct? false
end
end
identities do
identity :unique_email, [:email, :site_id] do
description "The email must be unique for the site"
all_tenants? true
nils_distinct? false
end
end
I am using this approach in another resources and they are okey but for user!! i have this error
** (Spark.Error.DslError) [MishkaCms.Accounts.User]
identities -> identity:
The `:email` attribute on the resource `MishkaCms.Accounts.User` should be uniquely constrained
(elixir 1.18.4) lib/process.ex:896: Process.info/2
(spark 2.2.67) lib/spark/error/dsl_error.ex:30: Spark.Error.DslError.exception/1
...
** (Spark.Error.DslError) [MishkaCms.Accounts.User]
identities -> identity:
The `:email` attribute on the resource `MishkaCms.Accounts.User` should be uniquely constrained
(elixir 1.18.4) lib/process.ex:896: Process.info/2
(spark 2.2.67) lib/spark/error/dsl_error.ex:30: Spark.Error.DslError.exception/1
...
I think some strategies like password and magic_link should be changed!! and the token needs to have site in is clame too Could you please help me where should i start 😂 not to change all things!! i read the docs but i could not find to fix my problem maybe i missed something Thank you in advance Gist: https://gist.github.com/shahryarjb/bdb32c98967d315095e131637de45e39
Solution:
It is automatically added by virtue of being the multitenancy attribute
Jump to solution
3 Replies
Shahryar
ShahryarOP•2mo ago
Sorry for sending the post again. I tried to remove the uniqueness constraint on some fields like email in each section so that they could be read from identities instead. But unfortunately, other errors such as missing username started to appear. Do I need a custom strategy for this?
ZachDaniel
ZachDaniel•2mo ago
You don't need to include site_id in the identity fields
Solution
ZachDaniel
ZachDaniel•2mo ago
It is automatically added by virtue of being the multitenancy attribute

Did you find this page helpful?