sysPermModel.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package perm
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "perms-system-server/internal/consts"
  8. "github.com/zeromicro/go-zero/core/stores/cache"
  9. "github.com/zeromicro/go-zero/core/stores/sqlx"
  10. )
  11. var _ SysPermModel = (*customSysPermModel)(nil)
  12. type (
  13. SysPermModel interface {
  14. sysPermModel
  15. FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error)
  16. FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error)
  17. FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error)
  18. // FindMapByProductCodeWithTx 在事务内查询权限快照;配合 SysProductModel.LockByCodeTx 锁住
  19. // product 行,可把"读取现有权限 → 增/改/禁用"这段与其他 SyncPermissions 串行化,
  20. // 避免两次并发同步都认为 code X 不存在并并发 INSERT 导致 1062(见审计 M-6)。
  21. FindMapByProductCodeWithTx(ctx context.Context, session sqlx.Session, productCode string) (map[string]*SysPerm, error)
  22. DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error)
  23. }
  24. customSysPermModel struct {
  25. *defaultSysPermModel
  26. }
  27. )
  28. func NewSysPermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysPermModel {
  29. return &customSysPermModel{
  30. defaultSysPermModel: newSysPermModel(conn, c, cachePrefix, opts...),
  31. }
  32. }
  33. func (m *customSysPermModel) FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error) {
  34. var total int64
  35. countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE `productCode` = ?", m.table)
  36. if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, productCode); err != nil {
  37. return nil, 0, err
  38. }
  39. var list []*SysPerm
  40. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? ORDER BY id DESC LIMIT ?,?", sysPermRows, m.table)
  41. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode, (page-1)*pageSize, pageSize); err != nil {
  42. return nil, 0, err
  43. }
  44. return list, total, nil
  45. }
  46. func (m *customSysPermModel) FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error) {
  47. // 审计 L-1:第 7 轮 L-4 已经统一走 prepared statement 占位符,这里对齐同一口径,避免未来把
  48. // StatusEnabled 从 int 改成 int8/枚举时 %d 悄悄不匹配;另一层收益是 SQL 指纹更稳定,慢查询
  49. // 日志 / DBA 审计能按参数化查询稳定归并。
  50. var codes []string
  51. query := fmt.Sprintf("SELECT `code` FROM %s WHERE `productCode` = ? AND `status` = ?", m.table)
  52. if err := m.QueryRowsNoCacheCtx(ctx, &codes, query, productCode, consts.StatusEnabled); err != nil {
  53. return nil, err
  54. }
  55. return codes, nil
  56. }
  57. func (m *customSysPermModel) FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error) {
  58. if len(ids) == 0 {
  59. return nil, nil
  60. }
  61. placeholders := make([]string, len(ids))
  62. args := make([]interface{}, len(ids))
  63. for i, id := range ids {
  64. placeholders[i] = "?"
  65. args[i] = id
  66. }
  67. var list []*SysPerm
  68. query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` IN (%s)", sysPermRows, m.table, strings.Join(placeholders, ","))
  69. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
  70. return nil, err
  71. }
  72. return list, nil
  73. }
  74. func (m *customSysPermModel) FindMapByProductCodeWithTx(ctx context.Context, session sqlx.Session, productCode string) (map[string]*SysPerm, error) {
  75. var list []*SysPerm
  76. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ?", sysPermRows, m.table)
  77. if err := session.QueryRowsCtx(ctx, &list, query, productCode); err != nil {
  78. return nil, err
  79. }
  80. result := make(map[string]*SysPerm, len(list))
  81. for _, p := range list {
  82. result[p.Code] = p
  83. }
  84. return result, nil
  85. }
  86. func (m *customSysPermModel) DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error) {
  87. // 审计 L-1:把 status = %d 全部换成参数占位符,与 sysUserRole / sysUserPerm / sysRole
  88. // L-4 修复风格对齐。占位符顺序:
  89. // SELECT:productCode, statusEnabled, [codes...]
  90. // UPDATE:statusDisabled, now, productCode, statusEnabled, [codes...]
  91. // 先查出将被禁用的行,构建缓存 key
  92. var findQuery string
  93. var findArgs []interface{}
  94. if len(codes) == 0 {
  95. findQuery = fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? AND `status` = ?", sysPermRows, m.table)
  96. findArgs = []interface{}{productCode, consts.StatusEnabled}
  97. } else {
  98. placeholders := make([]string, len(codes))
  99. findArgs = make([]interface{}, 0, len(codes)+2)
  100. findArgs = append(findArgs, productCode, consts.StatusEnabled)
  101. for i, code := range codes {
  102. placeholders[i] = "?"
  103. findArgs = append(findArgs, code)
  104. }
  105. findQuery = fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? AND `status` = ? AND `code` NOT IN (%s)",
  106. sysPermRows, m.table, strings.Join(placeholders, ","))
  107. }
  108. // 审计 L-R11-2:本段 SELECT 的唯一用途是构造缓存失效键 —— 只需 id / productCode / code 三列;
  109. // 原来的 SELECT %s (全部列) 会把 name/remark/status/createTime/updateTime 等业务字段也一并
  110. // 搬回应用层然后丢弃,DisableNotInCodesWithTx 在 SyncPerms 高频场景(一次提交涉及 <1k perm)
  111. // 下是纯浪费。改拼一个精简版 findQuery,复用 findArgs。
  112. findQuery = strings.Replace(findQuery, sysPermRows, "`id`, `productCode`, `code`", 1)
  113. var affected []struct {
  114. Id int64 `db:"id"`
  115. ProductCode string `db:"productCode"`
  116. Code string `db:"code"`
  117. }
  118. if err := session.QueryRowsCtx(ctx, &affected, findQuery+" FOR UPDATE", findArgs...); err != nil {
  119. return 0, err
  120. }
  121. if len(affected) == 0 {
  122. return 0, nil
  123. }
  124. keys := make([]string, 0, len(affected)*2)
  125. for _, data := range affected {
  126. keys = append(keys,
  127. fmt.Sprintf("%s%v", cacheSysPermIdPrefix, data.Id),
  128. fmt.Sprintf("%s%v:%v", cacheSysPermProductCodeCodePrefix, data.ProductCode, data.Code),
  129. )
  130. }
  131. var updateQuery string
  132. var updateArgs []interface{}
  133. if len(codes) == 0 {
  134. updateQuery = fmt.Sprintf("UPDATE %s SET `status` = ?, `updateTime` = ? WHERE `productCode` = ? AND `status` = ?", m.table)
  135. updateArgs = []interface{}{consts.StatusDisabled, now, productCode, consts.StatusEnabled}
  136. } else {
  137. placeholders := make([]string, len(codes))
  138. updateArgs = make([]interface{}, 0, len(codes)+4)
  139. updateArgs = append(updateArgs, consts.StatusDisabled, now, productCode, consts.StatusEnabled)
  140. for i, code := range codes {
  141. placeholders[i] = "?"
  142. updateArgs = append(updateArgs, code)
  143. }
  144. updateQuery = fmt.Sprintf("UPDATE %s SET `status` = ?, `updateTime` = ? WHERE `productCode` = ? AND `status` = ? AND `code` NOT IN (%s)",
  145. m.table, strings.Join(placeholders, ","))
  146. }
  147. res, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  148. return session.ExecCtx(ctx, updateQuery, updateArgs...)
  149. }, keys...)
  150. if err != nil {
  151. return 0, err
  152. }
  153. rows, _ := res.RowsAffected()
  154. return rows, nil
  155. }