getUserPermsLogic.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Code scaffolded by goctl. Safe to edit.
  2. // goctl 1.10.1
  3. package user
  4. import (
  5. "context"
  6. authHelper "perms-system-server/internal/logic/auth"
  7. "perms-system-server/internal/middleware"
  8. "perms-system-server/internal/response"
  9. "perms-system-server/internal/svc"
  10. "perms-system-server/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type GetUserPermsLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewGetUserPermsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserPermsLogic {
  19. return &GetUserPermsLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. // GetUserPerms 查询用户在当前产品下的个性化权限覆盖项(仅 sys_user_perm 的 ALLOW/DENY 行,不含角色继承权限)。
  26. // 访问控制:超管不限;本人可查自己;其余调用者须是产品 ADMIN,且权限等级须高于目标用户。
  27. // 审计 L-R13-1:RequireProductAdminFor 在任何实体读取前调用,防止普通 MEMBER 通过响应差异枚举 userId 存在性。
  28. func (l *GetUserPermsLogic) GetUserPerms(req *types.GetUserPermsReq) (resp *types.GetUserPermsResp, err error) {
  29. caller := middleware.GetUserDetails(l.ctx)
  30. if caller == nil {
  31. return nil, response.ErrUnauthorized("未登录")
  32. }
  33. productCode := middleware.GetProductCode(l.ctx)
  34. isSelf := caller.UserId == req.UserId
  35. if !caller.IsSuperAdmin && !isSelf {
  36. // 审计 L-R13-1:先鉴权再读实体,避免枚举 userId。
  37. if err := authHelper.RequireProductAdminFor(l.ctx, productCode); err != nil {
  38. return nil, err
  39. }
  40. if err := authHelper.CheckManageAccess(l.ctx, l.svcCtx, req.UserId, productCode); err != nil {
  41. return nil, err
  42. }
  43. }
  44. rows, err := l.svcCtx.SysUserPermModel.FindByUserIdForProduct(l.ctx, req.UserId, productCode)
  45. if err != nil {
  46. return nil, err
  47. }
  48. perms := make([]types.UserPermItem, 0, len(rows))
  49. for _, r := range rows {
  50. perms = append(perms, types.UserPermItem{
  51. PermId: r.PermId,
  52. Effect: r.Effect,
  53. })
  54. }
  55. return &types.GetUserPermsResp{Perms: perms}, nil
  56. }