refactor: use permission check service for isTeamAdmin (#24026)

* saml

* addGuests.handler.ts

* rename canAccess to canAccessOrganization and use organization.read

* update usages

* team update handler

* team member invite

* event type update

* remove isTeamAdmin util

* updateInternalNotesPresets

* setInviteExpiration

* event type update

* deleteInvite

* createInvite

* team publish

* fixes

* final

* address feedback

* update test
This commit is contained in:
Benny Joo authored and GitHub committed 2025-09-25 08:54:16 +01:00
1 parent 2ed0380e4f
commit 8d7cdf9d3b
21 files changed
+238 -145

No files matched your search

@@ -1,4 +1,4 @@
import { canAccess } from "@calcom/features/ee/sso/lib/saml";
import { canAccessOrganization } from "@calcom/features/ee/sso/lib/saml";
import prisma from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
@@ -16,7 +16,7 @@ const userCanCreateTeamGroupMapping = async (
});
}
const { message, access } = await canAccess(user, organizationId);
const { message, access } = await canAccessOrganization(user, organizationId);
if (!access) {
throw new TRPCError({
code: "BAD_REQUEST",
+19 -3
View File
@@ -1,7 +1,8 @@
import type { SAMLSSORecord, OIDCSSORecord } from "@boxyhq/saml-jackson";
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { HOSTED_CAL_FEATURES } from "@calcom/lib/constants";
import { isTeamAdmin } from "@calcom/features/ee/teams/lib/queries";
import { MembershipRole } from "@calcom/prisma/enums";
export const samlDatabaseUrl = process.env.SAML_DATABASE_URL || "";
export const isSAMLLoginEnabled = samlDatabaseUrl.length > 0;
@@ -28,7 +29,7 @@ export const isSAMLAdmin = (email: string) => {
return false;
};
export const canAccess = async (user: { id: number; email: string }, teamId: number | null) => {
export const canAccessOrganization = async (user: { id: number; email: string }, teamId: number | null) => {
const { id: userId, email } = user;
if (!isSAMLLoginEnabled) {
@@ -40,7 +41,22 @@ export const canAccess = async (user: { id: number; email: string }, teamId: num
// Hosted
if (HOSTED_CAL_FEATURES) {
if (teamId === null || !(await isTeamAdmin(userId, teamId))) {
if (teamId === null) {
return {
message: "dont_have_permission",
access: false,
};
}
const permissionCheckService = new PermissionCheckService();
const hasPermission = await permissionCheckService.checkPermission({
userId,
teamId,
permission: "organization.read",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
if (!hasPermission) {
return {
message: "dont_have_permission",
access: false,
-23
View File
@@ -385,29 +385,6 @@ export async function getTeamWithoutMembers(args: {
};
}
// also returns team
export async function isTeamAdmin(userId: number, teamId: number) {
const team = await prisma.membership.findFirst({
where: {
userId,
teamId,
accepted: true,
OR: [{ role: "ADMIN" }, { role: "OWNER" }],
},
include: {
team: {
select: {
metadata: true,
parentId: true,
isOrganization: true,
},
},
},
});
if (!team) return false;
return team;
}
export async function isTeamOwner(userId: number, teamId: number) {
return !!(await prisma.membership.findFirst({
where: {
@@ -1,6 +1,5 @@
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
import { isTeamAdmin } from "@calcom/features/ee/teams/lib/queries";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
@@ -97,12 +96,23 @@ class PBACRoleManager implements IRoleManager {
class LegacyRoleManager implements IRoleManager {
public isPBACEnabled = false;
async checkPermissionToChangeRole(userId: number, targetId: number, scope: "org" | "team"): Promise<void> {
const membership =
scope === "team"
? !!(await isTeamAdmin(userId, targetId))
: !!(await isOrganisationAdmin(userId, targetId));
let hasPermission = false;
if (scope === "team") {
const team = await prisma.membership.findFirst({
where: {
userId,
teamId: targetId,
accepted: true,
OR: [{ role: "ADMIN" }, { role: "OWNER" }],
},
});
hasPermission = !!team;
} else {
hasPermission = !!(await isOrganisationAdmin(userId, targetId));
}
// Only OWNER/ADMIN can update role
if (!membership) {
if (!hasPermission) {
throw new RoleManagementError(
"Only owners or admin can update roles",
RoleManagementErrorCode.UNAUTHORIZED