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