oms/backend/database/migrations/20261104070300-seed-permissions-and-roles.js
liufumingren 85938c81ec feat: 组织架构、权限管理与员工档案
- 组织架构:主体管理改为组织架构页(左侧 tab:集团设置 / 下属公司),主体列表仅保留顶部下拉切换
  - 新增下属公司表 principal_subsidiaries、接口与页面,按当前主体隔离
  - 集团设置读取/编辑当前主体基础信息,补所属主体校验与更新字段白名单
- 权限管理:新增 permissions / roles / role_permissions 表
  - 权限以现有顶级菜单为分类,每个菜单两种范围:集团全部 / 所在公司
  - 系统预设角色:系统管理员(全部集团权限)、货主权限(全部所在公司权限)
  - 权限管理页只展示角色组,支持按菜单勾选权限;系统预设角色只读
- 员工档案:账号菜单改为员工档案
  - 新增员工-下属公司多对多关联(principal_user_companies)
  - 创建/编辑员工必须选择角色组;除所有者外必须至少关联一家公司
- 菜单重命名与旧地址重定向;账号导出新增角色组、所属公司

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-04 20:52:43 +08:00

87 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 权限 / 角色组初始数据:
* 1. 以现有菜单作为分类,为每个顶级菜单生成两种权限:集团全部(scope=1)、所在公司(scope=2)
* 2. 预置系统角色:系统管理员(全部「集团全部」权限)、货主权限(全部「所在公司」权限)
* 3. 按原有 principal_users.role 回填角色组:owner → 货主权限,admin → 系统管理员
*/
module.exports = {
async up(queryInterface) {
const sequelize = queryInterface.sequelize;
const [ menus ] = await sequelize.query(
'SELECT id, name FROM menus WHERE parent_id = 0 ORDER BY sort DESC, id ASC'
);
for (const menu of menus) {
const rows = [
{ scope: 1, suffix: 'group', label: '集团全部' },
{ scope: 2, suffix: 'company', label: '所在公司' },
];
for (const row of rows) {
await sequelize.query(
`INSERT INTO permissions (menu_id, name, code, scope, status, remark, created_at, updated_at)
VALUES (?, ?, ?, ?, 1, ?, NOW(), NOW())`,
{
replacements: [
menu.id,
`${menu.name}(${row.label})`,
`menu_${menu.id}_${row.suffix}`,
row.scope,
`菜单「${menu.name}」的${row.label}权限`,
],
}
);
}
}
const roles = [
{ name: '系统管理员', code: 'super_admin', remark: '系统预设:拥有全部菜单的集团权限' },
{ name: '货主权限', code: 'owner', remark: '系统预设:拥有全部菜单的所在公司权限' },
];
for (const role of roles) {
await sequelize.query(
`INSERT INTO roles (name, code, is_system, status, remark, created_at, updated_at)
VALUES (?, ?, 1, 1, ?, NOW(), NOW())`,
{ replacements: [ role.name, role.code, role.remark ] }
);
}
const [ roleRows ] = await sequelize.query('SELECT id, code FROM roles WHERE is_system = 1');
const roleIdByCode = new Map(roleRows.map(row => [ row.code, Number(row.id) ]));
const scopeByRoleCode = { super_admin: 1, owner: 2 };
for (const [ code, scope ] of Object.entries(scopeByRoleCode)) {
const roleId = roleIdByCode.get(code);
if (!roleId) continue;
await sequelize.query(
`INSERT INTO role_permissions (role_id, permission_id, created_at, updated_at)
SELECT ?, p.id, NOW(), NOW() FROM permissions p WHERE p.scope = ?`,
{ replacements: [ roleId, scope ] }
);
}
// 旧角色字段回填:owner → 货主权限,admin → 系统管理员(member 保持未分配)
if (roleIdByCode.get('owner')) {
await sequelize.query(
'UPDATE principal_users SET role_id = ? WHERE role_id IS NULL AND role = ?',
{ replacements: [ roleIdByCode.get('owner'), 'owner' ] }
);
}
if (roleIdByCode.get('super_admin')) {
await sequelize.query(
'UPDATE principal_users SET role_id = ? WHERE role_id IS NULL AND role = ?',
{ replacements: [ roleIdByCode.get('super_admin'), 'admin' ] }
);
}
},
async down(queryInterface) {
const sequelize = queryInterface.sequelize;
await sequelize.query('UPDATE principal_users SET role_id = NULL');
await sequelize.query('DELETE FROM role_permissions');
await sequelize.query('DELETE FROM roles');
await sequelize.query('DELETE FROM permissions');
},
};