resolveWorkspace resolves a workspace by slug or UUID, scoped to the authenticated user's accessible workspaces when a user context is present. Returns nil (not an error) if no workspace is found.
(slugOrID string, user *models.User)
| 2372 | // authenticated user's accessible workspaces when a user context is present. |
| 2373 | // Returns nil (not an error) if no workspace is found. |
| 2374 | func (s *Server) resolveWorkspace(slugOrID string, user *models.User) (*models.Workspace, error) { |
| 2375 | // 1. Is it a UUID? Try resolving by ID first, then fall back to slug. |
| 2376 | // A workspace slug could be UUID-shaped (e.g. imported data), so we |
| 2377 | // can't short-circuit here. |
| 2378 | if isUUID(slugOrID) { |
| 2379 | ws, err := s.store.GetWorkspaceByID(slugOrID) |
| 2380 | if ws != nil || err != nil { |
| 2381 | return ws, err |
| 2382 | } |
| 2383 | // Not found by ID — fall through to slug-based resolution |
| 2384 | } |
| 2385 | |
| 2386 | // 2. No authenticated user — fall back to global slug lookup |
| 2387 | // (fresh install, or pre-auth paths) |
| 2388 | if user == nil { |
| 2389 | return s.store.GetWorkspaceBySlug(slugOrID) |
| 2390 | } |
| 2391 | |
| 2392 | // 3. Admin users — global slug lookup (admins can see all workspaces) |
| 2393 | if user.Role == "admin" { |
| 2394 | return s.store.GetWorkspaceBySlug(slugOrID) |
| 2395 | } |
| 2396 | |
| 2397 | // 4. Auth-scoped slug resolution: find workspaces where user is owner or member |
| 2398 | workspaces, err := s.store.GetWorkspacesBySlugForUser(slugOrID, user.ID) |
| 2399 | if err != nil { |
| 2400 | return nil, err |
| 2401 | } |
| 2402 | |
| 2403 | if len(workspaces) == 1 { |
| 2404 | return &workspaces[0], nil |
| 2405 | } |
| 2406 | if len(workspaces) == 0 { |
| 2407 | return nil, nil |
| 2408 | } |
| 2409 | |
| 2410 | // Ambiguous: multiple workspaces match — this should be rare. |
| 2411 | // For now, return the first one. The 409 disambiguation is only needed |
| 2412 | // when we actually have per-owner slug uniqueness (after the unique |
| 2413 | // constraint is changed). Currently slugs are globally unique. |
| 2414 | return &workspaces[0], nil |
| 2415 | } |
| 2416 | |
| 2417 | // isUUID is defined in handlers_items.go |
| 2418 |
no test coverage detected