setUserPermsLogic.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // SetUserPerms 设置用户个性化权限。对指定用户在当前产品下做权限全量覆盖,支持 ALLOW(附加)和 DENY(拒绝)两种效果,用于角色权限之外的细粒度调整。
  29. func (l *SetUserPermsLogic) SetUserPerms(req *types.SetPermsReq) error {
  30. if _, err := l.svcCtx.SysUserModel.FindOne(l.ctx, req.UserId); err != nil {
  31. return response.ErrNotFound("用户不存在")
  32. }
  33. productCode := middleware.GetProductCode(l.ctx)
  34. if err := authHelper.RequireProductAdminFor(l.ctx, productCode); err != nil {
  35. return err
  36. }
  37. product, err := l.svcCtx.SysProductModel.FindOneByCode(l.ctx, productCode)
  38. if err != nil {
  39. return response.ErrNotFound("产品不存在")
  40. }
  41. if product.Status != consts.StatusEnabled {
  42. return response.ErrBadRequest("产品已被禁用,无法设置权限")
  43. }
  44. if err := authHelper.CheckManageAccess(l.ctx, l.svcCtx, req.UserId, productCode); err != nil {
  45. return err
  46. }
  47. member, memberErr := l.svcCtx.SysProductMemberModel.FindOneByProductCodeUserId(l.ctx, productCode, req.UserId)
  48. if memberErr != nil {
  49. return response.ErrBadRequest("目标用户不是当前产品的成员")
  50. }
  51. if member.Status != consts.StatusEnabled {
  52. return response.ErrBadRequest("目标用户的成员资格已被禁用")
  53. }
  54. for _, p := range req.Perms {
  55. if p.Effect != consts.PermEffectAllow && p.Effect != consts.PermEffectDeny {
  56. return response.ErrBadRequest("effect值无效,仅支持 ALLOW 和 DENY")
  57. }
  58. }
  59. perms := req.Perms
  60. if len(perms) > 0 {
  61. seen := make(map[int64]string, len(perms))
  62. uniquePerms := make([]types.UserPermItem, 0, len(perms))
  63. for _, p := range perms {
  64. if prev, ok := seen[p.PermId]; ok {
  65. if prev != p.Effect {
  66. return response.ErrBadRequest("同一权限ID不能同时为 ALLOW 和 DENY")
  67. }
  68. continue
  69. }
  70. seen[p.PermId] = p.Effect
  71. uniquePerms = append(uniquePerms, p)
  72. }
  73. perms = uniquePerms
  74. }
  75. if len(perms) > 0 {
  76. permIds := make([]int64, 0, len(perms))
  77. for _, p := range perms {
  78. permIds = append(permIds, p.PermId)
  79. }
  80. dbPerms, err := l.svcCtx.SysPermModel.FindByIds(l.ctx, permIds)
  81. if err != nil {
  82. return err
  83. }
  84. if len(dbPerms) != len(perms) {
  85. return response.ErrBadRequest("包含无效的权限ID")
  86. }
  87. for _, p := range dbPerms {
  88. if p.ProductCode != productCode {
  89. return response.ErrBadRequest("不能设置其他产品的权限")
  90. }
  91. if p.Status != consts.StatusEnabled {
  92. return response.ErrBadRequest(fmt.Sprintf("权限 %s 已被禁用,无法设置", p.Code))
  93. }
  94. }
  95. }
  96. if err := l.svcCtx.SysUserPermModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
  97. if err := l.svcCtx.SysUserPermModel.DeleteByUserIdForProductTx(ctx, session, req.UserId, productCode); err != nil {
  98. return err
  99. }
  100. if len(perms) == 0 {
  101. return nil
  102. }
  103. now := time.Now().Unix()
  104. data := make([]*userperm.SysUserPerm, 0, len(perms))
  105. for _, p := range perms {
  106. data = append(data, &userperm.SysUserPerm{
  107. UserId: req.UserId,
  108. PermId: p.PermId,
  109. Effect: p.Effect,
  110. CreateTime: now,
  111. UpdateTime: now,
  112. })
  113. }
  114. return l.svcCtx.SysUserPermModel.BatchInsertWithTx(ctx, session, data)
  115. }); err != nil {
  116. return err
  117. }
  118. l.svcCtx.UserDetailsLoader.Del(l.ctx, req.UserId, productCode)
  119. return nil
  120. }