fix: Exclude suspended admins from last admin checks (#14813)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-30 09:03:43 -05:00
1 parent 39f1b82919
commit 4b69469b40
3 files changed
+53 -7

No files matched your search

+23
View File
@@ -195,6 +195,29 @@ describe("user model", () => {
});
});
describe("suspend", () => {
it("should prevent suspending the last active admin", async () => {
const admin = await buildAdmin();
await buildAdmin({
teamId: admin.teamId,
suspendedAt: new Date(),
});
await expect(admin.update({ suspendedAt: new Date() })).rejects.toThrow(
"At least one admin is required"
);
});
it("should allow suspending an admin when another admin is active", async () => {
const admin = await buildAdmin();
await buildAdmin({ teamId: admin.teamId });
await admin.update({ suspendedAt: new Date() });
await admin.reload();
expect(admin.suspendedAt).toBeTruthy();
});
});
describe("getSessionToken", () => {
it("should set JWT secret", async () => {
const user = await buildUser();
+13 -7
View File
@@ -720,6 +720,7 @@ class User extends ParanoidModel<
where: {
teamId: model.teamId,
role: UserRole.Admin,
suspendedAt: null,
id: {
[Op.ne]: model.id,
},
@@ -764,14 +765,18 @@ class User extends ParanoidModel<
options: InstanceUpdateOptions<InferAttributes<User>>
) {
const previousRole = model.previous("role");
if (
const isDemoted =
model.changed("role") &&
previousRole === UserRole.Admin &&
UserRoleHelper.isRoleLower(model.role, UserRole.Admin)
) {
// Shares the lock name with the deletion checks, so a demotion and a
// deletion cannot each count the other account as the remaining admin.
UserRoleHelper.isRoleLower(model.role, UserRole.Admin);
const isSuspended =
model.changed("suspendedAt") &&
!model.previous("suspendedAt") &&
!!model.suspendedAt;
if (previousRole === UserRole.Admin && (isDemoted || isSuspended)) {
// Shares the lock name with the deletion checks, so a demotion,
// suspension or deletion cannot each count the other account as the
// remaining admin.
await LockHelper.acquire(
model.sequelize,
`users:${model.teamId}`,
@@ -782,6 +787,7 @@ class User extends ParanoidModel<
where: {
teamId: model.teamId,
role: UserRole.Admin,
suspendedAt: null,
id: {
[Op.ne]: model.id,
},
+17
View File
@@ -1018,6 +1018,23 @@ describe("#users.delete", () => {
expect(res.status).toEqual(400);
});
it("should not allow deleting self when the only other admin is suspended", async () => {
const admin = await buildAdmin();
await buildAdmin({
teamId: admin.teamId,
suspendedById: admin.id,
suspendedAt: new Date(),
});
const res = await server.post("/api/users.delete", admin, {
body: {
code: admin.deleteConfirmationCode,
},
});
const body = await res.json();
expect(res.status).toEqual(400);
expect(body.message).toContain("Cannot delete account as only admin");
});
it("should require correct code when no id passed", async () => {
const user = await buildAdmin();
await buildUser({