userProductsLogic.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package member
  2. import (
  3. "context"
  4. "perms-system-server/internal/middleware"
  5. "perms-system-server/internal/response"
  6. "perms-system-server/internal/svc"
  7. "perms-system-server/internal/types"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type UserProductsLogic struct {
  11. logx.Logger
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. }
  15. func NewUserProductsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserProductsLogic {
  16. return &UserProductsLogic{
  17. Logger: logx.WithContext(ctx),
  18. ctx: ctx,
  19. svcCtx: svcCtx,
  20. }
  21. }
  22. // UserProducts 查询指定用户加入的产品列表。
  23. // 访问控制:仅超管或本人可调用,防止普通用户枚举他人的产品归属(IDOR)。
  24. func (l *UserProductsLogic) UserProducts(req *types.UserProductsReq) (resp *types.UserProductsResp, err error) {
  25. caller := middleware.GetUserDetails(l.ctx)
  26. if caller == nil {
  27. return nil, response.ErrUnauthorized("未登录")
  28. }
  29. if !caller.IsSuperAdmin && caller.UserId != req.UserId {
  30. return nil, response.ErrForbidden("无权查看他人的产品列表")
  31. }
  32. members, err := l.svcCtx.SysProductMemberModel.FindByUserId(l.ctx, req.UserId)
  33. if err != nil {
  34. return nil, err
  35. }
  36. items := make([]types.UserProductItem, 0, len(members))
  37. for _, m := range members {
  38. product, err := l.svcCtx.SysProductModel.FindOneByCode(l.ctx, m.ProductCode)
  39. if err != nil {
  40. continue
  41. }
  42. items = append(items, types.UserProductItem{
  43. ProductCode: m.ProductCode,
  44. ProductName: product.Name,
  45. MemberType: m.MemberType,
  46. Status: m.Status,
  47. })
  48. }
  49. return &types.UserProductsResp{List: items}, nil
  50. }