feat: org owner-admin cant see the teams created by other owner-admin 9982 cal 2120 (#10430)
* WIP * New files for other teams * Update code domain model location * Fixes for finding teams in orgs * VerticalTabs remove other teams appearance and only display if org admin * Added conditional to consider orgs admin when updating team profile * team-member-list wip * Update memberInviteModal with disableCopyLink prop * cleaning up * Undo changes to files * remove logs * clean up * undo changes * undo change * Undo changes * Reset files to main * typo on import * Update packages/features/ee/organizations/pages/components/OtherTeamListItem.tsx Co-authored-by: Omar López <[email protected]> * useSearchParams hook instead of useRouter * remove unused code and clean up * Fix type --------- Co-authored-by: Omar López <[email protected]>
This commit is contained in:
1 parent
95dee6dd37
commit
871e17a865
23 files changed
+1602
-188
No files matched your search
@@ -174,4 +174,13 @@ export const isAdminMiddleware = isAuthed.unstable_pipe(({ ctx, next }) => {
|
||||
return next({ ctx: { ...ctx, user: user } });
|
||||
});
|
||||
|
||||
// Org admins can be admins or owners
|
||||
export const isOrgAdminMiddleware = isAuthed.unstable_pipe(({ ctx, next }) => {
|
||||
const { user } = ctx;
|
||||
if (!user?.organization?.isOrgAdmin) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
return next({ ctx: { ...ctx, user: user } });
|
||||
});
|
||||
|
||||
export default sessionMiddleware;
|
||||
@@ -1,5 +1,5 @@
|
||||
import perfMiddleware from "../middlewares/perfMiddleware";
|
||||
import { isAdminMiddleware, isAuthed } from "../middlewares/sessionMiddleware";
|
||||
import { isAdminMiddleware, isAuthed, isOrgAdminMiddleware } from "../middlewares/sessionMiddleware";
|
||||
import { procedure } from "../trpc";
|
||||
import publicProcedure from "./publicProcedure";
|
||||
|
||||
@@ -27,5 +27,6 @@ const authedProcedure = procedure.use(perfMiddleware).use(isAuthed);
|
||||
/*export const authedRateLimitedProcedure = ({ intervalInMs, limit }: IRateLimitOptions) =>
|
||||
authedProcedure.use(isRateLimitedByUserIdMiddleware({ intervalInMs, limit }));*/
|
||||
export const authedAdminProcedure = publicProcedure.use(isAdminMiddleware);
|
||||
export const authedOrgAdminProcedure = publicProcedure.use(isOrgAdminMiddleware);
|
||||
|
||||
export default authedProcedure;
|
||||
@@ -1,12 +1,17 @@
|
||||
import { ZVerifyCodeInputSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import authedProcedure, { authedAdminProcedure } from "../../../procedures/authedProcedure";
|
||||
import authedProcedure, {
|
||||
authedAdminProcedure,
|
||||
authedOrgAdminProcedure,
|
||||
} from "../../../procedures/authedProcedure";
|
||||
import { router } from "../../../trpc";
|
||||
import { ZAdminVerifyInput } from "./adminVerify.schema";
|
||||
import { ZCreateInputSchema } from "./create.schema";
|
||||
import { ZCreateTeamsSchema } from "./createTeams.schema";
|
||||
import { ZGetMembersInput } from "./getMembers.schema";
|
||||
import { ZGetOtherTeamInputSchema } from "./getOtherTeam.handler";
|
||||
import { ZListMembersSchema } from "./listMembers.schema";
|
||||
import { ZListOtherTeamMembersSchema } from "./listOtherTeamMembers.handler";
|
||||
import { ZSetPasswordSchema } from "./setPassword.schema";
|
||||
import { ZUpdateInputSchema } from "./update.schema";
|
||||
|
||||
@@ -24,6 +29,9 @@ type OrganizationsRouterHandlerCache = {
|
||||
listMembers?: typeof import("./listMembers.handler").listMembersHandler;
|
||||
getBrand?: typeof import("./getBrand.handler").getBrandHandler;
|
||||
getMembers?: typeof import("./getMembers.handler").getMembersHandler;
|
||||
listOtherTeams?: typeof import("./listOtherTeams.handler").listOtherTeamHandler;
|
||||
getOtherTeam?: typeof import("./getOtherTeam.handler").getOtherTeamHandler;
|
||||
listOtherTeamMembers?: typeof import("./listOtherTeamMembers.handler").listOtherTeamMembers;
|
||||
};
|
||||
|
||||
const UNSTABLE_HANDLER_CACHE: OrganizationsRouterHandlerCache = {};
|
||||
@@ -227,4 +235,56 @@ export const viewerOrganizationsRouter = router({
|
||||
ctx,
|
||||
});
|
||||
}),
|
||||
listOtherTeams: authedOrgAdminProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeams) {
|
||||
UNSTABLE_HANDLER_CACHE.listOtherTeams = await import("./listOtherTeams.handler").then(
|
||||
(mod) => mod.listOtherTeamHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeams) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.listOtherTeams({
|
||||
ctx,
|
||||
});
|
||||
}),
|
||||
getOtherTeam: authedOrgAdminProcedure.input(ZGetOtherTeamInputSchema).query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.getOtherTeam) {
|
||||
UNSTABLE_HANDLER_CACHE.getOtherTeam = await import("./getOtherTeam.handler").then(
|
||||
(mod) => mod.getOtherTeamHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.getOtherTeam) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.getOtherTeam({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
listOtherTeamMembers: authedOrgAdminProcedure
|
||||
.input(ZListOtherTeamMembersSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeamMembers) {
|
||||
UNSTABLE_HANDLER_CACHE.listOtherTeamMembers = await import("./listOtherTeamMembers.handler").then(
|
||||
(mod) => mod.listOtherTeamMembers
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeamMembers) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.listOtherTeamMembers({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
|
||||
export const ZGetOtherTeamInputSchema = z.object({
|
||||
teamId: z.number(),
|
||||
});
|
||||
|
||||
export type TGetOtherTeamInputSchema = z.infer<typeof ZGetOtherTeamInputSchema>;
|
||||
|
||||
type GetOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TGetOtherTeamInputSchema;
|
||||
};
|
||||
|
||||
export const getOtherTeamHandler = async ({ input }: GetOptions) => {
|
||||
// No need to validate if user is admin of org as we already do that on authedOrgAdminProcedure
|
||||
const team = await prisma.team.findFirst({
|
||||
where: {
|
||||
id: input.teamId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
slug: true,
|
||||
logo: true,
|
||||
bio: true,
|
||||
metadata: true,
|
||||
isPrivate: true,
|
||||
parent: {
|
||||
select: {
|
||||
id: true,
|
||||
slug: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Team not found." });
|
||||
}
|
||||
|
||||
return {
|
||||
...team,
|
||||
safeBio: markdownToSafeHTML(team.bio),
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,81 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import z from "zod";
|
||||
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
|
||||
export const ZListOtherTeamMembersSchema = z.object({
|
||||
teamId: z.number(),
|
||||
query: z.string().optional(),
|
||||
limit: z.number().optional(),
|
||||
offset: z.number().optional(),
|
||||
});
|
||||
|
||||
export type TListOtherTeamMembersSchema = z.infer<typeof ZListOtherTeamMembersSchema>;
|
||||
|
||||
type ListOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TListOtherTeamMembersSchema;
|
||||
};
|
||||
|
||||
export const listOtherTeamMembers = async ({ ctx, input }: ListOptions) => {
|
||||
const whereConditional: Prisma.MembershipWhereInput = {
|
||||
teamId: input.teamId,
|
||||
};
|
||||
const { limit = 20 } = input;
|
||||
let { offset = 0 } = input;
|
||||
|
||||
if (input.query) {
|
||||
offset = 0;
|
||||
whereConditional.user = {
|
||||
OR: [
|
||||
{
|
||||
username: {
|
||||
contains: input.query,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: {
|
||||
contains: input.query,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
email: {
|
||||
contains: input.query,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const members = await prisma.membership.findMany({
|
||||
where: whereConditional,
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
accepted: true,
|
||||
disableImpersonation: true,
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
name: true,
|
||||
email: true,
|
||||
avatar: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
distinct: ["userId"],
|
||||
orderBy: { role: "desc" },
|
||||
take: limit,
|
||||
skip: offset,
|
||||
});
|
||||
|
||||
return members;
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
|
||||
type ListOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
};
|
||||
|
||||
export const listOtherTeamHandler = async ({ ctx }: ListOptions) => {
|
||||
const teamsInOrgIamNotPartOf = await prisma.membership.findMany({
|
||||
where: {
|
||||
userId: {
|
||||
not: ctx.user.id,
|
||||
},
|
||||
team: {
|
||||
parent: {
|
||||
is: {
|
||||
id: ctx.user?.organization?.id,
|
||||
},
|
||||
},
|
||||
members: {
|
||||
none: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
},
|
||||
orderBy: { role: "desc" },
|
||||
distinct: ["teamId"],
|
||||
});
|
||||
|
||||
return teamsInOrgIamNotPartOf.map(({ team, ...membership }) => ({
|
||||
role: membership.role,
|
||||
accepted: membership.accepted,
|
||||
isOrgAdmin: true,
|
||||
...team,
|
||||
}));
|
||||
};
|
||||
@@ -19,7 +19,13 @@ type UpdateOptions = {
|
||||
};
|
||||
|
||||
export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
if (!(await isTeamAdmin(ctx.user?.id, input.id))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
const isOrgAdmin = ctx.user?.organization?.isOrgAdmin;
|
||||
|
||||
if (!isOrgAdmin) {
|
||||
if (!(await isTeamAdmin(ctx.user?.id, input.id))) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
}
|
||||
|
||||
if (input.slug) {
|
||||
const userConflict = await prisma.team.findMany({
|
||||
|
||||
Reference in new issue
Block a user