feat(license): enhance license key management and authorization checks

- Added authorization checks to ensure only users with the "owner" role can activate or deactivate license keys.
- Updated the menu item visibility logic to simplify role checks for admin and owner users.
- Commented out the cloud environment redirection logic in the license settings page for future consideration.
This commit is contained in:
Mauricio Siu
2026-01-31 18:03:03 -06:00
parent dc756e2bbb
commit 00ce8cad1b
3 changed files with 26 additions and 11 deletions
+3 -3
View File
@@ -21,6 +21,7 @@ import {
Key, Key,
KeyRound, KeyRound,
Loader2, Loader2,
LogIn,
type LucideIcon, type LucideIcon,
Package, Package,
PieChart, PieChart,
@@ -30,7 +31,6 @@ import {
Trash2, Trash2,
User, User,
Users, Users,
LogIn,
} from "lucide-react"; } from "lucide-react";
import Link from "next/link"; import Link from "next/link";
import { usePathname } from "next/navigation"; import { usePathname } from "next/navigation";
@@ -404,8 +404,8 @@ const MENU: Menu = {
url: "/dashboard/settings/license", url: "/dashboard/settings/license",
icon: Key, icon: Key,
// Only enabled for admins in non-cloud environments // Only enabled for admins in non-cloud environments
isEnabled: ({ auth, isCloud }) => isEnabled: ({ auth }) =>
!!((auth?.role === "owner" || auth?.role === "admin") && !isCloud), !!(auth?.role === "owner" || auth?.role === "admin"),
}, },
{ {
isSingle: true, isSingle: true,
@@ -36,14 +36,14 @@ export async function getServerSideProps(
) { ) {
const { req, res } = ctx; const { req, res } = ctx;
const locale = await getLocale(req.cookies); const locale = await getLocale(req.cookies);
if (IS_CLOUD) { // if (IS_CLOUD) {
return { // return {
redirect: { // redirect: {
permanent: true, // permanent: true,
destination: "/dashboard/projects", // destination: "/dashboard/projects",
}, // },
}; // };
} // }
const { user, session } = await validateRequest(ctx.req); const { user, session } = await validateRequest(ctx.req);
if (!user) { if (!user) {
return { return {
@@ -26,6 +26,13 @@ export const licenseKeyRouter = createTRPCRouter({
}); });
} }
if (ctx.user.role !== "owner") {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not authorized to activate a license key",
});
}
if (!currentUser.enableEnterpriseFeatures) { if (!currentUser.enableEnterpriseFeatures) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
@@ -117,6 +124,14 @@ export const licenseKeyRouter = createTRPCRouter({
message: "No license key found", message: "No license key found",
}); });
} }
if (ctx.user.role !== "owner") {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not authorized to deactivate a license key",
});
}
await deactivateLicenseKey(currentUser.licenseKey); await deactivateLicenseKey(currentUser.licenseKey);
await db await db
.update(user) .update(user)