mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-17 17:35:17 +00:00
feat(auth): add enterprise feature flags to user context and request validation
- Updated user context to include `enableEnterpriseFeatures` and `isValidEnterpriseLicense` properties. - Modified request validation to set these properties based on user data, enhancing enterprise feature management. - Adjusted the enterprise procedure to check user flags directly from the context instead of querying the database.
This commit is contained in:
@@ -7,10 +7,8 @@
|
|||||||
* need to use are documented accordingly near the end.
|
* need to use are documented accordingly near the end.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { user as userSchema } from "@dokploy/server/db/schema";
|
|
||||||
import { validateRequest } from "@dokploy/server/lib/auth";
|
import { validateRequest } from "@dokploy/server/lib/auth";
|
||||||
import type { OpenApiMeta } from "@dokploy/trpc-openapi";
|
import type { OpenApiMeta } from "@dokploy/trpc-openapi";
|
||||||
import { eq } from "drizzle-orm";
|
|
||||||
import { initTRPC, TRPCError } from "@trpc/server";
|
import { initTRPC, TRPCError } from "@trpc/server";
|
||||||
import type { CreateNextContextOptions } from "@trpc/server/adapters/next";
|
import type { CreateNextContextOptions } from "@trpc/server/adapters/next";
|
||||||
import {
|
import {
|
||||||
@@ -33,7 +31,14 @@ import { db } from "@/server/db";
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
interface CreateContextOptions {
|
interface CreateContextOptions {
|
||||||
user: (User & { role: "member" | "admin" | "owner"; ownerId: string }) | null;
|
user:
|
||||||
|
| (User & {
|
||||||
|
role: "member" | "admin" | "owner";
|
||||||
|
ownerId: string;
|
||||||
|
enableEnterpriseFeatures: boolean;
|
||||||
|
isValidEnterpriseLicense: boolean;
|
||||||
|
})
|
||||||
|
| null;
|
||||||
session:
|
session:
|
||||||
| (Session & { activeOrganizationId: string; impersonatedBy?: string })
|
| (Session & { activeOrganizationId: string; impersonatedBy?: string })
|
||||||
| null;
|
| null;
|
||||||
@@ -234,17 +239,9 @@ export const enterpriseProcedure = t.procedure.use(async ({ ctx, next }) => {
|
|||||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentUser = await ctx.db.query.user.findFirst({
|
|
||||||
where: eq(userSchema.id, ctx.user.id),
|
|
||||||
columns: {
|
|
||||||
enableEnterpriseFeatures: true,
|
|
||||||
isValidEnterpriseLicense: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!currentUser?.enableEnterpriseFeatures ||
|
!ctx.user?.enableEnterpriseFeatures ||
|
||||||
!currentUser.isValidEnterpriseLicense
|
!ctx.user.isValidEnterpriseLicense
|
||||||
) {
|
) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "FORBIDDEN",
|
code: "FORBIDDEN",
|
||||||
|
|||||||
@@ -279,6 +279,16 @@ const { handler, api } = betterAuth({
|
|||||||
input: true,
|
input: true,
|
||||||
defaultValue: "",
|
defaultValue: "",
|
||||||
},
|
},
|
||||||
|
enableEnterpriseFeatures: {
|
||||||
|
type: "boolean",
|
||||||
|
required: false,
|
||||||
|
input: false,
|
||||||
|
},
|
||||||
|
isValidEnterpriseLicense: {
|
||||||
|
type: "boolean",
|
||||||
|
required: false,
|
||||||
|
input: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
@@ -399,6 +409,8 @@ export const validateRequest = async (request: IncomingMessage) => {
|
|||||||
twoFactorEnabled: userFromDb.twoFactorEnabled,
|
twoFactorEnabled: userFromDb.twoFactorEnabled,
|
||||||
role: member?.role || "member",
|
role: member?.role || "member",
|
||||||
ownerId: member?.organization.ownerId || apiKeyRecord.user.id,
|
ownerId: member?.organization.ownerId || apiKeyRecord.user.id,
|
||||||
|
enableEnterpriseFeatures: userFromDb.enableEnterpriseFeatures,
|
||||||
|
isValidEnterpriseLicense: userFromDb.isValidEnterpriseLicense,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -437,10 +449,15 @@ export const validateRequest = async (request: IncomingMessage) => {
|
|||||||
),
|
),
|
||||||
with: {
|
with: {
|
||||||
organization: true,
|
organization: true,
|
||||||
|
user: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
session.user.role = member?.role || "member";
|
session.user.role = member?.role || "member";
|
||||||
|
session.user.enableEnterpriseFeatures =
|
||||||
|
member?.user.enableEnterpriseFeatures || false;
|
||||||
|
session.user.isValidEnterpriseLicense =
|
||||||
|
member?.user.isValidEnterpriseLicense || false;
|
||||||
if (member) {
|
if (member) {
|
||||||
session.user.ownerId = member.organization.ownerId;
|
session.user.ownerId = member.organization.ownerId;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user