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)
| 2412 | // authenticated user's accessible workspaces when a user context is present. |
| 2413 | // Returns nil (not an error) if no workspace is found. |
| 2414 | func (s *Server) resolveWorkspace(slugOrID string, user *models.User) (*models.Workspace, error) { |
| 2415 | // 1. Is it a UUID? Try resolving by ID first, then fall back to slug. |
| 2416 | // A workspace slug could be UUID-shaped (e.g. imported data), so we |
| 2417 | // can't short-circuit here. |
| 2418 | if isUUID(slugOrID) { |
| 2419 | ws, err := s.store.GetWorkspaceByID(slugOrID) |
| 2420 | if ws != nil || err != nil { |
| 2421 | return ws, err |
| 2422 | } |
| 2423 | // Not found by ID — fall through to slug-based resolution |
| 2424 | } |
| 2425 | |
| 2426 | // 2. No authenticated user — fall back to global slug lookup |
| 2427 | // (fresh install, or pre-auth paths) |
| 2428 | if user == nil { |
| 2429 | return s.store.GetWorkspaceBySlug(slugOrID) |
| 2430 | } |
| 2431 | |
| 2432 | // 3. Admin users — global slug lookup (admins can see all workspaces) |
| 2433 | if user.Role == "admin" { |
| 2434 | return s.store.GetWorkspaceBySlug(slugOrID) |
| 2435 | } |
| 2436 | |
| 2437 | // 4. Auth-scoped slug resolution: find workspaces where user is owner or member |
| 2438 | workspaces, err := s.store.GetWorkspacesBySlugForUser(slugOrID, user.ID) |
| 2439 | if err != nil { |
| 2440 | return nil, err |
| 2441 | } |
| 2442 | |
| 2443 | if len(workspaces) == 1 { |
| 2444 | return &workspaces[0], nil |
| 2445 | } |
| 2446 | if len(workspaces) == 0 { |
| 2447 | return nil, nil |
| 2448 | } |
| 2449 | |
| 2450 | // Ambiguous: multiple workspaces match — this should be rare. |
| 2451 | // For now, return the first one. The 409 disambiguation is only needed |
| 2452 | // when we actually have per-owner slug uniqueness (after the unique |
| 2453 | // constraint is changed). Currently slugs are globally unique. |
| 2454 | return &workspaces[0], nil |
| 2455 | } |
| 2456 | |
| 2457 | // isUUID is defined in handlers_items.go |
| 2458 |
no test coverage detected