114 lines
3.0 KiB
TypeScript
114 lines
3.0 KiB
TypeScript
import { clearCacheKey } from "~/utils/cache.server";
|
|
import { createScopedLogger } from "~/utils/logger.server";
|
|
import { db } from "../../db.server";
|
|
import { adminGetAllTenants } from "../tenants.db.server";
|
|
import { adminGetAllUsersNames } from "../users.db.server";
|
|
|
|
const rolePermissionsLogger = createScopedLogger("db.rolePermissions");
|
|
|
|
export async function getAllRolePermissions() {
|
|
return await db.rolePermission.findMany({
|
|
include: {
|
|
permission: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createRolePermission(data: { roleId: string; permissionId: string }) {
|
|
const existing = await db.rolePermission.findFirst({
|
|
where: {
|
|
roleId: data.roleId,
|
|
permissionId: data.permissionId,
|
|
},
|
|
});
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
return await db.rolePermission.create({
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function setRolePermissions(roleId: string, permissionNames: string[]) {
|
|
const role = await db.role.findUnique({ where: { id: roleId } });
|
|
if (!role) {
|
|
return;
|
|
}
|
|
|
|
const uniquePermissionNames = [...new Set(permissionNames)];
|
|
const permissions = uniquePermissionNames.length
|
|
? await db.permission.findMany({
|
|
where: { name: { in: uniquePermissionNames } },
|
|
select: { id: true },
|
|
})
|
|
: [];
|
|
|
|
await db.$transaction(async (tx) => {
|
|
await tx.rolePermission.deleteMany({
|
|
where: { roleId },
|
|
});
|
|
|
|
if (permissions.length > 0) {
|
|
await tx.rolePermission.createMany({
|
|
data: permissions.map((permission) => ({
|
|
roleId,
|
|
permissionId: permission.id,
|
|
})),
|
|
});
|
|
}
|
|
});
|
|
|
|
clearCacheKey(`roles:${role.type}`);
|
|
if (role.type === "app") {
|
|
const allTenants = await adminGetAllTenants();
|
|
rolePermissionsLogger.info({ tenantCount: allTenants.length }, "Clearing user roles cache");
|
|
for (const tenant of allTenants) {
|
|
for (const user of tenant.users) {
|
|
clearCacheKey(`userRoles:${user.userId}:${tenant.id}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
const allUsers = await adminGetAllUsersNames();
|
|
for (const user of allUsers) {
|
|
clearCacheKey(`userRole:${user.id}:${role.name}`);
|
|
}
|
|
}
|
|
|
|
export async function setPermissionRoles(permissionId: string, roleNames: string[]) {
|
|
const uniqueRoleNames = [...new Set(roleNames)];
|
|
const roles = uniqueRoleNames.length
|
|
? await db.role.findMany({
|
|
where: { name: { in: uniqueRoleNames } },
|
|
select: { id: true },
|
|
})
|
|
: [];
|
|
|
|
await db.$transaction(async (tx) => {
|
|
await tx.rolePermission.deleteMany({
|
|
where: { permissionId },
|
|
});
|
|
|
|
if (roles.length > 0) {
|
|
await tx.rolePermission.createMany({
|
|
data: roles.map((role) => ({
|
|
roleId: role.id,
|
|
permissionId,
|
|
})),
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function deleteRolePermission(roleId: string, permissionId: string) {
|
|
return await db.rolePermission.deleteMany({
|
|
where: { roleId, permissionId },
|
|
});
|
|
}
|
|
|
|
export async function deleteRolePermissionById(id: string) {
|
|
return await db.rolePermission.delete({
|
|
where: { id },
|
|
});
|
|
}
|