bindRolePermsLogic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package role
  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/model/roleperm"
  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 BindRolePermsLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewBindRolePermsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindRolePermsLogic {
  21. return &BindRolePermsLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *BindRolePermsLogic) BindRolePerms(req *types.BindPermsReq) error {
  28. role, err := l.svcCtx.SysRoleModel.FindOne(l.ctx, req.RoleId)
  29. if err != nil {
  30. return response.ErrNotFound("角色不存在")
  31. }
  32. if err := authHelper.RequireProductAdminFor(l.ctx, role.ProductCode); err != nil {
  33. return err
  34. }
  35. if len(req.PermIds) > 0 {
  36. seen := make(map[int64]bool, len(req.PermIds))
  37. uniqueIds := make([]int64, 0, len(req.PermIds))
  38. for _, id := range req.PermIds {
  39. if !seen[id] {
  40. seen[id] = true
  41. uniqueIds = append(uniqueIds, id)
  42. }
  43. }
  44. req.PermIds = uniqueIds
  45. }
  46. if len(req.PermIds) > 0 {
  47. perms, err := l.svcCtx.SysPermModel.FindByIds(l.ctx, req.PermIds)
  48. if err != nil {
  49. return err
  50. }
  51. if len(perms) != len(req.PermIds) {
  52. return response.ErrBadRequest("包含无效的权限ID")
  53. }
  54. for _, p := range perms {
  55. if p.ProductCode != role.ProductCode {
  56. return response.ErrBadRequest("不能绑定其他产品的权限")
  57. }
  58. if p.Status != consts.StatusEnabled {
  59. return response.ErrBadRequest(fmt.Sprintf("权限 %s 已被禁用,无法绑定", p.Code))
  60. }
  61. }
  62. }
  63. existingPermIds, err := l.svcCtx.SysRolePermModel.FindPermIdsByRoleId(l.ctx, req.RoleId)
  64. if err != nil {
  65. return err
  66. }
  67. existingSet := make(map[int64]bool, len(existingPermIds))
  68. for _, id := range existingPermIds {
  69. existingSet[id] = true
  70. }
  71. newSet := make(map[int64]bool, len(req.PermIds))
  72. for _, id := range req.PermIds {
  73. newSet[id] = true
  74. }
  75. var toAdd []int64
  76. for _, id := range req.PermIds {
  77. if !existingSet[id] {
  78. toAdd = append(toAdd, id)
  79. }
  80. }
  81. var toRemove []int64
  82. for _, id := range existingPermIds {
  83. if !newSet[id] {
  84. toRemove = append(toRemove, id)
  85. }
  86. }
  87. if len(toAdd) == 0 && len(toRemove) == 0 {
  88. return nil
  89. }
  90. if err := l.svcCtx.SysRolePermModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
  91. for _, permId := range toRemove {
  92. query := fmt.Sprintf("DELETE FROM %s WHERE `roleId` = ? AND `permId` = ?", l.svcCtx.SysRolePermModel.TableName())
  93. if _, err := session.ExecCtx(ctx, query, req.RoleId, permId); err != nil {
  94. return err
  95. }
  96. }
  97. if len(toAdd) > 0 {
  98. now := time.Now().Unix()
  99. data := make([]*roleperm.SysRolePerm, 0, len(toAdd))
  100. for _, permId := range toAdd {
  101. data = append(data, &roleperm.SysRolePerm{
  102. RoleId: req.RoleId,
  103. PermId: permId,
  104. CreateTime: now,
  105. UpdateTime: now,
  106. })
  107. }
  108. return l.svcCtx.SysRolePermModel.BatchInsertWithTx(ctx, session, data)
  109. }
  110. return nil
  111. }); err != nil {
  112. return err
  113. }
  114. affectedUserIds, _ := l.svcCtx.SysUserRoleModel.FindUserIdsByRoleId(l.ctx, req.RoleId)
  115. l.svcCtx.UserDetailsLoader.BatchDel(l.ctx, affectedUserIds, role.ProductCode)
  116. return nil
  117. }