fix: run user group count inside a transaction

This commit is contained in:
Elias Schneider
2025-05-25 22:24:20 +02:00
parent 38d7ee4432
commit f03b80f9d7

View File

@@ -1175,13 +1175,19 @@ func (s *OidcService) GetDeviceCodeInfo(ctx context.Context, userCode string, us
}
func (s *OidcService) GetAllowedGroupsCountOfClient(ctx context.Context, id string) (int64, error) {
// We only perform select queries here, so we can rollback in all cases
tx := s.db.Begin()
defer func() {
tx.Rollback()
}()
var client model.OidcClient
err := s.db.WithContext(ctx).Where("id = ?", id).First(&client).Error
err := tx.WithContext(ctx).Where("id = ?", id).First(&client).Error
if err != nil {
return 0, err
}
count := s.db.WithContext(ctx).Model(&client).Association("AllowedUserGroups").Count()
count := tx.WithContext(ctx).Model(&client).Association("AllowedUserGroups").Count()
return count, nil
}