Skip to content

Commit 4ac8cbb

Browse files
committed
fix: redirect with error toast instead of raw json on denied dashboard management actions
1 parent 5cea2bb commit 4ac8cbb

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

  • apps/webapp/app/routes
    • _app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.regions
    • _app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.settings.general
    • _app.orgs.$organizationSlug.settings._index

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.regions/route.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
MapPinIcon,
88
} from "@heroicons/react/20/solid";
99
import { Form } from "@remix-run/react";
10-
import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
10+
import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
1111
import { tryCatch } from "@trigger.dev/core";
1212
import { useState } from "react";
1313
import { typedjson, useTypedLoaderData } from "remix-typedjson";
@@ -103,18 +103,22 @@ export const action = dashboardAction(
103103
async ({ user, ability, request, params }) => {
104104
const { organizationSlug, projectParam, envParam } = params;
105105

106-
if (!ability.can("manage", { type: "project" })) {
107-
return json({ ok: false, error: "Unauthorized" } as const, { status: 403 });
108-
}
109-
110-
const project = await findProjectBySlug(organizationSlug, projectParam, user.id);
111-
112106
const redirectPath = regionsPath(
113107
{ slug: organizationSlug },
114108
{ slug: projectParam },
115109
{ slug: envParam }
116110
);
117111

112+
if (!ability.can("manage", { type: "project" })) {
113+
throw redirectWithErrorMessage(
114+
redirectPath,
115+
request,
116+
"You don't have permission to change the default region"
117+
);
118+
}
119+
120+
const project = await findProjectBySlug(organizationSlug, projectParam, user.id);
121+
118122
if (!project) {
119123
throw redirectWithErrorMessage(redirectPath, request, "Project not found");
120124
}

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.settings.general/route.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ export const action = dashboardAction(
106106
switch (submission.value.action) {
107107
case "rename": {
108108
if (!ability.can("manage", { type: "project" })) {
109-
return json({ ok: false, error: "Unauthorized" } as const, { status: 403 });
109+
throw redirectWithErrorMessage(
110+
v3ProjectPath({ slug: organizationSlug }, { slug: projectParam }),
111+
request,
112+
"You don't have permission to rename this project"
113+
);
110114
}
111115
const resultOrFail = await projectSettingsService.renameProject(
112116
projectId,
@@ -135,7 +139,11 @@ export const action = dashboardAction(
135139
}
136140
case "delete": {
137141
if (!ability.can("manage", { type: "project" })) {
138-
return json({ ok: false, error: "Unauthorized" } as const, { status: 403 });
142+
throw redirectWithErrorMessage(
143+
v3ProjectPath({ slug: organizationSlug }, { slug: projectParam }),
144+
request,
145+
"You don't have permission to delete this project"
146+
);
139147
}
140148
const resultOrFail = await projectSettingsService.deleteProject(projectId, userId);
141149

apps/webapp/app/routes/_app.orgs.$organizationSlug.settings._index/route.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ export const action = dashboardAction(
177177
switch (submission.value.action) {
178178
case "rename": {
179179
if (!ability.can("manage", { type: "organization" })) {
180-
return json({ ok: false, error: "Unauthorized" } as const, { status: 403 });
180+
throw redirectWithErrorMessage(
181+
organizationSettingsPath({ slug: organizationSlug }),
182+
request,
183+
"You don't have permission to rename this organization"
184+
);
181185
}
182186
await prisma.organization.update({
183187
where: {
@@ -201,7 +205,11 @@ export const action = dashboardAction(
201205
}
202206
case "delete": {
203207
if (!ability.can("manage", { type: "organization" })) {
204-
return json({ ok: false, error: "Unauthorized" } as const, { status: 403 });
208+
throw redirectWithErrorMessage(
209+
organizationSettingsPath({ slug: organizationSlug }),
210+
request,
211+
"You don't have permission to delete this organization"
212+
);
205213
}
206214
const deleteOrganizationService = new DeleteOrganizationService();
207215
try {

0 commit comments

Comments
 (0)