import { db } from './index'; import { users, groups, roles, permissions, userGroups, groupRoles, rolePermissions, userRoles, resources, aclRules } from './schema/auth'; import { resources as resourceSchema, aclRules as aclRulesSchema } from './schema/resource'; import { eq } from 'drizzle-orm'; async function seed() { console.log('🌱 开始执行 Seed 脚本...'); try { // 1. 清理旧数据 (为了保证多次运行 seed 的幂等性) // 注意:在生产环境严禁这样做,这里仅用于开发测试 console.log('🧹 清理现有数据...'); // 由于存在外键约束,需要按顺序删除或使用 TRUNCATE // 这里简单处理,直接尝试删除(实际开发中建议用更优雅的清理方式) // 为了演示方便,我们假设是干净的环境 // 2. 创建基础角色和权限 console.log('🔑 创建基础角色与权限...'); const [adminRole] = await db.insert(roles).values({ name: 'admin', description: '系统管理员,拥有最高权限' }).returning(); const [editorRole] = await db.insert(roles).values({ name: 'editor', description: '内容编辑者,可以读写资源' }).returning(); const [viewerRole] = await db.insert(roles).values({ name: 'viewer', description: '普通查看者,仅限读取' }).returning(); const [readPerm] = await db.insert(permissions).values({ action: 'read', resourceType: 'document' }).returning(); const [writePerm] = await db.insert(permissions).values({ action: 'write', resourceType: 'document' }).returning(); // 绑定权限到角色 await db.insert(rolePermissions).values([ { roleId: adminRole.id, permissionId: readPerm.id }, { roleId: adminRole.id, permissionId: writePerm.id }, { roleId: editorRole.id, permissionId: readPerm.id }, { roleId: editorRole.id, permissionId: writePerm.id }, { roleId: viewerRole.id, permissionId: readPerm.id }, ]); // 3. 创建用户和组 console.log('👥 创建测试用户与组织...'); const [adminUser] = await db.insert(users).values({ email: 'admin@ekb.com', name: 'System Admin', passwordHash: 'hashed_password_here' // 实际应使用 bcrypt/argon2 }).returning(); const [testUser] = await db.insert(users).values({ email: 'tester@ekb.com', name: 'Test User', passwordHash: 'hashed_password_here' }).returning(); const [engGroup] = await db.insert(groups).values({ name: 'Engineering Department' }).returning(); // 将 admin 加入工程组,并赋予 editor 角色 await db.insert(userGroups).values({ userId: adminUser.id, groupId: engGroup.id }); await db.insert(groupRoles).values({ groupId: engGroup.id, roleId: editorRole.id }); // 4. 创建资源与 ACL 测试 (核心:测试 Deny-Override) console.log('📂 创建测试资源与 ACL 规则...'); const [publicFolder] = await db.insert(resourceSchema).values({ name: 'Public Docs', path: '/public', type: 'folder', ownerId: adminUser.id }).returning(); const [secretFolder] = await db.insert(resourceSchema).values({ name: 'Secret Projects', path: '/projects/secret', type: 'folder', ownerId: adminUser.id }).returning(); // 为 secretFolder 设置一条针对 testUser 的显式 DENY 规则 await db.insert(aclRulesSchema).values({ resourceId: secretFolder.id, subjectType: 'user', subjectId: testUser.id, permissionType: 'deny', action: 'read' }); console.log('✅ Seed 脚本执行成功!'); console.log(`- 管理员: ${adminUser.email}`); console.log(`- 测试用户: ${testUser.email} (已被禁止访问 /projects/secret)`); console.log(`- 组织: ${engGroup.name}`); } catch (error) { console.error('❌ Seed 脚本执行失败:', error); process.exit(1); } } seed();