setUserPermsLogic.go 4.0 KB

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