Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: user count should expand the group (#19845)

ecmadaoe4cdd7306f4c
diff --git a/backend/store/group.go b/backend/store/group.go
index 02a8ff6854..38f794dc63 100644
--- a/backend/store/group.go
+++ b/backend/store/group.go
@@ -214,6 +214,7 @@ func (s *Store) CreateGroup(ctx context.Context, create *GroupMessage) (*GroupMe
if create.Email != "" {
s.groupCache.Add(getGroupCacheKey(create.Workspace, create.Email), create)
}
+ s.userCountCache.Remove(create.Workspace)
return create, nil
}
@@ -282,6 +283,7 @@ func (s *Store) UpdateGroup(ctx context.Context, patch *UpdateGroupMessage) (*Gr
s.groupCache.Add(getGroupCacheKey(group.Workspace, group.Email), &group)
s.groupMembersCache.Remove(getGroupMembersCacheKey(group.Workspace, "groups/"+group.Email))
}
+ s.userCountCache.Remove(patch.Workspace)
return &group, nil
}
@@ -302,6 +304,7 @@ func (s *Store) DeleteGroup(ctx context.Context, workspace string, id string) er
s.groupCache.Remove(getGroupCacheKey(workspace, email.String))
s.groupMembersCache.Remove(getGroupMembersCacheKey(workspace, "groups/"+email.String))
}
+ s.userCountCache.Remove(workspace)
return nil
}
diff --git a/backend/store/policy.go b/backend/store/policy.go
index 43c6e387c5..d4a1d4060d 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -102,8 +102,9 @@ func (s *Store) PatchWorkspaceIamPolicy(ctx context.Context, patch *PatchIamPoli
return nil, err
}
- // Invalidate IAM policy cache after mutation.
+ // Invalidate caches after mutation.
s.iamPolicyCache.Remove(getIamPolicyCacheKey(patch.Workspace, storepb.Policy_WORKSPACE, workspaceResource))
+ s.userCountCache.Remove(patch.Workspace)
return s.GetWorkspaceIamPolicy(ctx, patch.Workspace)
}
diff --git a/backend/store/stats.go b/backend/store/stats.go
index 9ba66a0410..94ccf0768e 100644
--- a/backend/store/stats.go
+++ b/backend/store/stats.go
@@ -38,18 +38,45 @@ func (s *Store) CountAllActivePrincipals(ctx context.Context) (int, error) {
// CountActiveEndUsersPerWorkspace counts active end users who are members of the given workspace
// by joining principal with workspace IAM policy bindings.
func (s *Store) CountActiveEndUsersPerWorkspace(ctx context.Context, workspaceID string) (int, error) {
+ if v, ok := s.userCountCache.Get(workspaceID); ok && s.enableCache {
+ return v, nil
+ }
+
+ // Count distinct active users who are members of the workspace, either
+ // directly (users/email) or via group expansion (groups/{email|id} -> group members).
q := qb.Q().Space(`
- SELECT count(DISTINCT principal.id)
+ WITH iam_members AS (
+ SELECT DISTINCT jsonb_array_elements_text(binding->'members') AS member
+ FROM policy,
+ jsonb_array_elements(policy.payload->'bindings') AS binding
+ WHERE policy.workspace = ?
+ AND policy.resource_type = 'WORKSPACE'
+ AND policy.type = 'IAM'
+ ),
+ direct_users AS (
+ SELECT SUBSTRING(member FROM 7) AS email
+ FROM iam_members
+ WHERE member LIKE 'users/%'
+ ),
+ group_users AS (
+ SELECT SUBSTRING(gm.value->>'member' FROM 7) AS email
+ FROM iam_members im
+ JOIN user_group ug ON ug.workspace = ?
+ AND (ug.email = SUBSTRING(im.member FROM 8) OR ug.id = SUBSTRING(im.member FROM 8))
+ , jsonb_array_elements(ug.payload->'members') AS gm
+ WHERE im.member LIKE 'groups/%'
+ AND gm.value->>'member' LIKE 'users/%'
+ ),
+ all_users AS (
+ SELECT email FROM direct_users
+ UNION
+ SELECT email FROM group_users
+ )
+ SELECT count(*)
FROM principal
- JOIN policy ON policy.workspace = ? AND policy.resource_type = 'WORKSPACE' AND policy.type = 'IAM'
WHERE principal.deleted = FALSE
- AND EXISTS (
- SELECT 1
- FROM jsonb_array_elements(policy.payload->'bindings') AS binding,
- jsonb_array_elements_text(binding->'members') AS member
- WHERE member = 'users/' || principal.email
- )
- `, workspaceID)
+ AND principal.email IN (SELECT email FROM all_users)
+ `, workspaceID, workspaceID)
query, args, err := q.ToSQL()
if err != nil {
@@ -60,5 +87,6 @@ func (s *Store) CountActiveEndUsersPerWorkspace(ctx context.Context, workspaceID
if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&count); err != nil {
return 0, err
}
+ s.userCountCache.Add(workspaceID, count)
return count, nil
}
diff --git a/backend/store/store.go b/backend/store/store.go
index 3ea9f38ca4..085176a3a0 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -33,6 +33,7 @@ type Store struct {
memberGroupsCache *expirable.LRU[string, []string]
dbSchemaCache *expirable.LRU[string, *model.DatabaseMetadata]
iamPolicyCache *expirable.LRU[string, *IamPolicyMessage]
+ userCountCache *expirable.LRU[string, int]
// Large objects.
sheetFullCache *lru.Cache[string, *SheetMessage]
@@ -75,6 +76,7 @@ func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {
memberGroupsCache := expirable.NewLRU[string, []string](4096, nil, time.Minute)
dbSchemaCache := expirable.NewLRU[string, *model.DatabaseMetadata](128, nil, 5*time.Minute)
iamPolicyCache := expirable.NewLRU[string, *IamPolicyMessage](1024, nil, time.Minute)
+ userCountCache := expirable.NewLRU[string, int](128, nil, time.Minute)
// Initialize database connection (handles both direct URL and file-based)
dbConnManager := NewDBConnectionManager(pgURL)
@@ -100,6 +102,7 @@ func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {
memberGroupsCache: memberGroupsCache,
dbSchemaCache: dbSchemaCache,
iamPolicyCache: iamPolicyCache,
+ userCountCache: userCountCache,
}
return s, nil
diff --git a/backend/store/workspace.go b/backend/store/workspace.go
index e2cd94a8d3..364ee78037 100644
--- a/backend/store/workspace.go
+++ b/backend/store/workspace.go
@@ -266,7 +266,7 @@ func (s *Store) ListWorkspacesByEmail(ctx context.Context, find *FindWorkspaceMe
FROM user_group ug,
jsonb_array_elements(ug.payload->'members') AS gm
WHERE ug.workspace = w.resource_id
- AND 'groups/' || ug.email = member
+ AND ('groups/' || ug.email = member OR 'groups/' || ug.id = member)
AND gm->>'member' = ?
)`, memberName)
if find.IncludeAllUser {