push
This commit is contained in:
@@ -20,29 +20,52 @@ function protectSelfUpdate(req, res, next) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户时的自保护:
|
||||
* 删除用户时的保护:
|
||||
* - 不能删除自己
|
||||
* - 不能删除最后一个管理员
|
||||
* - 不能删除比自己级别高的用户
|
||||
* - 最高只能删除同级用户
|
||||
* - 不能删除最后一个总经理
|
||||
*/
|
||||
async function protectUserDelete(req, res, next) {
|
||||
if (Number(req.params.id) === req.user.id) {
|
||||
const targetId = Number(req.params.id)
|
||||
const operatorLevel = req.user.role_level || 4
|
||||
|
||||
// 不能删除自己
|
||||
if (targetId === req.user.id) {
|
||||
return res.status(400).json({ code: 400, message: '不能删除自己' })
|
||||
}
|
||||
|
||||
const [rows] = await pool.query('SELECT department_id FROM users WHERE id = ?', [req.params.id])
|
||||
if (rows.length > 0) {
|
||||
const [adminDept] = await pool.query('SELECT id FROM departments WHERE name = ?', ['admin'])
|
||||
if (adminDept.length > 0 && rows[0].department_id === adminDept[0].id) {
|
||||
try {
|
||||
const [rows] = await pool.query('SELECT role_level, department_id FROM users WHERE id = ?', [targetId])
|
||||
if (rows.length === 0) {
|
||||
return res.status(404).json({ code: 404, message: '用户不存在' })
|
||||
}
|
||||
|
||||
const targetLevel = rows[0].role_level || 4
|
||||
|
||||
// 不能删除比自己级别高的用户(数字越小级别越高)
|
||||
if (targetLevel < operatorLevel) {
|
||||
return res.status(403).json({ code: 403, message: '不能删除比自己级别高的用户' })
|
||||
}
|
||||
|
||||
// 最高只能删除同级
|
||||
// 这个逻辑已经在上面的检查中覆盖了(targetLevel >= operatorLevel 才能继续)
|
||||
|
||||
// 不能删除最后一个总经理
|
||||
if (targetLevel === 1) {
|
||||
const [[{ cnt }]] = await pool.query(
|
||||
'SELECT COUNT(*) AS cnt FROM users WHERE department_id = ?',
|
||||
[adminDept[0].id]
|
||||
'SELECT COUNT(*) AS cnt FROM users WHERE role_level = 1'
|
||||
)
|
||||
if (cnt <= 1) {
|
||||
return res.status(400).json({ code: 400, message: '不能删除最后一个管理员' })
|
||||
return res.status(400).json({ code: 400, message: '不能删除最后一个总经理' })
|
||||
}
|
||||
}
|
||||
|
||||
next()
|
||||
} catch (e) {
|
||||
console.error('[protectUserDelete] error:', e)
|
||||
return res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
next()
|
||||
}
|
||||
|
||||
module.exports = { protectSelfUpdate, protectUserDelete }
|
||||
|
||||
Reference in New Issue
Block a user