syncPermsLogic.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package pub
  2. import (
  3. "context"
  4. "time"
  5. "perms-system-server/internal/consts"
  6. permModel "perms-system-server/internal/model/perm"
  7. "perms-system-server/internal/response"
  8. "perms-system-server/internal/svc"
  9. "perms-system-server/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "github.com/zeromicro/go-zero/core/stores/sqlx"
  12. "golang.org/x/crypto/bcrypt"
  13. )
  14. type SyncPermsLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewSyncPermsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncPermsLogic {
  20. return &SyncPermsLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. func (l *SyncPermsLogic) SyncPerms(req *types.SyncPermsReq) (resp *types.SyncPermsResp, err error) {
  27. product, err := l.svcCtx.SysProductModel.FindOneByAppKey(l.ctx, req.AppKey)
  28. if err != nil {
  29. return nil, response.ErrUnauthorized("无效的appKey")
  30. }
  31. if err := bcrypt.CompareHashAndPassword([]byte(product.AppSecret), []byte(req.AppSecret)); err != nil {
  32. return nil, response.ErrUnauthorized("appSecret验证失败")
  33. }
  34. if product.Status != consts.StatusEnabled {
  35. return nil, response.ErrForbidden("产品已被禁用")
  36. }
  37. existingMap, err := l.svcCtx.SysPermModel.FindMapByProductCode(l.ctx, product.Code)
  38. if err != nil {
  39. return nil, err
  40. }
  41. now := time.Now().Unix()
  42. var added, updated int64
  43. codes := make([]string, 0, len(req.Perms))
  44. var toInsert []*permModel.SysPerm
  45. var toUpdate []*permModel.SysPerm
  46. seen := make(map[string]bool, len(req.Perms))
  47. for _, item := range req.Perms {
  48. if seen[item.Code] {
  49. continue
  50. }
  51. seen[item.Code] = true
  52. codes = append(codes, item.Code)
  53. existing, ok := existingMap[item.Code]
  54. if !ok {
  55. toInsert = append(toInsert, &permModel.SysPerm{
  56. ProductCode: product.Code,
  57. Name: item.Name,
  58. Code: item.Code,
  59. Remark: item.Remark,
  60. Status: consts.StatusEnabled,
  61. CreateTime: now,
  62. UpdateTime: now,
  63. })
  64. added++
  65. continue
  66. }
  67. if existing.Name != item.Name || existing.Remark != item.Remark || existing.Status != consts.StatusEnabled {
  68. existing.Name = item.Name
  69. existing.Remark = item.Remark
  70. existing.Status = consts.StatusEnabled
  71. existing.UpdateTime = now
  72. toUpdate = append(toUpdate, existing)
  73. updated++
  74. }
  75. }
  76. var disabled int64
  77. if err := l.svcCtx.SysPermModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
  78. if len(toInsert) > 0 {
  79. if err := l.svcCtx.SysPermModel.BatchInsertWithTx(ctx, session, toInsert); err != nil {
  80. return err
  81. }
  82. }
  83. if len(toUpdate) > 0 {
  84. if err := l.svcCtx.SysPermModel.BatchUpdateWithTx(ctx, session, toUpdate); err != nil {
  85. return err
  86. }
  87. }
  88. var err error
  89. disabled, err = l.svcCtx.SysPermModel.DisableNotInCodesWithTx(ctx, session, product.Code, codes, now)
  90. return err
  91. }); err != nil {
  92. return nil, err
  93. }
  94. if added > 0 || updated > 0 || disabled > 0 {
  95. l.svcCtx.UserDetailsLoader.CleanByProduct(l.ctx, product.Code)
  96. }
  97. return &types.SyncPermsResp{
  98. Added: added,
  99. Updated: updated,
  100. Disabled: disabled,
  101. }, nil
  102. }