diff --git a/server/routes/api/users/users.test.ts b/server/routes/api/users/users.test.ts index ee884ac3eb..fec3e1bd85 100644 --- a/server/routes/api/users/users.test.ts +++ b/server/routes/api/users/users.test.ts @@ -1266,6 +1266,34 @@ describe("#users.update_role", () => { }); expect(res.status).toEqual(400); }); + + it("should return the same error for a user in another team", async () => { + const admin = await buildAdmin(); + const user = await buildAdmin(); + + for (const role of [UserRole.Admin, UserRole.Member]) { + const res = await server.post("/api/users.update_role", admin, { + body: { + id: user.id, + role, + }, + }); + expect(res.status).toEqual(403); + } + expect((await user.reload()).role).toEqual(UserRole.Admin); + }); + + it("should return the same error for an unknown user", async () => { + const admin = await buildAdmin(); + + const res = await server.post("/api/users.update_role", admin, { + body: { + id: faker.string.uuid(), + role: UserRole.Admin, + }, + }); + expect(res.status).toEqual(403); + }); }); describe("#users.promote", () => { diff --git a/server/routes/api/users/users.ts b/server/routes/api/users/users.ts index d1c21b8b34..ca50171450 100644 --- a/server/routes/api/users/users.ts +++ b/server/routes/api/users/users.ts @@ -371,10 +371,11 @@ async function updateRole(ctx: APIContext) { const actor = ctx.state.auth.user; const user = await User.findByPk(userId, { - rejectOnEmpty: true, transaction, lock: transaction.LOCK.UPDATE, }); + authorize(actor, "update", user); + await Team.findByPk(user.teamId, { transaction, lock: transaction.LOCK.UPDATE,