refactor: update project and API components to utilize environment context for organization authorization checks and enhance service retrieval methods

This commit is contained in:
Mauricio Siu
2025-09-01 20:36:03 -06:00
parent e0b6a8627a
commit 399bcb0302
4 changed files with 82 additions and 100 deletions
@@ -187,23 +187,28 @@ export const ShowProjects = () => {
)} )}
<div className="w-full grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 3xl:grid-cols-5 flex-wrap gap-5"> <div className="w-full grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 3xl:grid-cols-5 flex-wrap gap-5">
{filteredProjects?.map((project) => { {filteredProjects?.map((project) => {
// const emptyServices = const emptyServices =
// project?.environments.length === 0 && project?.environments.map((env) => env.applications.length === 0 &&
// project?.mongo.length === 0 && env.mariadb.length === 0 &&
// project?.environments.mysql.length === 0 && env.mongo.length === 0 &&
// project?.environments.postgres.length === 0 && env.mysql.length === 0 &&
// project?.environments.redis.length === 0 && env.postgres.length === 0 &&
// project?.applications.length === 0 && env.redis.length === 0 &&
// project?.compose.length === 0; env.applications.length === 0 &&
env.compose.length === 0).every(Boolean);
// const totalServices = const totalServices =
// project?.mariadb.length + project?.environments.map((env) => env.mariadb.length +
// project?.mongo.length + env.mongo.length +
// project?.mysql.length + env.mysql.length +
// project?.postgres.length + env.postgres.length +
// project?.redis.length + env.redis.length +
// project?.applications.length + env.applications.length +
// project?.compose.length; env.compose.length ).reduce((acc, curr) => acc + curr, 0);
const haveServicesWithDomains =
project?.environments.map((env) => env.applications.length > 0 ||
env.compose.length > 0).some(Boolean);
return ( return (
<div <div
@@ -214,8 +219,7 @@ export const ShowProjects = () => {
href={`/dashboard/project/${project.projectId}`} href={`/dashboard/project/${project.projectId}`}
> >
<Card className="group relative w-full h-full bg-transparent transition-colors hover:bg-border"> <Card className="group relative w-full h-full bg-transparent transition-colors hover:bg-border">
{/* {project.applications.length > 0 || {haveServicesWithDomains ? (
project.compose.length > 0 ? (
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
<Button <Button
@@ -230,44 +234,47 @@ export const ShowProjects = () => {
className="w-[200px] space-y-2 overflow-y-auto max-h-[400px]" className="w-[200px] space-y-2 overflow-y-auto max-h-[400px]"
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}
> >
{project.applications.length > 0 && ( {project.environments.some((env) => env.applications.length > 0) && (
<DropdownMenuGroup> <DropdownMenuGroup>
<DropdownMenuLabel> <DropdownMenuLabel>
Applications Applications
</DropdownMenuLabel> </DropdownMenuLabel>
{project.applications.map((app) => ( {project.environments.map((env) =>
<div key={app.applicationId}> env.applications.map((app) => (
<DropdownMenuSeparator /> <div key={app.applicationId}>
<DropdownMenuGroup>
<DropdownMenuLabel className="font-normal capitalize text-xs flex items-center justify-between">
{app.name}
<StatusTooltip
status={app.applicationStatus}
/>
</DropdownMenuLabel>
<DropdownMenuSeparator /> <DropdownMenuSeparator />
{app.domains.map((domain) => ( <DropdownMenuGroup>
<DropdownMenuItem <DropdownMenuLabel className="font-normal capitalize text-xs flex items-center justify-between">
key={domain.domainId} {app.name}
asChild <StatusTooltip
> status={app.applicationStatus}
<Link />
className="space-x-4 text-xs cursor-pointer justify-between" </DropdownMenuLabel>
target="_blank" <DropdownMenuSeparator />
href={`${domain.https ? "https" : "http"}://${domain.host}${domain.path}`} {app.domains.map((domain) => (
<DropdownMenuItem
key={domain.domainId}
asChild
> >
<span className="truncate"> <Link
{domain.host} className="space-x-4 text-xs cursor-pointer justify-between"
</span> target="_blank"
<ExternalLinkIcon className="size-4 shrink-0" /> href={`${domain.https ? "https" : "http"}://${domain.host}${domain.path}`}
</Link> >
</DropdownMenuItem> <span className="truncate">
))} {domain.host}
</DropdownMenuGroup> </span>
</div> <ExternalLinkIcon className="size-4 shrink-0" />
))} </Link>
</DropdownMenuItem>
))}
</DropdownMenuGroup>
</div>
))
)}
</DropdownMenuGroup> </DropdownMenuGroup>
)} )}
{/*
{project.compose.length > 0 && ( {project.compose.length > 0 && (
<DropdownMenuGroup> <DropdownMenuGroup>
<DropdownMenuLabel> <DropdownMenuLabel>
@@ -305,10 +312,10 @@ export const ShowProjects = () => {
</div> </div>
))} ))}
</DropdownMenuGroup> </DropdownMenuGroup>
)} )} */}
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
) : null} */} ) : null}
<CardHeader> <CardHeader>
<CardTitle className="flex items-center justify-between gap-2"> <CardTitle className="flex items-center justify-between gap-2">
<span className="flex flex-col gap-1.5"> <span className="flex flex-col gap-1.5">
@@ -379,7 +386,7 @@ export const ShowProjects = () => {
Are you sure to delete this Are you sure to delete this
project? project?
</AlertDialogTitle> </AlertDialogTitle>
{/* {!emptyServices ? ( {!emptyServices ? (
<div className="flex flex-row gap-4 rounded-lg bg-yellow-50 p-2 dark:bg-yellow-950"> <div className="flex flex-row gap-4 rounded-lg bg-yellow-50 p-2 dark:bg-yellow-950">
<AlertTriangle className="text-yellow-600 dark:text-yellow-400" /> <AlertTriangle className="text-yellow-600 dark:text-yellow-400" />
<span className="text-sm text-yellow-600 dark:text-yellow-400"> <span className="text-sm text-yellow-600 dark:text-yellow-400">
@@ -393,14 +400,14 @@ export const ShowProjects = () => {
This action cannot be This action cannot be
undone undone
</AlertDialogDescription> </AlertDialogDescription>
)} */} )}
</AlertDialogHeader> </AlertDialogHeader>
<AlertDialogFooter> <AlertDialogFooter>
<AlertDialogCancel> <AlertDialogCancel>
Cancel Cancel
</AlertDialogCancel> </AlertDialogCancel>
<AlertDialogAction <AlertDialogAction
// disabled={!emptyServices} disabled={!emptyServices}
onClick={async () => { onClick={async () => {
await mutateAsync({ await mutateAsync({
projectId: projectId:
@@ -438,12 +445,12 @@ export const ShowProjects = () => {
<DateTooltip date={project.createdAt}> <DateTooltip date={project.createdAt}>
Created Created
</DateTooltip> </DateTooltip>
{/* <span> <span>
{totalServices}{" "} {totalServices}{" "}
{totalServices === 1 {totalServices === 1
? "service" ? "service"
: "services"} : "services"}
</span> */} </span>
</div> </div>
</CardFooter> </CardFooter>
</Card> </Card>
+11 -11
View File
@@ -34,7 +34,7 @@ export const domainRouter = createTRPCRouter({
if (input.domainType === "compose" && input.composeId) { if (input.domainType === "compose" && input.composeId) {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if ( if (
compose.project.organizationId !== ctx.session.activeOrganizationId compose.environment.project.organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -44,7 +44,7 @@ export const domainRouter = createTRPCRouter({
} else if (input.domainType === "application" && input.applicationId) { } else if (input.domainType === "application" && input.applicationId) {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -70,7 +70,7 @@ export const domainRouter = createTRPCRouter({
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -83,7 +83,7 @@ export const domainRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (compose.environment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this compose", message: "You are not authorized to access this compose",
@@ -122,7 +122,7 @@ export const domainRouter = createTRPCRouter({
if (currentDomain.applicationId) { if (currentDomain.applicationId) {
const newApp = await findApplicationById(currentDomain.applicationId); const newApp = await findApplicationById(currentDomain.applicationId);
if ( if (
newApp.project.organizationId !== ctx.session.activeOrganizationId newApp.environment.project.organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -132,7 +132,7 @@ export const domainRouter = createTRPCRouter({
} else if (currentDomain.composeId) { } else if (currentDomain.composeId) {
const newCompose = await findComposeById(currentDomain.composeId); const newCompose = await findComposeById(currentDomain.composeId);
if ( if (
newCompose.project.organizationId !== ctx.session.activeOrganizationId newCompose.environment.project.organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -144,7 +144,7 @@ export const domainRouter = createTRPCRouter({
currentDomain.previewDeploymentId, currentDomain.previewDeploymentId,
); );
if ( if (
newPreviewDeployment.application.project.organizationId !== newPreviewDeployment.application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -175,7 +175,7 @@ export const domainRouter = createTRPCRouter({
if (domain.applicationId) { if (domain.applicationId) {
const application = await findApplicationById(domain.applicationId); const application = await findApplicationById(domain.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -184,7 +184,7 @@ export const domainRouter = createTRPCRouter({
} }
} else if (domain.composeId) { } else if (domain.composeId) {
const compose = await findComposeById(domain.composeId); const compose = await findComposeById(domain.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (compose.environment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this compose", message: "You are not authorized to access this compose",
@@ -200,7 +200,7 @@ export const domainRouter = createTRPCRouter({
if (domain.applicationId) { if (domain.applicationId) {
const application = await findApplicationById(domain.applicationId); const application = await findApplicationById(domain.applicationId);
if ( if (
application.project.organizationId !== application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -211,7 +211,7 @@ export const domainRouter = createTRPCRouter({
} else if (domain.composeId) { } else if (domain.composeId) {
const compose = await findComposeById(domain.composeId); const compose = await findComposeById(domain.composeId);
if ( if (
compose.project.organizationId !== ctx.session.activeOrganizationId compose.environment.project.organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -15,7 +15,7 @@ export const previewDeploymentRouter = createTRPCRouter({
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -31,7 +31,7 @@ export const previewDeploymentRouter = createTRPCRouter({
input.previewDeploymentId, input.previewDeploymentId,
); );
if ( if (
previewDeployment.application.project.organizationId !== previewDeployment.application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -49,7 +49,7 @@ export const previewDeploymentRouter = createTRPCRouter({
input.previewDeploymentId, input.previewDeploymentId,
); );
if ( if (
previewDeployment.application.project.organizationId !== previewDeployment.application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -31,7 +31,11 @@ export const findPreviewDeploymentById = async (
application: { application: {
with: { with: {
server: true, server: true,
project: true, environment: {
with: {
project: true,
},
},
}, },
}, },
}, },
@@ -45,36 +49,7 @@ export const findPreviewDeploymentById = async (
return application; return application;
}; };
export const findApplicationByPreview = async (applicationId: string) => {
const application = await db.query.applications.findFirst({
with: {
previewDeployments: {
where: eq(previewDeployments.applicationId, applicationId),
},
project: true,
domains: true,
deployments: true,
mounts: true,
redirects: true,
security: true,
ports: true,
registry: true,
gitlab: true,
github: true,
bitbucket: true,
gitea: true,
server: true,
},
});
if (!application) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Application not found",
});
}
return application;
};
export const removePreviewDeployment = async (previewDeploymentId: string) => { export const removePreviewDeployment = async (previewDeploymentId: string) => {
try { try {
@@ -163,7 +138,7 @@ export const createPreviewDeployment = async (
const appName = `preview-${application.appName}-${generatePassword(6)}`; const appName = `preview-${application.appName}-${generatePassword(6)}`;
const org = await db.query.organization.findFirst({ const org = await db.query.organization.findFirst({
where: eq(organization.id, application.project.organizationId), where: eq(organization.id, application.environment.project.organizationId),
}); });
const generateDomain = await generateWildcardDomain( const generateDomain = await generateWildcardDomain(
application.previewWildcard || "*.traefik.me", application.previewWildcard || "*.traefik.me",