* PBAC on team members page + org members page to use specific permissions with fallbacks * Implement checks on edit sheet fetch to check for permisisons * WIP permission fixes * fix tests * WIP * fix edit mode perms * fix remove and list view permissions * fix add * feat: add pbac permissions for impersonation (#23035) * registery + migration + i18n * UI for can impersonate * impersonation backend * remove permission from db query * add missing prisma import * add resend invitation and edit mode checks for delete/changeRole * fix canChangeRole * canChangeRole gate * fix permission logic * fix depends on * push migration * fix migration * fix scoped advanced permissions * use listMembers instead of invite proxy * remove isOrgAdmin check to rely on pbac * wait for session to load before navigating * use event-types testId to ensure session loaded * use profile page instead of eventTypes * use correct roles * fix wait for session * correct orgs private isFallBackRoles * set a timeout on waiting for session * remove adminOrOwner check since its in fallpack --------- Co-authored-by: Volnei Munhoz <volnei.munhoz@gmail.com> Co-authored-by: Eunjae Lee <hey@eunjae.dev>
151 lines
3.9 KiB
TypeScript
151 lines
3.9 KiB
TypeScript
import { TeamBilling } from "@calcom/ee/billing/teams";
|
|
import { Resource, CustomAction } from "@calcom/features/pbac/domain/types/permission-registry";
|
|
import { getSpecificPermissions } from "@calcom/features/pbac/lib/resource-permissions";
|
|
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
|
|
import { prisma } from "@calcom/prisma";
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../types";
|
|
import type { TBulkUsersDelete } from "./bulkDeleteUsers.schema.";
|
|
|
|
type BulkDeleteUsersHandler = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TBulkUsersDelete;
|
|
};
|
|
|
|
export async function bulkDeleteUsersHandler({ ctx, input }: BulkDeleteUsersHandler) {
|
|
const currentUser = ctx.user;
|
|
const currentUserOrgId = currentUser.organizationId ?? currentUser.profiles[0].organizationId;
|
|
|
|
if (!currentUserOrgId) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
// Get user's membership role in the organization
|
|
const membership = await prisma.membership.findFirst({
|
|
where: {
|
|
userId: currentUser.id,
|
|
teamId: currentUserOrgId,
|
|
},
|
|
select: {
|
|
role: true,
|
|
},
|
|
});
|
|
|
|
if (!membership) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED", message: "User is not a member of this organization." });
|
|
}
|
|
|
|
// Check PBAC permissions for removing organization members
|
|
const permissions = await getSpecificPermissions({
|
|
userId: currentUser.id,
|
|
teamId: currentUserOrgId,
|
|
resource: Resource.Organization,
|
|
userRole: membership.role,
|
|
actions: [CustomAction.Remove],
|
|
fallbackRoles: {
|
|
[CustomAction.Remove]: {
|
|
roles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!permissions[CustomAction.Remove]) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
|
|
// Loop over all users in input.userIds and remove all memberships for the organization including child teams
|
|
const deleteMany = prisma.membership.deleteMany({
|
|
where: {
|
|
userId: {
|
|
in: input.userIds,
|
|
},
|
|
team: {
|
|
OR: [
|
|
{
|
|
parentId: currentUserOrgId,
|
|
},
|
|
{ id: currentUserOrgId },
|
|
],
|
|
},
|
|
},
|
|
});
|
|
|
|
const removeOrgrelation = prisma.user.updateMany({
|
|
where: {
|
|
id: {
|
|
in: input.userIds,
|
|
},
|
|
},
|
|
data: {
|
|
organizationId: null,
|
|
// Set username to null - to make sure there is no conflicts
|
|
username: null,
|
|
// Set completedOnboarding to false - to make sure the user has to complete onboarding again -> Setup a new username
|
|
completedOnboarding: false,
|
|
},
|
|
});
|
|
|
|
const removeManagedEventTypes = prisma.eventType.deleteMany({
|
|
where: {
|
|
userId: {
|
|
in: input.userIds,
|
|
},
|
|
parent: {
|
|
team: {
|
|
OR: [
|
|
{
|
|
parentId: currentUserOrgId,
|
|
},
|
|
{ id: currentUserOrgId },
|
|
],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const removeHostAssignment = prisma.host.deleteMany({
|
|
where: {
|
|
userId: {
|
|
in: input.userIds,
|
|
},
|
|
eventType: {
|
|
team: {
|
|
OR: [
|
|
{
|
|
parentId: currentUserOrgId,
|
|
},
|
|
{ id: currentUserOrgId },
|
|
],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const removeProfiles = ProfileRepository.deleteMany({
|
|
userIds: input.userIds,
|
|
});
|
|
|
|
// We do this in a transaction to make sure that all memberships are removed before we remove the organization relation from the user
|
|
// We also do this to make sure that if one of the queries fail, the whole transaction fails
|
|
await prisma.$transaction([
|
|
removeProfiles,
|
|
deleteMany,
|
|
removeOrgrelation,
|
|
removeManagedEventTypes,
|
|
removeHostAssignment,
|
|
]);
|
|
|
|
const teamBilling = await TeamBilling.findAndInit(currentUserOrgId);
|
|
await teamBilling.updateQuantity();
|
|
|
|
return {
|
|
success: true,
|
|
usersDeleted: input.userIds.length,
|
|
};
|
|
}
|
|
|
|
export default bulkDeleteUsersHandler;
|