This commit is contained in:
Kyaru
2026-06-30 23:11:19 +08:00
parent ccb4c32614
commit 11cb335507
8 changed files with 295 additions and 92 deletions

View File

@@ -4,7 +4,7 @@
* 允许在创建员工时一并创建系统用户账号(需 user:manage 权限)
* 将用户创建参数挂到 req._createUser由路由在写入 employee 后处理
*/
function allowUserCreation(req, res, next) {
async function allowUserCreation(req, res, next) {
const { username, password, department_id } = req.body || {}
// 没传用户相关字段 → 只创建员工,跳过
@@ -27,8 +27,37 @@ function allowUserCreation(req, res, next) {
return res.status(400).json({ code: 400, message: '密码至少 6 位' })
}
// 检查是否是添加总经理办公室的用户
try {
const { pool } = require('../db')
const [dept] = await pool.query('SELECT name FROM departments WHERE id = ?', [department_id])
if (dept.length > 0 && dept[0].name === 'general_manager') {
const operatorLevel = req.user.role_level || 4
const operatorDept = req.user.departmentName
// 只有信息技术部和人力资源部的部门经理才能添加总经理办公室的用户
if (operatorLevel > 2 || !['admin', 'hr'].includes(operatorDept)) {
return res.status(403).json({ code: 403, message: '只有信息技术部和人力资源部的部门经理才能添加总经理办公室的用户' })
}
// 一般科员不能添加总经理办公室的用户
if (operatorLevel === 4) {
return res.status(403).json({ code: 403, message: '一般科员不能添加总经理办公室的用户' })
}
// 只能添加副总经理,总经理是系统预设的
// 这个检查在员工创建时通过 position 字段控制
}
} catch (e) {
console.error('[allowUserCreation] error:', e)
return res.status(500).json({ code: 500, message: e.message })
}
// 获取角色级别
const { role_level } = req.body || {}
// 挂到 req供路由在 INSERT employee 后使用
req._createUser = { username, password, department_id }
req._createUser = { username, password, department_id, role_level: role_level || 4 }
next()
}