Replies: 2 comments 1 reply
-
I just found the get_session method in starlette_admin: I am going to try override this, and I suspect that should work. I will update here if I am able to get it working. |
Beta Was this translation helpful? Give feedback.
-
I managed to get this working. I found that the easiest way was to top apply the row level security within the is_authenticated method within the AuthProvider. I think this first since it an authorization (or sort of). My is_authenticated method looks something like this: async def is_authenticated(self, request: Request) -> bool:
And my apply_row_level_security method is: async def apply_row_level_security(self, request, tenant_id): All seems to be working nicely now. |
Beta Was this translation helpful? Give feedback.
-
Hi All.
I have set up a multi-tenant app where different companies (tenants) can use the app.
To ensure that one company can not access the data of another by mistake, I have implemented row level security. This was done by creating a get_session() method, which only allows access to rows that contain information for that tenant (method is pasted below).
I am now hoping to use Starlette Admin all allow users from a given company to access their data (but no one else's data)
Is there a way with Starlette-admin to intercept the session used to access the data, and apply this row level security?
Alternately, can you suggest other ways to ensure that users from one tenant can not access data from another tenant?
Thanks!
async def get_session() -> AsyncGenerator[AsyncSession, None]:
async with async_session() as session:
try:
tenant_id = get_global_tenant_id()
query = text(f"SET app.current_tenant={tenant_id};")
await session.execute(text("SET SESSION ROLE tenant_user;"))
await session.execute(query)
yield session
except:
await session.rollback()
raise
finally:
await session.execute(text("RESET ROLE;"))
await session.commit()
Beta Was this translation helpful? Give feedback.
All reactions