From e3ade3aeb8936fc33cea7fbedc70b6fe4832f4fa Mon Sep 17 00:00:00 2001 From: henrygd Date: Fri, 10 Apr 2026 18:32:37 -0400 Subject: [PATCH] hub: optimize System.HasUser check --- internal/hub/systems/system.go | 13 ++++++++----- internal/hub/systems/systems_test.go | 13 +++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/internal/hub/systems/system.go b/internal/hub/systems/system.go index 947ef257..b9a0103f 100644 --- a/internal/hub/systems/system.go +++ b/internal/hub/systems/system.go @@ -8,7 +8,6 @@ import ( "hash/fnv" "math/rand" "net" - "slices" "strings" "sync/atomic" "time" @@ -363,12 +362,16 @@ func (sys *System) HasUser(app core.App, user *core.Record) bool { if v, _ := utils.GetEnv("SHARE_ALL_SYSTEMS"); v == "true" { return true } - record, err := sys.getRecord(app) - if err != nil { + var recordData = struct { + Users string + }{} + err := app.DB().NewQuery("SELECT users FROM systems WHERE id={:id}"). + Bind(dbx.Params{"id": sys.Id}). + One(&recordData) + if err != nil || recordData.Users == "" { return false } - users := record.GetStringSlice("users") - return slices.Contains(users, user.Id) + return strings.Contains(recordData.Users, user.Id) } // setDown marks a system as down in the database. diff --git a/internal/hub/systems/systems_test.go b/internal/hub/systems/systems_test.go index 1ddb817f..e9632c6c 100644 --- a/internal/hub/systems/systems_test.go +++ b/internal/hub/systems/systems_test.go @@ -436,7 +436,7 @@ func TestHasUser(t *testing.T) { user2, err := tests.CreateUser(hub, "user2@test.com", "password123") require.NoError(t, err) - record, err := tests.CreateRecord(hub, "systems", map[string]any{ + systemRecord, err := tests.CreateRecord(hub, "systems", map[string]any{ "name": "has-user-test", "host": "127.0.0.1", "port": "33914", @@ -444,7 +444,7 @@ func TestHasUser(t *testing.T) { }) require.NoError(t, err) - sys, err := sm.GetSystemFromStore(record.Id) + sys, err := sm.GetSystemFromStore(systemRecord.Id) require.NoError(t, err) t.Run("user in list returns true", func(t *testing.T) { @@ -468,4 +468,13 @@ func TestHasUser(t *testing.T) { t.Setenv("BESZEL_HUB_SHARE_ALL_SYSTEMS", "true") assert.True(t, sys.HasUser(hub, user2)) }) + + t.Run("additional user works", func(t *testing.T) { + assert.False(t, sys.HasUser(hub, user2)) + systemRecord.Set("users", []string{user1.Id, user2.Id}) + err = hub.Save(systemRecord) + require.NoError(t, err) + assert.True(t, sys.HasUser(hub, user1)) + assert.True(t, sys.HasUser(hub, user2)) + }) }