feat: enable PBAC checking on organization settings page (#22467)
This commit is contained in:
1 parent
5360cb511d
commit
4b2f89d6e8
26 files changed
+936
-105
No files matched your search
@@ -1,7 +1,9 @@
|
||||
import { Resource } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { getResourcePermissions } from "@calcom/features/pbac/lib/resource-permissions";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { Attribute } from "@calcom/prisma/client";
|
||||
import { Prisma } from "@calcom/prisma/client";
|
||||
import { Prisma, MembershipRole } from "@calcom/prisma/client";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
@@ -28,6 +30,42 @@ const createAttributesHandler = async ({ input, ctx }: GetOptions) => {
|
||||
});
|
||||
}
|
||||
|
||||
const membership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
},
|
||||
select: {
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You need to be apart of this organization to use this feature",
|
||||
});
|
||||
}
|
||||
|
||||
const { canCreate } = await getResourcePermissions({
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
resource: Resource.Attributes,
|
||||
userRole: membership.role,
|
||||
fallbackRoles: {
|
||||
create: {
|
||||
roles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!canCreate) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You don't have permission to create attributes",
|
||||
});
|
||||
}
|
||||
|
||||
const slug = slugify(input.name);
|
||||
const uniqueOptions = getOptionsWithValidContains(input.options);
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Resource } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { getResourcePermissions } from "@calcom/features/pbac/lib/resource-permissions";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/client";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
@@ -22,6 +25,42 @@ const deleteAttributeHandler = async ({ input, ctx }: DeleteOptions) => {
|
||||
});
|
||||
}
|
||||
|
||||
const membership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
},
|
||||
select: {
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You need to be apart of this organization to use this feature",
|
||||
});
|
||||
}
|
||||
|
||||
const { canDelete } = await getResourcePermissions({
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
resource: Resource.Attributes,
|
||||
userRole: membership.role,
|
||||
fallbackRoles: {
|
||||
delete: {
|
||||
roles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!canDelete) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You don't have permission to delete attributes",
|
||||
});
|
||||
}
|
||||
|
||||
const attribute = await prisma.attribute.delete({
|
||||
where: {
|
||||
teamId: org.id,
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { Resource } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { getResourcePermissions } from "@calcom/features/pbac/lib/resource-permissions";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/client";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
@@ -24,6 +27,42 @@ const editAttributesHandler = async ({ input, ctx }: GetOptions) => {
|
||||
});
|
||||
}
|
||||
|
||||
const membership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
},
|
||||
select: {
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You need to be apart of this organization to use this feature",
|
||||
});
|
||||
}
|
||||
|
||||
const { canEdit } = await getResourcePermissions({
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
resource: Resource.Attributes,
|
||||
userRole: membership.role,
|
||||
fallbackRoles: {
|
||||
update: {
|
||||
roles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!canEdit) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You don't have permission to edit attributes",
|
||||
});
|
||||
}
|
||||
|
||||
// If an option is removed, it is to be removed from contains of corresponding group as well if any
|
||||
const options = getOptionsWithValidContains(input.options);
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Resource } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { getResourcePermissions } from "@calcom/features/pbac/lib/resource-permissions";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/client";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
@@ -22,6 +25,42 @@ const toggleActiveHandler = async ({ input, ctx }: GetOptions) => {
|
||||
});
|
||||
}
|
||||
|
||||
const membership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
},
|
||||
select: {
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You need to be apart of this organization to use this feature",
|
||||
});
|
||||
}
|
||||
|
||||
const { canEdit } = await getResourcePermissions({
|
||||
userId: ctx.user.id,
|
||||
teamId: org.id,
|
||||
resource: Resource.Attributes,
|
||||
userRole: membership.role,
|
||||
fallbackRoles: {
|
||||
update: {
|
||||
roles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!canEdit) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You don't have permission to modify attributes",
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure that this users org owns the attribute
|
||||
const attribute = await prisma.attribute.findUnique({
|
||||
where: {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
|
||||
import { Resource } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { getResourcePermissions } from "@calcom/features/pbac/lib/resource-permissions";
|
||||
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
||||
import { getMetadataHelpers } from "@calcom/lib/getMetadataHelpers";
|
||||
import { uploadLogo } from "@calcom/lib/server/avatar";
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
import { resizeBase64Image } from "@calcom/lib/server/resizeBase64Image";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { UserPermissionRole } from "@calcom/prisma/enums";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
@@ -109,12 +110,33 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
// A user can only have one org so we pass in their currentOrgId here
|
||||
const currentOrgId = ctx.user?.organization?.id || input.orgId;
|
||||
|
||||
const isUserOrganizationAdmin = currentOrgId && (await isOrganisationAdmin(ctx.user?.id, currentOrgId));
|
||||
const isUserRoleAdmin = ctx.user.role === UserPermissionRole.ADMIN;
|
||||
if (!currentOrgId) throw new TRPCError({ code: "BAD_REQUEST", message: "Organization ID is required." });
|
||||
|
||||
const isUserAuthorizedToUpdate = !!(isUserOrganizationAdmin || isUserRoleAdmin);
|
||||
const membership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
teamId: currentOrgId,
|
||||
},
|
||||
select: {
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!currentOrgId || !isUserAuthorizedToUpdate) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
if (!membership) throw new TRPCError({ code: "UNAUTHORIZED", message: "User role is required." });
|
||||
|
||||
const { canEdit } = await getResourcePermissions({
|
||||
userId: ctx.user.id,
|
||||
teamId: currentOrgId,
|
||||
resource: Resource.Organization,
|
||||
userRole: membership.role,
|
||||
fallbackRoles: {
|
||||
update: {
|
||||
roles: [MembershipRole.OWNER, MembershipRole.ADMIN],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!canEdit) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
if (input.slug) {
|
||||
const userConflict = await prisma.team.findMany({
|
||||
|
||||
Reference in new issue
Block a user