setUserPermsLogic.go 2.9 KB

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