ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.53k stars 262 forks source link

Fix global record validations with existing scope #294

Closed mikecmpbll closed 1 year ago

mikecmpbll commented 1 year ago

Due to how the args object was being (man)handled, we were leaking the tenant scope into the global scope validation where it shouldn't have been. This only happened when there was a scope argument supplied to validates_uniquness_to_tenant.

mikecmpbll commented 1 year ago

to expand on this a little; the # if tenant is set (instance is not global) and records can be global - validating within records with blank tenant validation query was:

SELECT 1 AS one
FROM "projects"
WHERE "projects"."name" = ?
  AND "projects"."user_defined_scope" = ?
  AND "projects"."account_id" = ?
  AND "projects"."account_id" IS NULL
LIMIT ?

the account_id = <tenant_id> portion of the query doesn't belong there, and was leaking into args[:scope] from how validation_args was being built for the main validation.