diff --git a/server/models/User.test.ts b/server/models/User.test.ts index 51d3c1ce29..89c2c41ee2 100644 --- a/server/models/User.test.ts +++ b/server/models/User.test.ts @@ -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(); diff --git a/server/models/User.ts b/server/models/User.ts index db558d087e..9c1e1577a0 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -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> ) { 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, }, diff --git a/server/routes/api/users/users.test.ts b/server/routes/api/users/users.test.ts index 1f0dca08c3..f0ff7c90c0 100644 --- a/server/routes/api/users/users.test.ts +++ b/server/routes/api/users/users.test.ts @@ -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({