fix: 区服报表复用批量游戏权限校验

This commit is contained in:
Yvan
2026-11-05 09:26:16 +08:00
parent 26a8030e8d
commit ee20692e95
3 changed files with 39 additions and 13 deletions
+3 -12
View File
@@ -291,18 +291,9 @@ func filterAnalyticsGames(db *gorm.DB, scope business.AnalyticsScope, f business
for key := range rows {
keys = append(keys, key)
}
allowed := map[business.AnalyticsKey]bool{}
for start := 0; start < len(keys); start += 300 {
var games []analyticsAggPO
q := approvedGamesQuery(db, scope.Viewer, f.Sources).Where("(g.source_db,g.id) IN ?", analyticsPairs(keys[start:min(start+300, len(keys))]))
if err := q.Select("g.source_db,g.id AS entity_id").Scan(&games).Error; err != nil {
return business.ErrStorage
}
for _, game := range games {
if game.ID != nil {
allowed[business.AnalyticsKey{SourceDB: game.SourceDB, ID: *game.ID}] = true
}
}
allowed, err := approvedGameKeys(db, scope.Viewer, f.Sources, keys)
if err != nil {
return err
}
for key := range rows {
if !allowed[key] {
+19
View File
@@ -29,6 +29,25 @@ func approvedGamesQuery(db *gorm.DB, viewer business.UserScope, sources []string
return q
}
// Check authorization for the distinct games present in an aggregated report.
func approvedGameKeys(db *gorm.DB, viewer business.UserScope, sources []string, keys []business.AnalyticsKey) (map[business.AnalyticsKey]bool, error) {
allowed := map[business.AnalyticsKey]bool{}
for start := 0; start < len(keys); start += 300 {
var games []struct {
SourceDB string `gorm:"column:source_db"`
ID int64
}
q := approvedGamesQuery(db, viewer, sources).Where("(g.source_db,g.id) IN ?", analyticsPairs(keys[start:min(start+300, len(keys))]))
if err := q.Select("g.source_db,g.id").Scan(&games).Error; err != nil {
return nil, business.ErrStorage
}
for _, game := range games {
allowed[business.AnalyticsKey{SourceDB: game.SourceDB, ID: game.ID}] = true
}
}
return allowed, nil
}
func migrateApprovedGameIndexes(db *gorm.DB) error {
return migrateQueryIndexes(db, []businessQueryIndex{
{"tab_promote_apply", "idx_sw_apply_game_scope", []string{"game_id", "source_db", "status", "promote_id"}},
+17 -1
View File
@@ -17,7 +17,6 @@ func NewServerAnalysisRepo(data Provider) business.ServerAnalysisRepo {
func serverReportQuery(db *gorm.DB, scope business.AnalyticsScope, f business.ServerAnalysisFilter, table string, mode int) *gorm.DB {
q := ledgerScopeQuery(db, business.LedgerScope{Viewer: scope.Viewer, IncludeSub: scope.IncludeSub, Unrestricted: scope.Viewer.All}, business.LedgerFilter{Sources: f.Sources}, table)
q = q.Where("r.stat_date >= ? AND r.stat_date <= ? AND r.dedup_mode=? AND r.server_num != ''", f.Begin, f.End, mode).Where("r.game_name IN ?", f.GameNames)
q = q.Where("(r.source_db,r.game_id) IN (?)", approvedGamesQuery(db, scope.Viewer, f.Sources).Select("g.source_db,g.id"))
if f.ServerNum != "" {
q = q.Where("r.server_num=?", f.ServerNum)
}
@@ -88,6 +87,23 @@ func (r *serverAnalysisRepo) Aggregate(ctx context.Context, scope business.Analy
row.Pay = po.Amount
row.GameName = strings.TrimSpace(po.GameName)
}
gameSet := map[business.AnalyticsKey]bool{}
for key := range rows {
gameSet[business.AnalyticsKey{SourceDB: key.SourceDB, ID: key.GameID}] = true
}
gameKeys := make([]business.AnalyticsKey, 0, len(gameSet))
for key := range gameSet {
gameKeys = append(gameKeys, key)
}
allowed, err := approvedGameKeys(db, scope.Viewer, f.Sources, gameKeys)
if err != nil {
return nil, err
}
for key := range rows {
if !allowed[business.AnalyticsKey{SourceDB: key.SourceDB, ID: key.GameID}] {
delete(rows, key)
}
}
if err := fillServerAnalysisNames(db, rows); err != nil {
return nil, err
}