85 lines
2.5 KiB
TypeScript
85 lines
2.5 KiB
TypeScript
|
|
import { PrismaClient } from '@prisma/client';
|
||
|
|
|
||
|
|
const prisma = new PrismaClient();
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
console.log('🔧 修复工作台权限配置...\n');
|
||
|
|
|
||
|
|
// 1. 获取所有租户
|
||
|
|
const tenants = await prisma.tenant.findMany({
|
||
|
|
select: { id: true, name: true }
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const tenant of tenants) {
|
||
|
|
console.log(`📝 处理租户: ${tenant.name} (ID: ${tenant.id})`);
|
||
|
|
|
||
|
|
// 2. 检查/创建 workbench:read 权限
|
||
|
|
let perm = await prisma.permission.findFirst({
|
||
|
|
where: { tenantId: tenant.id, code: 'workbench:read' }
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!perm) {
|
||
|
|
perm = await prisma.permission.create({
|
||
|
|
data: {
|
||
|
|
tenantId: tenant.id,
|
||
|
|
code: 'workbench:read',
|
||
|
|
name: '查看工作台',
|
||
|
|
resource: 'workbench',
|
||
|
|
action: 'read',
|
||
|
|
description: '访问工作台页面',
|
||
|
|
}
|
||
|
|
});
|
||
|
|
console.log(' ✅ 创建权限: workbench:read');
|
||
|
|
} else {
|
||
|
|
console.log(' ⏭️ 权限已存在: workbench:read');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 为该租户的所有角色分配此权限
|
||
|
|
const roles = await prisma.role.findMany({
|
||
|
|
where: { tenantId: tenant.id },
|
||
|
|
select: { id: true, name: true, code: true }
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const role of roles) {
|
||
|
|
const existing = await prisma.rolePermission.findFirst({
|
||
|
|
where: { roleId: role.id, permissionId: perm.id }
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!existing) {
|
||
|
|
await prisma.rolePermission.create({
|
||
|
|
data: { roleId: role.id, permissionId: perm.id }
|
||
|
|
});
|
||
|
|
console.log(` ✅ 分配给角色: ${role.name}`);
|
||
|
|
} else {
|
||
|
|
console.log(` ⏭️ 角色 ${role.name} 已有此权限`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. 更新工作台菜单的权限配置
|
||
|
|
console.log('\n🔧 更新工作台菜单权限配置...');
|
||
|
|
const workbenchMenu = await prisma.menu.findFirst({
|
||
|
|
where: { name: '工作台' }
|
||
|
|
});
|
||
|
|
|
||
|
|
if (workbenchMenu) {
|
||
|
|
if (!workbenchMenu.permission || workbenchMenu.permission.trim() === '') {
|
||
|
|
await prisma.menu.update({
|
||
|
|
where: { id: workbenchMenu.id },
|
||
|
|
data: { permission: 'workbench:read' }
|
||
|
|
});
|
||
|
|
console.log('✅ 工作台菜单已设置 permission: workbench:read');
|
||
|
|
} else {
|
||
|
|
console.log(`⏭️ 工作台菜单已有权限: ${workbenchMenu.permission}`);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.log('⚠️ 未找到工作台菜单');
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('\n✅ 完成!请重新登录以查看更新后的菜单。');
|
||
|
|
}
|
||
|
|
|
||
|
|
main()
|
||
|
|
.catch(console.error)
|
||
|
|
.finally(() => prisma.$disconnect());
|