sysRoleModel.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package role
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "perms-system-server/internal/consts"
  9. "github.com/zeromicro/go-zero/core/stores/cache"
  10. "github.com/zeromicro/go-zero/core/stores/sqlx"
  11. )
  12. var ErrUpdateConflict = errors.New("update conflict: data has been modified by another operation")
  13. var _ SysRoleModel = (*customSysRoleModel)(nil)
  14. type (
  15. SysRoleModel interface {
  16. sysRoleModel
  17. FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysRole, int64, error)
  18. FindByIds(ctx context.Context, ids []int64) ([]*SysRole, error)
  19. FindMinPermsLevelByUserIdAndProductCode(ctx context.Context, userId int64, productCode string) (int64, error)
  20. UpdateWithOptLock(ctx context.Context, data *SysRole, expectedUpdateTime int64) error
  21. // LockByIdTx 在当前事务里锁住 sys_role 行(SELECT ... FOR UPDATE),用于把"同一 role 的
  22. // BindRolePerms 并发覆盖"串行化,消除"existing 在事务外读 + 事务内 delete/insert"
  23. // 造成的第三态合并问题(见审计 M-R10-2)。
  24. LockByIdTx(ctx context.Context, session sqlx.Session, id int64) (*SysRole, error)
  25. }
  26. customSysRoleModel struct {
  27. *defaultSysRoleModel
  28. }
  29. )
  30. func NewSysRoleModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysRoleModel {
  31. return &customSysRoleModel{
  32. defaultSysRoleModel: newSysRoleModel(conn, c, cachePrefix, opts...),
  33. }
  34. }
  35. func (m *customSysRoleModel) FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysRole, int64, error) {
  36. var total int64
  37. countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE `productCode` = ?", m.table)
  38. if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, productCode); err != nil {
  39. return nil, 0, err
  40. }
  41. var list []*SysRole
  42. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? ORDER BY `permsLevel` ASC, id DESC LIMIT ?,?", sysRoleRows, m.table)
  43. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode, (page-1)*pageSize, pageSize); err != nil {
  44. return nil, 0, err
  45. }
  46. return list, total, nil
  47. }
  48. func (m *customSysRoleModel) FindByIds(ctx context.Context, ids []int64) ([]*SysRole, error) {
  49. if len(ids) == 0 {
  50. return nil, nil
  51. }
  52. args := make([]interface{}, len(ids))
  53. marks := make([]string, len(ids))
  54. for i, id := range ids {
  55. args[i] = id
  56. marks[i] = "?"
  57. }
  58. var list []*SysRole
  59. query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` IN (%s)", sysRoleRows, m.table, strings.Join(marks, ","))
  60. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
  61. return nil, err
  62. }
  63. return list, nil
  64. }
  65. func (m *customSysRoleModel) UpdateWithOptLock(ctx context.Context, data *SysRole, expectedUpdateTime int64) error {
  66. sysRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleIdPrefix, data.Id)
  67. sysRoleProductCodeNameKey := fmt.Sprintf("%s%v:%v", cacheSysRoleProductCodeNamePrefix, data.ProductCode, data.Name)
  68. res, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  69. query := fmt.Sprintf("UPDATE %s SET `name`=?, `remark`=?, `status`=?, `permsLevel`=?, `updateTime`=? WHERE `id`=? AND `updateTime`=?", m.table)
  70. return conn.ExecCtx(ctx, query, data.Name, data.Remark, data.Status, data.PermsLevel, data.UpdateTime, data.Id, expectedUpdateTime)
  71. }, sysRoleIdKey, sysRoleProductCodeNameKey)
  72. if err != nil {
  73. return err
  74. }
  75. affected, _ := res.RowsAffected()
  76. if affected == 0 {
  77. return ErrUpdateConflict
  78. }
  79. return nil
  80. }
  81. // LockByIdTx 见接口注释。注意:本函数不走缓存层,必须在 TransactCtx / Session 下调用;
  82. // SELECT ... FOR UPDATE 的行锁由 InnoDB 持有到事务结束。
  83. func (m *customSysRoleModel) LockByIdTx(ctx context.Context, session sqlx.Session, id int64) (*SysRole, error) {
  84. var data SysRole
  85. query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1 FOR UPDATE", sysRoleRows, m.table)
  86. if err := session.QueryRowCtx(ctx, &data, query, id); err != nil {
  87. return nil, err
  88. }
  89. return &data, nil
  90. }
  91. func (m *customSysRoleModel) FindMinPermsLevelByUserIdAndProductCode(ctx context.Context, userId int64, productCode string) (int64, error) {
  92. var level int64
  93. query := fmt.Sprintf(
  94. "SELECT IFNULL(MIN(r.`permsLevel`), -1) FROM %s r INNER JOIN `sys_user_role` ur ON r.`id` = ur.`roleId` WHERE ur.`userId` = ? AND r.`productCode` = ? AND r.`status` = ?",
  95. m.table,
  96. )
  97. if err := m.QueryRowNoCacheCtx(ctx, &level, query, userId, productCode, consts.StatusEnabled); err != nil {
  98. return 0, err
  99. }
  100. if level < 0 {
  101. return 0, ErrNotFound
  102. }
  103. return level, nil
  104. }