setUserPermsLogic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package user
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "perms-system-server/internal/consts"
  7. authHelper "perms-system-server/internal/logic/auth"
  8. "perms-system-server/internal/middleware"
  9. "perms-system-server/internal/model/userperm"
  10. "perms-system-server/internal/response"
  11. "perms-system-server/internal/svc"
  12. "perms-system-server/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. "github.com/zeromicro/go-zero/core/stores/sqlx"
  15. )
  16. type SetUserPermsLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewSetUserPermsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetUserPermsLogic {
  22. return &SetUserPermsLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. }
  27. }
  28. func (l *SetUserPermsLogic) SetUserPerms(req *types.SetPermsReq) error {
  29. if _, err := l.svcCtx.SysUserModel.FindOne(l.ctx, req.UserId); err != nil {
  30. return response.ErrNotFound("用户不存在")
  31. }
  32. productCode := middleware.GetProductCode(l.ctx)
  33. if err := authHelper.CheckManageAccess(l.ctx, l.svcCtx, req.UserId, productCode); err != nil {
  34. return err
  35. }
  36. if _, err := l.svcCtx.SysProductMemberModel.FindOneByProductCodeUserId(l.ctx, productCode, req.UserId); err != nil {
  37. return response.ErrBadRequest("目标用户不是当前产品的成员")
  38. }
  39. for _, p := range req.Perms {
  40. if p.Effect != consts.PermEffectAllow && p.Effect != consts.PermEffectDeny {
  41. return response.ErrBadRequest("effect值无效,仅支持 ALLOW 和 DENY")
  42. }
  43. }
  44. if len(req.Perms) > 0 {
  45. seen := make(map[int64]string, len(req.Perms))
  46. uniquePerms := make([]types.UserPermItem, 0, len(req.Perms))
  47. for _, p := range req.Perms {
  48. if prev, ok := seen[p.PermId]; ok {
  49. if prev != p.Effect {
  50. return response.ErrBadRequest("同一权限ID不能同时为 ALLOW 和 DENY")
  51. }
  52. continue
  53. }
  54. seen[p.PermId] = p.Effect
  55. uniquePerms = append(uniquePerms, p)
  56. }
  57. req.Perms = uniquePerms
  58. }
  59. if len(req.Perms) > 0 {
  60. permIds := make([]int64, 0, len(req.Perms))
  61. for _, p := range req.Perms {
  62. permIds = append(permIds, p.PermId)
  63. }
  64. perms, err := l.svcCtx.SysPermModel.FindByIds(l.ctx, permIds)
  65. if err != nil {
  66. return err
  67. }
  68. if len(perms) != len(req.Perms) {
  69. return response.ErrBadRequest("包含无效的权限ID")
  70. }
  71. for _, p := range perms {
  72. if p.ProductCode != productCode {
  73. return response.ErrBadRequest("不能设置其他产品的权限")
  74. }
  75. if p.Status != consts.StatusEnabled {
  76. return response.ErrBadRequest(fmt.Sprintf("权限 %s 已被禁用,无法设置", p.Code))
  77. }
  78. }
  79. }
  80. if err := l.svcCtx.SysUserPermModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
  81. if err := l.svcCtx.SysUserPermModel.DeleteByUserIdForProductTx(ctx, session, req.UserId, productCode); err != nil {
  82. return err
  83. }
  84. if len(req.Perms) == 0 {
  85. return nil
  86. }
  87. now := time.Now().Unix()
  88. data := make([]*userperm.SysUserPerm, 0, len(req.Perms))
  89. for _, p := range req.Perms {
  90. data = append(data, &userperm.SysUserPerm{
  91. UserId: req.UserId,
  92. PermId: p.PermId,
  93. Effect: p.Effect,
  94. CreateTime: now,
  95. UpdateTime: now,
  96. })
  97. }
  98. return l.svcCtx.SysUserPermModel.BatchInsertWithTx(ctx, session, data)
  99. }); err != nil {
  100. return err
  101. }
  102. l.svcCtx.UserDetailsLoader.Del(l.ctx, req.UserId, productCode)
  103. return nil
  104. }