98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
|
|
import { PrismaClient } from '@prisma/client';
|
||
|
|
|
||
|
|
const prisma = new PrismaClient();
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
console.log('🔍 检查现有 homework 权限...');
|
||
|
|
|
||
|
|
// 获取所有租户
|
||
|
|
const tenants = await prisma.tenant.findMany({
|
||
|
|
select: { id: true, name: true }
|
||
|
|
});
|
||
|
|
console.log(`找到 ${tenants.length} 个租户`);
|
||
|
|
|
||
|
|
// 定义作业管理权限
|
||
|
|
const homeworkPermissions = [
|
||
|
|
{ code: 'homework:read', name: '查看作业', resource: 'homework', action: 'read' },
|
||
|
|
{ code: 'homework:create', name: '创建作业', resource: 'homework', action: 'create' },
|
||
|
|
{ code: 'homework:update', name: '编辑作业', resource: 'homework', action: 'update' },
|
||
|
|
{ code: 'homework:delete', name: '删除作业', resource: 'homework', action: 'delete' },
|
||
|
|
];
|
||
|
|
|
||
|
|
for (const tenant of tenants) {
|
||
|
|
console.log(`\n📝 为租户 "${tenant.name}" (ID: ${tenant.id}) 添加权限...`);
|
||
|
|
|
||
|
|
for (const perm of homeworkPermissions) {
|
||
|
|
// 检查权限是否已存在
|
||
|
|
const existing = await prisma.permission.findFirst({
|
||
|
|
where: {
|
||
|
|
tenantId: tenant.id,
|
||
|
|
code: perm.code,
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
if (existing) {
|
||
|
|
console.log(` ⏭️ ${perm.name} (${perm.code}) 已存在`);
|
||
|
|
} else {
|
||
|
|
await prisma.permission.create({
|
||
|
|
data: {
|
||
|
|
tenantId: tenant.id,
|
||
|
|
code: perm.code,
|
||
|
|
name: perm.name,
|
||
|
|
resource: perm.resource,
|
||
|
|
action: perm.action,
|
||
|
|
description: `作业管理 - ${perm.name}`,
|
||
|
|
}
|
||
|
|
});
|
||
|
|
console.log(` ✅ ${perm.name} (${perm.code}) 已创建`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取管理员角色并分配权限
|
||
|
|
const adminRole = await prisma.role.findFirst({
|
||
|
|
where: {
|
||
|
|
tenantId: tenant.id,
|
||
|
|
code: 'admin',
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
if (adminRole) {
|
||
|
|
console.log(`\n🔐 为管理员角色分配权限...`);
|
||
|
|
|
||
|
|
const allHomeworkPerms = await prisma.permission.findMany({
|
||
|
|
where: {
|
||
|
|
tenantId: tenant.id,
|
||
|
|
resource: 'homework',
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const perm of allHomeworkPerms) {
|
||
|
|
const existing = await prisma.rolePermission.findFirst({
|
||
|
|
where: {
|
||
|
|
roleId: adminRole.id,
|
||
|
|
permissionId: perm.id,
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!existing) {
|
||
|
|
await prisma.rolePermission.create({
|
||
|
|
data: {
|
||
|
|
roleId: adminRole.id,
|
||
|
|
permissionId: perm.id,
|
||
|
|
}
|
||
|
|
});
|
||
|
|
console.log(` ✅ 已分配 ${perm.code} 给管理员角色`);
|
||
|
|
} else {
|
||
|
|
console.log(` ⏭️ ${perm.code} 已分配`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('\n✅ 权限配置完成!');
|
||
|
|
}
|
||
|
|
|
||
|
|
main()
|
||
|
|
.catch(console.error)
|
||
|
|
.finally(() => prisma.$disconnect());
|