sysUserPermModel.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. FindByUserIdForProduct(ctx context.Context, userId int64, productCode string) ([]SysUserPerm, error)
  16. DeleteByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) error
  17. }
  18. customSysUserPermModel struct {
  19. *defaultSysUserPermModel
  20. }
  21. )
  22. func NewSysUserPermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysUserPermModel {
  23. return &customSysUserPermModel{
  24. defaultSysUserPermModel: newSysUserPermModel(conn, c, cachePrefix, opts...),
  25. }
  26. }
  27. func (m *customSysUserPermModel) FindPermIdsByUserIdAndEffectForProduct(ctx context.Context, userId int64, effect string, productCode string) ([]int64, error) {
  28. var ids []int64
  29. query := fmt.Sprintf(
  30. "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` = ?",
  31. m.table)
  32. if err := m.QueryRowsNoCacheCtx(ctx, &ids, query, userId, effect, productCode, consts.StatusEnabled); err != nil {
  33. return nil, err
  34. }
  35. return ids, nil
  36. }
  37. func (m *customSysUserPermModel) FindByUserIdForProduct(ctx context.Context, userId int64, productCode string) ([]SysUserPerm, error) {
  38. var list []SysUserPerm
  39. query := fmt.Sprintf(
  40. "SELECT up.`id`, up.`userId`, up.`permId`, up.`effect`, up.`createTime`, up.`updateTime` FROM %s up INNER JOIN `sys_perm` p ON up.`permId` = p.`id` WHERE up.`userId` = ? AND p.`productCode` = ? AND p.`status` = ?",
  41. m.table)
  42. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, userId, productCode, consts.StatusEnabled); err != nil {
  43. return nil, err
  44. }
  45. return list, nil
  46. }
  47. func (m *customSysUserPermModel) DeleteByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) error {
  48. // 审计 L-R11-2:SELECT 只取 cache key 所需三列,不回灌全行。
  49. var list []struct {
  50. Id int64 `db:"id"`
  51. UserId int64 `db:"userId"`
  52. PermId int64 `db:"permId"`
  53. }
  54. 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)
  55. if err := session.QueryRowsCtx(ctx, &list, findQuery, userId, productCode); err != nil {
  56. return err
  57. }
  58. if len(list) == 0 {
  59. return nil
  60. }
  61. keys := make([]string, 0, len(list)*2)
  62. for _, data := range list {
  63. keys = append(keys,
  64. fmt.Sprintf("%s%v", cacheSysUserPermIdPrefix, data.Id),
  65. fmt.Sprintf("%s%v:%v", cacheSysUserPermUserIdPermIdPrefix, data.UserId, data.PermId),
  66. )
  67. }
  68. _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  69. query := fmt.Sprintf("DELETE FROM %s WHERE `userId` = ? AND `permId` IN (SELECT `id` FROM `sys_perm` WHERE `productCode` = ?)", m.table)
  70. return session.ExecCtx(ctx, query, userId, productCode)
  71. }, keys...)
  72. return err
  73. }