mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-21 13:55:33 +02:00
- Introduced new test files for permission checks, including `check-permission.test.ts`, `enterprise-only-resources.test.ts`, `resolve-permissions.test.ts`, and `service-access.test.ts`. - Implemented permission checks in various components to ensure actions are gated by user permissions, including `ShowTraefikConfig`, `UpdateTraefikConfig`, `ShowVolumes`, `ShowDomains`, and others. - Enhanced the logic for displaying UI elements based on user permissions, ensuring that only authorized users can access or modify resources.
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { getPublicIpWithFallback } from "@dokploy/server/wss/utils";
|
|
import { and, eq, isNotNull } from "drizzle-orm";
|
|
import { scheduleJob } from "node-schedule";
|
|
import { db } from "../../db/index";
|
|
import { user as userSchema } from "../../db/schema/user";
|
|
|
|
export const LICENSE_KEY_URL =
|
|
// process.env.NODE_ENV === "development"
|
|
// ? "http://localhost:4002"
|
|
"https://licenses-api.dokploy.com";
|
|
|
|
export const initEnterpriseBackupCronJobs = async () => {
|
|
scheduleJob("enterprise-check", "0 0 */3 * *", async () => {
|
|
const users = await db.query.user.findMany({
|
|
where: and(
|
|
isNotNull(userSchema.licenseKey),
|
|
isNotNull(userSchema.enableEnterpriseFeatures),
|
|
eq(userSchema.isValidEnterpriseLicense, true),
|
|
),
|
|
});
|
|
for (const user of users) {
|
|
if (user.isValidEnterpriseLicense) {
|
|
console.log(
|
|
"Validating license key....",
|
|
user.firstName,
|
|
user.lastName,
|
|
);
|
|
try {
|
|
const isValid = await validateLicenseKey(user.licenseKey || "");
|
|
if (!isValid) {
|
|
throw new Error("License key is invalid");
|
|
}
|
|
} catch (error) {
|
|
await db
|
|
.update(userSchema)
|
|
.set({ isValidEnterpriseLicense: false })
|
|
.where(eq(userSchema.id, user.id));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
export const validateLicenseKey = async (licenseKey: string) => {
|
|
try {
|
|
const ip = await getPublicIpWithFallback();
|
|
const result = await fetch(`${LICENSE_KEY_URL}/licenses/validate`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ licenseKey, ip }),
|
|
});
|
|
|
|
if (!result.ok) {
|
|
const errorData = await result.json().catch(() => ({}));
|
|
throw new Error(errorData.message || "Failed to validate license key");
|
|
}
|
|
|
|
const data = await result.json();
|
|
return data.valid;
|
|
} catch (error) {
|
|
console.error(
|
|
error instanceof Error ? error.message : "Failed to validate license key",
|
|
);
|
|
throw error;
|
|
}
|
|
};
|