Files
bj_power/bj_power_mes/internal/handler/perm.go
T
SunYF 756d978900 feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期
- 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度
- 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题
- 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险
- 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件
- 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
2026-10-06 19:36:22 +08:00

116 lines
4.9 KiB
Go
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.
package handler
import (
"net/http"
"strings"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/svc"
)
// pathPermMap 请求方法+路径 → 所需按钮级权限码(块2:后端接口兜底校验)。
// 路径中的 :id 统一以 * 结尾匹配。
var pathPermMap = map[string]string{
"POST:/api/v1/product-types": "produce.product:add",
"PUT:/api/v1/product-types": "produce.product:edit",
"DELETE:/api/v1/product-types/*": "produce.product:delete",
"POST:/api/v1/work-orders": "produce.workorder:add",
"PUT:/api/v1/work-orders": "produce.workorder:edit",
"POST:/api/v1/work-orders/status": "produce.workorder:edit",
"POST:/api/v1/work-orders/auto-schedule": "produce.workorder:dailyplan",
"DELETE:/api/v1/work-orders/*": "produce.workorder:delete",
"POST:/api/v1/daily-plans": "produce.workorder:dailyplan",
"POST:/api/v1/material-requests/generate": "produce.material:generate",
"POST:/api/v1/material-requests/auto-outbound": "produce.material:outbound",
// 数量不符处理(produce.qty 菜单下:补发/退库/调整关闭)
"POST:/api/v1/qty-reports/handle": "produce.qty:handle",
"POST:/api/v1/plc/send-process": "produce.plc:send",
"POST:/api/v1/process-flows": "produce.processflow:add",
"POST:/api/v1/process-flows/status": "produce.processflow:edit",
"DELETE:/api/v1/process-flows/*": "produce.processflow:delete",
"POST:/api/v1/process-flows/upload": "produce.processflow:upload",
"POST:/api/v1/process-steps": "produce.processflow:edit",
"POST:/api/v1/stations": "produce.station:edit",
"DELETE:/api/v1/stations/*": "produce.station:delete",
// 基础设置(WMS 地址/需求窗口/外推参数/日志保留)
"PUT:/api/v1/settings": "sys.settings:edit",
// 过程巡检汇总生成《工序间检验记录》(巡检终端操作域)
"POST:/api/v1/inspections/generate-inter-process": "sys.inspect:process",
// 质量检验处置(过程检/完工检不合格处置 退货/返修/退换):按记录类型在 handler 内做权限粒度校验
//(produce.quality:process / produce.quality:final),此处不映射单一码,避免误拦/误放。
// 检验员签字确认(需求B 第二签,巡检终端「待签任务」)
"POST:/api/v1/signs/inspector": "sys.inspect:sign",
// 附件中心(sys.attachment 菜单下):删除 / 归档清理属管理动作;
// 列表/预览/下载不设按钮门槛(有菜单权限即可查阅)
"POST:/api/v1/attachment/delete": "sys.attachment:manage",
"POST:/api/v1/attachment/archive": "sys.attachment:manage",
// 账号管理(拆分自原 sys.rbac:user;sys.account 菜单下增/改/删)
"POST:/api/v1/users": "sys.account:add",
"PUT:/api/v1/users": "sys.account:edit",
"DELETE:/api/v1/users/*": "sys.account:delete",
// 角色管理(拆分自原 sys.rbac:role/perm;sys.role 菜单下增/改/删)
"POST:/api/v1/roles": "sys.role:add",
"PUT:/api/v1/roles": "sys.role:edit",
"DELETE:/api/v1/roles/*": "sys.role:delete",
"POST:/api/v1/permissions": "sys.role:perm",
"PUT:/api/v1/permissions": "sys.role:perm",
"DELETE:/api/v1/permissions/*": "sys.role:perm",
}
// userHasPerm 校验当前登录用户是否拥有某权限码(SUPER_ADMIN 或 * 放行)。
// 用户ID 使用 uidFromRequest:优先取 context,缺失时回退解析 Authorization token。
func userHasPerm(r *http.Request, svcCtx *svc.ServiceContext, code string) bool {
if code == "" {
return true
}
userId := uidFromRequest(r, svcCtx)
if userId <= 0 {
return false
}
usr, err := svcCtx.EntClient.User.Get(r.Context(), userId)
if err != nil {
return false
}
role, err := svcCtx.EntClient.Role.Get(r.Context(), usr.RoleId)
if err != nil {
return false
}
if role.Code == "SUPER_ADMIN" {
return true
}
for _, c := range role.PermissionCodes {
if c == "*" || c == code {
return true
}
}
return false
}
// permissionGuard 按钮级权限后端兜底中间件(仅对写操作映射表内的路径生效)
func permissionGuard(svcCtx *svc.ServiceContext) func(http.HandlerFunc) http.HandlerFunc {
return func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
code, ok := pathPerm(r.Method, r.URL.Path)
if ok && !userHasPerm(r, svcCtx, code) {
httpx.FailHTTP(w, http.StatusForbidden, "无操作权限:"+code)
return
}
next(w, r)
}
}
}
// pathPerm 先按完整路径精确匹配;未命中时把末段替换为 * 再匹配(用于 :id 结尾的路径)。
func pathPerm(method, path string) (string, bool) {
key := method + ":" + strings.TrimSuffix(path, "/")
if code, ok := pathPermMap[key]; ok {
return code, true
}
segs := strings.Split(key, "/")
if len(segs) > 0 {
segs[len(segs)-1] = "*"
}
code, ok := pathPermMap[strings.Join(segs, "/")]
return code, ok
}