I have a Django app which has a single-account model. We are converting this to be multi-account, so almost every model will have a ForeignKey(Account)
.
What is the easiest way to make sure that each Account
(each account is in its own subdomain) can only access its own data? We have a middleware that populates the subdomain, and the current account on each request.
We can do this the hard way, by adding a filter(...., account = request.account)
in all of our views. This is not desirable as,
filter(...., account = request.account)
will be added to all of the queries, making this non-dry, repetative and error-prone.- Bigger risk is if there is a missing filter, anywhere it is security risk.