sysUserPermModel.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package userperm
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "perms-system-server/internal/consts"
  7. "github.com/zeromicro/go-zero/core/stores/cache"
  8. "github.com/zeromicro/go-zero/core/stores/sqlx"
  9. )
  10. var _ SysUserPermModel = (*customSysUserPermModel)(nil)
  11. type (
  12. SysUserPermModel interface {
  13. sysUserPermModel
  14. FindPermIdsByUserIdAndEffectForProduct(ctx context.Context, userId int64, effect string, productCode string) ([]int64, error)
  15. DeleteByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) error
  16. }
  17. customSysUserPermModel struct {
  18. *defaultSysUserPermModel
  19. }
  20. )
  21. func NewSysUserPermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysUserPermModel {
  22. return &customSysUserPermModel{
  23. defaultSysUserPermModel: newSysUserPermModel(conn, c, cachePrefix, opts...),
  24. }
  25. }
  26. func (m *customSysUserPermModel) FindPermIdsByUserIdAndEffectForProduct(ctx context.Context, userId int64, effect string, productCode string) ([]int64, error) {
  27. var ids []int64
  28. query := fmt.Sprintf(
  29. "SELECT up.`permId` FROM %s up INNER JOIN `sys_perm` p ON up.`permId` = p.`id` WHERE up.`userId` = ? AND up.`effect` = ? AND p.`productCode` = ? AND p.`status` = ?",
  30. m.table)
  31. if err := m.QueryRowsNoCacheCtx(ctx, &ids, query, userId, effect, productCode, consts.StatusEnabled); err != nil {
  32. return nil, err
  33. }
  34. return ids, nil
  35. }
  36. func (m *customSysUserPermModel) DeleteByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) error {
  37. // 审计 L-R11-2:SELECT 只取 cache key 所需三列,不回灌全行。
  38. var list []struct {
  39. Id int64 `db:"id"`
  40. UserId int64 `db:"userId"`
  41. PermId int64 `db:"permId"`
  42. }
  43. findQuery := fmt.Sprintf("SELECT `id`, `userId`, `permId` FROM %s WHERE `userId` = ? AND `permId` IN (SELECT `id` FROM `sys_perm` WHERE `productCode` = ?) FOR UPDATE", m.table)
  44. if err := session.QueryRowsCtx(ctx, &list, findQuery, userId, productCode); err != nil {
  45. return err
  46. }
  47. if len(list) == 0 {
  48. return nil
  49. }
  50. keys := make([]string, 0, len(list)*2)
  51. for _, data := range list {
  52. keys = append(keys,
  53. fmt.Sprintf("%s%v", cacheSysUserPermIdPrefix, data.Id),
  54. fmt.Sprintf("%s%v:%v", cacheSysUserPermUserIdPermIdPrefix, data.UserId, data.PermId),
  55. )
  56. }
  57. _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  58. query := fmt.Sprintf("DELETE FROM %s WHERE `userId` = ? AND `permId` IN (SELECT `id` FROM `sys_perm` WHERE `productCode` = ?)", m.table)
  59. return session.ExecCtx(ctx, query, userId, productCode)
  60. }, keys...)
  61. return err
  62. }