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)
| 2790 | // authenticated user's accessible workspaces when a user context is present. |
| 2791 | // Returns nil (not an error) if no workspace is found. |
| 2792 | func (s *Server) resolveWorkspace(slugOrID string, user *models.User) (*models.Workspace, error) { |
| 2793 | // 1. Is it a UUID? Try resolving by ID first, then fall back to slug. |
| 2794 | // A workspace slug could be UUID-shaped (e.g. imported data), so we |
| 2795 | // can't short-circuit here. |
| 2796 | if isUUID(slugOrID) { |
| 2797 | ws, err := s.store.GetWorkspaceByID(slugOrID) |
| 2798 | if ws != nil || err != nil { |
| 2799 | return ws, err |
| 2800 | } |
| 2801 | // Not found by ID — fall through to slug-based resolution |
| 2802 | } |
| 2803 | |
| 2804 | // 2. No authenticated user — fall back to global slug lookup |
| 2805 | // (fresh install, or pre-auth paths) |
| 2806 | if user == nil { |
| 2807 | return s.store.GetWorkspaceBySlug(slugOrID) |
| 2808 | } |
| 2809 | |
| 2810 | // 3. Admin users — global slug lookup (admins can see all workspaces) |
| 2811 | if user.Role == "admin" { |
| 2812 | return s.store.GetWorkspaceBySlug(slugOrID) |
| 2813 | } |
| 2814 | |
| 2815 | // 4. Auth-scoped slug resolution: find workspaces where user is owner or member |
| 2816 | workspaces, err := s.store.GetWorkspacesBySlugForUser(slugOrID, user.ID) |
| 2817 | if err != nil { |
| 2818 | return nil, err |
| 2819 | } |
| 2820 | |
| 2821 | if len(workspaces) == 1 { |
| 2822 | return &workspaces[0], nil |
| 2823 | } |
| 2824 | if len(workspaces) == 0 { |
| 2825 | return nil, nil |
| 2826 | } |
| 2827 | |
| 2828 | // Ambiguous: multiple workspaces match — this should be rare. |
| 2829 | // For now, return the first one. The 409 disambiguation is only needed |
| 2830 | // when we actually have per-owner slug uniqueness (after the unique |
| 2831 | // constraint is changed). Currently slugs are globally unique. |
| 2832 | return &workspaces[0], nil |
| 2833 | } |
| 2834 | |
| 2835 | // isUUID is defined in handlers_items.go |
| 2836 |
no test coverage detected