Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vi, describe, it, expect, beforeEach } from "vitest";

import { FeaturesRepository } from "@calcom/features/flags/features.repository";
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
import { isOrganisationAdmin, isOrganisationOwner } from "@calcom/lib/server/queries/organisations";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";

Expand All @@ -26,6 +26,7 @@ vi.mock("@calcom/prisma", () => ({
}));
vi.mock("@calcom/lib/server/queries/organisations", () => ({
isOrganisationAdmin: vi.fn(),
isOrganisationOwner: vi.fn(),
}));

describe("RoleManagementFactory", () => {
Expand Down Expand Up @@ -407,6 +408,36 @@ describe("RoleManagementFactory", () => {
)
);
});

it("should prevent changing admin to owner", async () => {
vi.mocked(isOrganisationAdmin).mockResolvedValue({
id: membershipId,
userId,
teamId: organizationId,
role: MembershipRole.ADMIN,
accepted: true,
disableImpersonation: false,
createdAt: new Date(),
updatedAt: new Date(),
customRoleId: null,
});
vi.mocked(isOrganisationOwner).mockResolvedValue(false);
const manager = await factory.createRoleManager(organizationId);
await expect(
manager.checkPermissionToChangeRole(
userId,
organizationId,
"org",
membershipId,
MembershipRole.OWNER
)
).rejects.toThrow(
new RoleManagementError(
"Only owners or admin can update roles",
RoleManagementErrorCode.UNAUTHORIZED
)
);
});
});

describe("assignRole", () => {
Expand Down
13 changes: 8 additions & 5 deletions packages/features/pbac/services/legacy-role-manager.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isTeamOwner } from "@calcom/features/ee/teams/lib/queries";
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
import { isOrganisationAdmin, isOrganisationOwner } from "@calcom/lib/server/queries/organisations";
import { prisma } from "@calcom/prisma";
import type { Membership } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
Expand Down Expand Up @@ -84,7 +84,10 @@ export class LegacyRoleManager implements IRoleManager {
});
hasPermission = !!team;
} else {
hasPermission = !!(await isOrganisationAdmin(userId, targetId));
hasPermission =
newRole === MembershipRole.OWNER
? !!(await isOrganisationOwner(userId, targetId))
: !!(await isOrganisationAdmin(userId, targetId));
}

// Only OWNER/ADMIN can update role
Expand Down Expand Up @@ -112,7 +115,7 @@ export class LegacyRoleManager implements IRoleManager {
organizationId: number,
role: MembershipRole | string,
// Used in other implementation

_membershipId: number
): Promise<void> {
await prisma.membership.update({
Expand All @@ -129,7 +132,7 @@ export class LegacyRoleManager implements IRoleManager {
}

// Used in other implementation

async getAllRoles(_organizationId: number): Promise<{ id: string; name: string }[]> {
return [
{ id: MembershipRole.OWNER, name: "Owner" },
Expand All @@ -139,7 +142,7 @@ export class LegacyRoleManager implements IRoleManager {
}

// Used in other implementation

async getTeamRoles(_teamId: number): Promise<{ id: string; name: string }[]> {
return [
{ id: MembershipRole.OWNER, name: "Owner" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => {
if (!organizationId)
throw new TRPCError({ code: "UNAUTHORIZED", message: "You must be a member of an organizaiton" });

const roleManager = await RoleManagementFactory.getInstance().createRoleManager(organizationId);

try {
await roleManager.checkPermissionToChangeRole(userId, organizationId, "org");
} catch (error) {
if (error instanceof RoleManagementError) {
throw new TRPCError({ code: "UNAUTHORIZED", message: error.message });
}
throw error;
}

await ensureOrganizationIsReviewed(organizationId);

// Is requested user a member of the organization?
const requestedMember = await prisma.membership.findFirst({
where: {
Expand Down Expand Up @@ -96,6 +83,25 @@ export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => {
if (!requestedMember)
throw new TRPCError({ code: "UNAUTHORIZED", message: "User does not belong to your organization" });

const roleManager = await RoleManagementFactory.getInstance().createRoleManager(organizationId);

try {
await roleManager.checkPermissionToChangeRole(
userId,
organizationId,
"org",
requestedMember.id,
input.role
);
} catch (error) {
if (error instanceof RoleManagementError) {
throw new TRPCError({ code: "UNAUTHORIZED", message: error.message });
}
throw error;
}

await ensureOrganizationIsReviewed(organizationId);

const hasUsernameUpdated = input.username !== requestedMember.user.profiles[0]?.username;

if (input.username && hasUsernameUpdated && user.profile.organization?.slug) {
Expand Down
Loading