e-scooter-rental-system/server/middleware/roleAuth.js

24 lines
723 B
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.

/**
* 角色权限分级中间件RBAC
* 用法: roleAuth('admin'), roleAuth('admin', 'store', 'rider')
*
* 角色说明:
* admin - 管理后台最高权限,可访问所有 API
* store - 门店管理员,可访问门店相关数据和业务操作
* rider - 骑手,只能访问自己的数据和公开浏览接口
*/
const roleAuth = (...allowedRoles) => {
return (req, res, next) => {
if (!req.user) {
return res.status(401).json({ success: false, message: '未登录' });
}
const { role } = req.user;
if (!allowedRoles.includes(role)) {
return res.status(403).json({ success: false, message: '无权访问' });
}
next();
};
};
module.exports = roleAuth;