mirror of
https://github.com/andwati/dokploy.git
synced 2026-10-21 04:24:30 +00:00
feat(organization): allow setting a default role for new members
This commit is contained in:
@@ -66,6 +66,7 @@ export const organization = pgTable("organization", {
|
||||
logo: text("logo"),
|
||||
createdAt: timestamp("created_at").notNull(),
|
||||
metadata: text("metadata"),
|
||||
defaultRole: text("default_role"),
|
||||
ownerId: text("owner_id")
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: "cascade" }),
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
getUserByToken,
|
||||
} from "../services/admin";
|
||||
import { createAuditLog } from "../services/proprietary/audit-log";
|
||||
import { resolveOrganizationDefaultRole } from "../services/proprietary/license-key";
|
||||
import {
|
||||
getWebServerSettings,
|
||||
updateWebServerSettings,
|
||||
@@ -289,10 +290,13 @@ const createBetterAuth = () =>
|
||||
message: "Provider not found",
|
||||
});
|
||||
}
|
||||
const defaultRole = provider.organizationId
|
||||
? await resolveOrganizationDefaultRole(provider.organizationId)
|
||||
: "member";
|
||||
await db.insert(schema.member).values({
|
||||
userId: user.id,
|
||||
organizationId: provider?.organizationId || "",
|
||||
role: "member",
|
||||
role: defaultRole,
|
||||
createdAt: new Date(),
|
||||
isDefault: true,
|
||||
});
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { db } from "@dokploy/server/db";
|
||||
import { user } from "@dokploy/server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import {
|
||||
organization,
|
||||
organizationRole,
|
||||
user,
|
||||
} from "@dokploy/server/db/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { getOrganizationOwnerId } from "./sso";
|
||||
|
||||
export const hasValidLicense = async (organizationId: string) => {
|
||||
@@ -22,3 +26,35 @@ export const hasValidLicense = async (organizationId: string) => {
|
||||
currentUser?.isValidEnterpriseLicense
|
||||
);
|
||||
};
|
||||
|
||||
export const resolveOrganizationDefaultRole = async (
|
||||
organizationId: string,
|
||||
) => {
|
||||
const org = await db.query.organization.findFirst({
|
||||
where: eq(organization.id, organizationId),
|
||||
columns: { defaultRole: true },
|
||||
});
|
||||
const defaultRole = org?.defaultRole;
|
||||
|
||||
if (!defaultRole || defaultRole === "owner") {
|
||||
return "member";
|
||||
}
|
||||
|
||||
if (defaultRole === "admin" || defaultRole === "member") {
|
||||
return defaultRole;
|
||||
}
|
||||
|
||||
const customRole = await db.query.organizationRole.findFirst({
|
||||
where: and(
|
||||
eq(organizationRole.organizationId, organizationId),
|
||||
eq(organizationRole.role, defaultRole),
|
||||
),
|
||||
columns: { id: true },
|
||||
});
|
||||
|
||||
if (!customRole || !(await hasValidLicense(organizationId))) {
|
||||
return "member";
|
||||
}
|
||||
|
||||
return defaultRole;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user