sysUserRoleModel.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package userrole
  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 _ SysUserRoleModel = (*customSysUserRoleModel)(nil)
  12. type (
  13. SysUserRoleModel interface {
  14. sysUserRoleModel
  15. FindRoleIdsByUserId(ctx context.Context, userId int64) ([]int64, error)
  16. FindRoleIdsByUserIdForProduct(ctx context.Context, userId int64, productCode string) ([]int64, error)
  17. // FindRoleIdsByUserIdForProductTx 是 FindRoleIdsByUserIdForProduct 的事务内变体:
  18. // 上游 BindRoles 在 FindOneForUpdateTx(member) 之后,需要在同一事务里读取 existing
  19. // roleIds 再做 diff,避免"existing 读-外部 / diff 写-内部"的窗口产生第三态
  20. // (见审计 M-R10-2)。此处不写库,不需要 FOR UPDATE;member 行锁已经负责并发串行化。
  21. FindRoleIdsByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) ([]int64, error)
  22. FindUserIdsByRoleId(ctx context.Context, roleId int64) ([]int64, error)
  23. FindUserIdsByRoleIdForUpdateTx(ctx context.Context, session sqlx.Session, roleId int64) ([]int64, error)
  24. DeleteByRoleIdTx(ctx context.Context, session sqlx.Session, roleId int64) error
  25. DeleteByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) error
  26. DeleteByUserIdAndRoleIdsTx(ctx context.Context, session sqlx.Session, userId int64, roleIds []int64) error
  27. }
  28. customSysUserRoleModel struct {
  29. *defaultSysUserRoleModel
  30. }
  31. )
  32. func NewSysUserRoleModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysUserRoleModel {
  33. return &customSysUserRoleModel{
  34. defaultSysUserRoleModel: newSysUserRoleModel(conn, c, cachePrefix, opts...),
  35. }
  36. }
  37. // FindRoleIdsByUserId 查询用户关联的所有角色 ID(跨全部产品聚合)。
  38. // 仅在超管未带产品上下文时通过 UserDetail 调用,返回结果不区分产品归属。
  39. func (m *customSysUserRoleModel) FindRoleIdsByUserId(ctx context.Context, userId int64) ([]int64, error) {
  40. var ids []int64
  41. query := fmt.Sprintf("SELECT `roleId` FROM %s WHERE `userId` = ?", m.table)
  42. if err := m.QueryRowsNoCacheCtx(ctx, &ids, query, userId); err != nil {
  43. return nil, err
  44. }
  45. return ids, nil
  46. }
  47. func (m *customSysUserRoleModel) FindRoleIdsByUserIdForProduct(ctx context.Context, userId int64, productCode string) ([]int64, error) {
  48. var ids []int64
  49. // status 走占位参数 + consts.StatusEnabled,避免把"启用"语义钉死成字面量 1;未来新增
  50. // status=3/已归档 之类时只要改 consts 就行,这里不会漏掉(见审计 L-4)。
  51. query := fmt.Sprintf("SELECT ur.`roleId` FROM %s ur INNER JOIN `sys_role` r ON ur.`roleId` = r.`id` WHERE ur.`userId` = ? AND r.`productCode` = ? AND r.`status` = ?", m.table)
  52. if err := m.QueryRowsNoCacheCtx(ctx, &ids, query, userId, productCode, consts.StatusEnabled); err != nil {
  53. return nil, err
  54. }
  55. return ids, nil
  56. }
  57. func (m *customSysUserRoleModel) FindRoleIdsByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) ([]int64, error) {
  58. var ids []int64
  59. query := fmt.Sprintf("SELECT ur.`roleId` FROM %s ur INNER JOIN `sys_role` r ON ur.`roleId` = r.`id` WHERE ur.`userId` = ? AND r.`productCode` = ? AND r.`status` = ?", m.table)
  60. if err := session.QueryRowsCtx(ctx, &ids, query, userId, productCode, consts.StatusEnabled); err != nil {
  61. return nil, err
  62. }
  63. return ids, nil
  64. }
  65. func (m *customSysUserRoleModel) FindUserIdsByRoleId(ctx context.Context, roleId int64) ([]int64, error) {
  66. var ids []int64
  67. query := fmt.Sprintf("SELECT `userId` FROM %s WHERE `roleId` = ?", m.table)
  68. if err := m.QueryRowsNoCacheCtx(ctx, &ids, query, roleId); err != nil {
  69. return nil, err
  70. }
  71. return ids, nil
  72. }
  73. func (m *customSysUserRoleModel) FindUserIdsByRoleIdForUpdateTx(ctx context.Context, session sqlx.Session, roleId int64) ([]int64, error) {
  74. var ids []int64
  75. query := fmt.Sprintf("SELECT `userId` FROM %s WHERE `roleId` = ? FOR UPDATE", m.table)
  76. if err := session.QueryRowsCtx(ctx, &ids, query, roleId); err != nil {
  77. return nil, err
  78. }
  79. return ids, nil
  80. }
  81. // userRoleKey 仅含 buildCacheKeys 真正需要的三列(审计 L-R11-2):以前 Delete 族 SELECT 整行
  82. // 只是为了拿 id / userId / roleId 构造缓存键,却把 createTime/updateTime 等业务字段一并搬运回
  83. // 应用层再丢弃。在"删除一个角色的所有绑定关系"这类关联行 O(关联用户数) 的场景下,SELECT 只读
  84. // 必要列能显著减少 goroutine 临时内存与网络 I/O。
  85. type userRoleKey struct {
  86. Id int64 `db:"id"`
  87. UserId int64 `db:"userId"`
  88. RoleId int64 `db:"roleId"`
  89. }
  90. func (m *customSysUserRoleModel) buildCacheKeysFromKeys(list []userRoleKey) []string {
  91. keys := make([]string, 0, len(list)*2)
  92. for _, data := range list {
  93. keys = append(keys,
  94. fmt.Sprintf("%s%v", cacheSysUserRoleIdPrefix, data.Id),
  95. fmt.Sprintf("%s%v:%v", cacheSysUserRoleUserIdRoleIdPrefix, data.UserId, data.RoleId),
  96. )
  97. }
  98. return keys
  99. }
  100. func (m *customSysUserRoleModel) DeleteByRoleIdTx(ctx context.Context, session sqlx.Session, roleId int64) error {
  101. var list []userRoleKey
  102. findQuery := fmt.Sprintf("SELECT `id`, `userId`, `roleId` FROM %s WHERE `roleId` = ? FOR UPDATE", m.table)
  103. if err := session.QueryRowsCtx(ctx, &list, findQuery, roleId); err != nil {
  104. return err
  105. }
  106. if len(list) == 0 {
  107. return nil
  108. }
  109. keys := m.buildCacheKeysFromKeys(list)
  110. _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  111. query := fmt.Sprintf("DELETE FROM %s WHERE `roleId` = ?", m.table)
  112. return session.ExecCtx(ctx, query, roleId)
  113. }, keys...)
  114. return err
  115. }
  116. func (m *customSysUserRoleModel) DeleteByUserIdForProductTx(ctx context.Context, session sqlx.Session, userId int64, productCode string) error {
  117. var list []userRoleKey
  118. findQuery := fmt.Sprintf("SELECT `id`, `userId`, `roleId` FROM %s WHERE `userId` = ? AND `roleId` IN (SELECT `id` FROM `sys_role` WHERE `productCode` = ?) FOR UPDATE", m.table)
  119. if err := session.QueryRowsCtx(ctx, &list, findQuery, userId, productCode); err != nil {
  120. return err
  121. }
  122. if len(list) == 0 {
  123. return nil
  124. }
  125. keys := m.buildCacheKeysFromKeys(list)
  126. _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  127. query := fmt.Sprintf("DELETE FROM %s WHERE `userId` = ? AND `roleId` IN (SELECT `id` FROM `sys_role` WHERE `productCode` = ?)", m.table)
  128. return session.ExecCtx(ctx, query, userId, productCode)
  129. }, keys...)
  130. return err
  131. }
  132. func (m *customSysUserRoleModel) DeleteByUserIdAndRoleIdsTx(ctx context.Context, session sqlx.Session, userId int64, roleIds []int64) error {
  133. if len(roleIds) == 0 {
  134. return nil
  135. }
  136. placeholders := make([]string, len(roleIds))
  137. args := make([]interface{}, 0, len(roleIds)+1)
  138. args = append(args, userId)
  139. for i, id := range roleIds {
  140. placeholders[i] = "?"
  141. args = append(args, id)
  142. }
  143. inClause := strings.Join(placeholders, ",")
  144. var list []userRoleKey
  145. findQuery := fmt.Sprintf("SELECT `id`, `userId`, `roleId` FROM %s WHERE `userId` = ? AND `roleId` IN (%s) FOR UPDATE", m.table, inClause)
  146. if err := session.QueryRowsCtx(ctx, &list, findQuery, args...); err != nil {
  147. return err
  148. }
  149. if len(list) == 0 {
  150. return nil
  151. }
  152. keys := m.buildCacheKeysFromKeys(list)
  153. _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  154. query := fmt.Sprintf("DELETE FROM %s WHERE `userId` = ? AND `roleId` IN (%s)", m.table, inClause)
  155. return session.ExecCtx(ctx, query, args...)
  156. }, keys...)
  157. return err
  158. }