Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: defaultGroup not always validated for access

Matthew Kilgoref6ccaf93eda2
diff --git a/backend/app/api/middleware.go b/backend/app/api/middleware.go
index 9dd27f91..2529fa01 100644
--- a/backend/app/api/middleware.go
+++ b/backend/app/api/middleware.go
@@ -188,20 +188,20 @@ func (a *app) mwTenant(next errchain.Handler) errchain.Handler {
return validate.NewRequestError(errors.New("invalid X-Tenant header format"), http.StatusBadRequest)
}
- // Validate user has access to the requested tenant
- hasAccess := false
- for _, gid := range user.GroupIDs {
- if gid == parsedTenantID {
- hasAccess = true
- break
- }
- }
+ tenantID = parsedTenantID
+ }
- if !hasAccess {
- return validate.NewRequestError(errors.New("user does not have access to the requested tenant"), http.StatusForbidden)
+ // Validate user has access to the resolved tenant (whether from header or default group)
+ hasAccess := false
+ for _, gid := range user.GroupIDs {
+ if gid == tenantID {
+ hasAccess = true
+ break
}
+ }
- tenantID = parsedTenantID
+ if !hasAccess {
+ return validate.NewRequestError(errors.New("user does not have access to the requested tenant"), http.StatusForbidden)
}
// Set the tenant in context
diff --git a/backend/internal/core/services/service_group.go b/backend/internal/core/services/service_group.go
index 74572cce..eaac5afa 100644
--- a/backend/internal/core/services/service_group.go
+++ b/backend/internal/core/services/service_group.go
@@ -69,7 +69,33 @@ func (svc *GroupService) RemoveMember(ctx Context, userID uuid.UUID) error {
return errors.New("user ID cannot be empty")
}
- return svc.repos.Groups.RemoveMember(ctx.Context, ctx.GID, userID)
+ err := svc.repos.Groups.RemoveMember(ctx.Context, ctx.GID, userID)
+ if err != nil {
+ return err
+ }
+
+ // If the removed group was the user's default group, reassign to another group
+ removedUser, err := svc.repos.Users.GetOneID(ctx.Context, userID)
+ if err != nil {
+ return err
+ }
+
+ if removedUser.DefaultGroupID == ctx.GID {
+ // Find another group the user is still a member of
+ var newDefaultGroupID uuid.UUID
+ for _, gid := range removedUser.GroupIDs {
+ if gid != ctx.GID {
+ newDefaultGroupID = gid
+ break
+ }
+ }
+ // Update to another group, or uuid.Nil if the user has no remaining groups
+ if err := svc.repos.Users.UpdateDefaultGroup(ctx.Context, userID, newDefaultGroupID); err != nil {
+ return err
+ }
+ }
+
+ return nil
}
func (svc *GroupService) DeleteInvitation(ctx Context, id uuid.UUID) error {