setUserPermsLogic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. permIds := make([]int64, 0, len(req.Perms))
  42. for _, p := range req.Perms {
  43. permIds = append(permIds, p.PermId)
  44. }
  45. perms, err := l.svcCtx.SysPermModel.FindByIds(l.ctx, permIds)
  46. if err != nil {
  47. return err
  48. }
  49. if len(perms) != len(req.Perms) {
  50. return response.ErrBadRequest("包含无效的权限ID")
  51. }
  52. for _, p := range perms {
  53. if p.ProductCode != productCode {
  54. return response.ErrBadRequest("不能设置其他产品的权限")
  55. }
  56. }
  57. }
  58. if err := l.svcCtx.SysUserPermModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
  59. if err := l.svcCtx.SysUserPermModel.DeleteByUserIdTx(ctx, session, req.UserId); err != nil {
  60. return err
  61. }
  62. if len(req.Perms) == 0 {
  63. return nil
  64. }
  65. now := time.Now().Unix()
  66. data := make([]*userperm.SysUserPerm, 0, len(req.Perms))
  67. for _, p := range req.Perms {
  68. data = append(data, &userperm.SysUserPerm{
  69. UserId: req.UserId,
  70. PermId: p.PermId,
  71. Effect: p.Effect,
  72. CreateTime: now,
  73. UpdateTime: now,
  74. })
  75. }
  76. return l.svcCtx.SysUserPermModel.BatchInsertWithTx(ctx, session, data)
  77. }); err != nil {
  78. return err
  79. }
  80. l.svcCtx.UserDetailsLoader.Del(l.ctx, req.UserId, productCode)
  81. return nil
  82. }