fix(auth): revoke API tokens when an admin is demoted (#6831) (#6832)

This commit is contained in:
Grzegorz
2026-11-03 05:53:59 +00:00
committed by GitHub
parent 7fdc97880a
commit c21980f5a6
3 changed files with 105 additions and 1 deletions
+11
View File
@@ -463,6 +463,17 @@ class AuthManager:
admin_count = sum(1 for d in self.users.values() if d.get("is_admin"))
if admin_count <= 1:
return SetAdminResult.LAST_ADMIN
# Token creation is admin-only and a demoted user can neither
# list nor revoke their tokens, so purge them with the role.
# Done before the flag flips: if the token store fails the
# exception propagates and the user is still an admin.
from core.database import get_db_session, ApiToken
with get_db_session() as db:
removed_tokens = db.query(ApiToken).filter(ApiToken.owner == username).delete()
if removed_tokens:
logger.info(
f"Revoked {removed_tokens} API token(s) owned by demoted admin '{username}'"
)
# Write order matters for lock-free readers: get_privileges()
# reads without _config_lock and trusts is_admin, so the admin
# flag must be flipped while the stored map is safe to expose —