120 lines
3.3 KiB
TypeScript
120 lines
3.3 KiB
TypeScript
import { clearCacheKey } from "~/utils/cache.server";
|
|
import { db } from "../../db.server";
|
|
import { invalidateUserPermissionCacheScopes } from "./permission-cache.server";
|
|
|
|
export async function invalidateRoleAssignmentScopes(roleIds: readonly string[]): Promise<void> {
|
|
if (roleIds.length === 0) return;
|
|
const assignments = await db.userRole.findMany({
|
|
where: { roleId: { in: [...new Set(roleIds)] } },
|
|
select: { userId: true, tenantId: true },
|
|
});
|
|
await invalidateUserPermissionCacheScopes(assignments);
|
|
}
|
|
|
|
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;
|
|
}
|
|
const created = await db.rolePermission.create({
|
|
data,
|
|
});
|
|
await invalidateRoleAssignmentScopes([data.roleId]);
|
|
return created;
|
|
}
|
|
|
|
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}`);
|
|
await invalidateRoleAssignmentScopes([roleId]);
|
|
}
|
|
|
|
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 },
|
|
})
|
|
: [];
|
|
const previousLinks = await db.rolePermission.findMany({
|
|
where: { permissionId },
|
|
select: { roleId: 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,
|
|
})),
|
|
});
|
|
}
|
|
});
|
|
await invalidateRoleAssignmentScopes([...previousLinks.map((link) => link.roleId), ...roles.map((role) => role.id)]);
|
|
}
|
|
|
|
export async function deleteRolePermission(roleId: string, permissionId: string) {
|
|
const result = await db.rolePermission.deleteMany({
|
|
where: { roleId, permissionId },
|
|
});
|
|
await invalidateRoleAssignmentScopes([roleId]);
|
|
return result;
|
|
}
|
|
|
|
export async function deleteRolePermissionById(id: string) {
|
|
const existing = await db.rolePermission.findUnique({ where: { id }, select: { roleId: true } });
|
|
const result = await db.rolePermission.delete({
|
|
where: { id },
|
|
});
|
|
if (existing) {
|
|
await invalidateRoleAssignmentScopes([existing.roleId]);
|
|
}
|
|
return result;
|
|
}
|