refactor: simplify user permission checks across application

This commit is contained in:
Mauricio Siu
2025-02-21 00:40:35 -06:00
parent 24c9d3f7ad
commit a317f0c4cc
19 changed files with 52 additions and 34 deletions

View File

@@ -1,7 +1,7 @@
import { db } from "@dokploy/server/db";
import type { users_temp } from "@dokploy/server/db/schema";
import { type users_temp, member } from "@dokploy/server/db/schema";
import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";
import { findUserById } from "./admin";
export type User = typeof users_temp.$inferSelect;
@@ -191,3 +191,26 @@ export const checkProjectAccess = async (
});
}
};
export const findMemberById = async (
userId: string,
organizationId: string,
) => {
const result = await db.query.member.findFirst({
where: and(
eq(member.userId, userId),
eq(member.organizationId, organizationId),
),
with: {
user: true,
},
});
if (!result) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Permission denied",
});
}
return result;
};