Check if a user is a superadmin by querying the database directly.
(user_id: int, db_session: AsyncSession)
| 9 | |
| 10 | |
| 11 | async def is_user_superadmin(user_id: int, db_session: AsyncSession) -> bool: |
| 12 | """Check if a user is a superadmin by querying the database directly.""" |
| 13 | _cache = getattr(db_session, '_superadmin_cache', None) |
| 14 | if _cache is None: |
| 15 | db_session._superadmin_cache = {} |
| 16 | _cache = db_session._superadmin_cache |
| 17 | if user_id in _cache: |
| 18 | return _cache[user_id] |
| 19 | result = (await db_session.execute(select(User.is_superadmin).where(User.id == user_id))).scalars().first() |
| 20 | value = bool(result) |
| 21 | _cache[user_id] = value |
| 22 | return value |
| 23 | |
| 24 | |
| 25 | async def _get_current_user_lazy(request: Request, db_session: AsyncSession = Depends(get_db_session)): |