* fix(web): show admin chrome only on the admin account's primary profile The top-right ServerActivity indicator and the sidebar Admin section were gated on the account-level role alone, so every profile on an admin account — including child profiles — saw admin system notifications and the indicator polled four admin endpoints on their behalf. Gate both on the active profile being the household primary, matching the existing is_primary idiom in SettingsLayout and the server-side quota exemption. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(web): resolve active profile via useCurrentProfile in admin route gates RequireAdmin/RequirePrimaryOrAdmin read the profile from useAuth(), but the admin chrome (AppSidebar, Layout) gates on useCurrentProfile(), which resolves the selected profile. Use the same source in the route gates so the redirect and the visible admin UI can never disagree. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(web,api): centralize acting-admin policy and enforce it server-side Address code-review findings on the primary-profile admin gate: - Add isActingAdmin to web/src/lib/permissions.ts as the single client-side definition of the policy (admin role + primary or no profile), with a useIsActingAdmin hook on top. Route gates, sidebar, Layout, and realtime channel gating all use it now, so the gate and the chrome can no longer disagree on null-profile handling. - Convert the admin-gated surfaces the original change missed (MediaItemMenu, EditMetadataDialog images tab, AddToCollectionDialog, MarkerEditor, theme CatalogBrowser, PersonDetail, SettingsLayout, ItemDetail content pages) so an admin on a non-primary profile is a regular viewer everywhere, not just in the sidebar. - Make the role-derived permission bypass (metadata curation, marker edit) follow the same policy on both client and server. - Enforce the policy server-side: RequireActingAdmin middleware refuses admin routes when the request declares a non-primary profile via X-Profile-Id, and the metadata-curation middleware holds admins on non-primary profiles to explicitly assigned permissions. - Stop spreading the profiles query result from useCurrentProfile so route gates only re-render when the resolved profile changes, and make it safe outside AuthProvider. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(web,api): fail closed on unresolved profiles in acting-admin policy Address review feedback on the acting-admin gate: - Server: actingAdminAllowed now denies when the declared profile cannot be resolved to one of the caller's profiles, so a bogus X-Profile-Id can no longer restore admin powers to a non-primary session. - Client: useIsActingAdmin returns false while a selected profile id has not yet resolved (e.g. hard refresh before the profiles query returns), instead of briefly treating it as "no profile selected". useCurrentProfile exposes hasSelectedProfile to make that state distinguishable. - hasPermission/canCurateMetadata/canEditMarkers now require the profile argument (resolved profile or explicit null), so a missed call site fails the typecheck instead of silently restoring the admin bypass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Code <noreply@anthropic.com> Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com>
148 lines
5.2 KiB
Go
148 lines
5.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/auth"
|
|
"github.com/Silo-Server/silo-server/internal/models"
|
|
)
|
|
|
|
type fakePermissionUserLoader struct {
|
|
user *models.User
|
|
err error
|
|
}
|
|
|
|
func (f fakePermissionUserLoader) GetByID(context.Context, int) (*models.User, error) {
|
|
return f.user, f.err
|
|
}
|
|
|
|
type fakeTargetLibraryResolver struct {
|
|
ids []int
|
|
err error
|
|
}
|
|
|
|
func (f fakeTargetLibraryResolver) ResolveMetadataTargetLibraryIDs(context.Context, string) ([]int, error) {
|
|
return f.ids, f.err
|
|
}
|
|
|
|
func requestWithItemID(role string) *http.Request {
|
|
req := httptest.NewRequest(http.MethodPost, "/admin/items/item-1/refresh-metadata", nil)
|
|
ctx := SetClaims(req.Context(), &auth.Claims{UserID: 7, Role: role, TokenType: auth.TokenTypeAccess})
|
|
routeCtx := chi.NewRouteContext()
|
|
routeCtx.URLParams.Add("id", "item-1")
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, routeCtx)
|
|
return req.WithContext(ctx)
|
|
}
|
|
|
|
func runMetadataCurationMiddleware(user *models.User, libraryIDs []int, role string) int {
|
|
mw := NewPermissionMiddleware(
|
|
fakePermissionUserLoader{user: user},
|
|
fakeTargetLibraryResolver{ids: libraryIDs},
|
|
nil,
|
|
)
|
|
next := mw.RequireMetadataCurationForItem(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
rec := httptest.NewRecorder()
|
|
next.ServeHTTP(rec, requestWithItemID(role))
|
|
return rec.Code
|
|
}
|
|
|
|
func runMetadataCurationMiddlewareWithProfile(
|
|
user *models.User,
|
|
libraryIDs []int,
|
|
role, profileID string,
|
|
check PrimaryProfileChecker,
|
|
) int {
|
|
mw := NewPermissionMiddleware(
|
|
fakePermissionUserLoader{user: user},
|
|
fakeTargetLibraryResolver{ids: libraryIDs},
|
|
check,
|
|
)
|
|
next := mw.RequireMetadataCurationForItem(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
req := requestWithItemID(role)
|
|
if profileID != "" {
|
|
req.Header.Set("X-Profile-Id", profileID)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
next.ServeHTTP(rec, req)
|
|
return rec.Code
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_AdminOnPrimaryProfileBypasses(t *testing.T) {
|
|
code := runMetadataCurationMiddlewareWithProfile(nil, nil, "admin", "prof-1", primaryChecker(true, true, nil))
|
|
if code != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_AdminOnNonPrimaryProfileWithoutAssignedPermission(t *testing.T) {
|
|
admin := &models.User{ID: 7, Role: "admin", Enabled: true, LibraryIDs: nil, Permissions: nil}
|
|
code := runMetadataCurationMiddlewareWithProfile(admin, []int{1}, "admin", "prof-2", primaryChecker(false, true, nil))
|
|
if code != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusForbidden)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_AdminOnNonPrimaryProfileWithAssignedPermission(t *testing.T) {
|
|
admin := &models.User{ID: 7, Role: "admin", Enabled: true, LibraryIDs: nil, Permissions: []string{"metadata_curation"}}
|
|
code := runMetadataCurationMiddlewareWithProfile(admin, []int{1}, "admin", "prof-2", primaryChecker(false, true, nil))
|
|
if code != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_AllowsAdmin(t *testing.T) {
|
|
code := runMetadataCurationMiddleware(nil, nil, "admin")
|
|
if code != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_RejectsUserWithoutPermission(t *testing.T) {
|
|
user := &models.User{ID: 7, Role: "user", Enabled: true, LibraryIDs: []int{1}, Permissions: nil}
|
|
code := runMetadataCurationMiddleware(user, []int{1}, "user")
|
|
if code != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusForbidden)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_AllowsUnrestrictedCurator(t *testing.T) {
|
|
user := &models.User{ID: 7, Role: "user", Enabled: true, LibraryIDs: nil, Permissions: []string{"metadata_curation"}}
|
|
code := runMetadataCurationMiddleware(user, []int{1, 2}, "user")
|
|
if code != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_AllowsWhenAllTargetLibrariesAreAllowed(t *testing.T) {
|
|
user := &models.User{ID: 7, Role: "user", Enabled: true, LibraryIDs: []int{1, 2, 3}, Permissions: []string{"metadata_curation"}}
|
|
code := runMetadataCurationMiddleware(user, []int{1, 3}, "user")
|
|
if code != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_RejectsWhenAnyTargetLibraryIsOutsideAccess(t *testing.T) {
|
|
user := &models.User{ID: 7, Role: "user", Enabled: true, LibraryIDs: []int{1}, Permissions: []string{"metadata_curation"}}
|
|
code := runMetadataCurationMiddleware(user, []int{1, 2}, "user")
|
|
if code != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusForbidden)
|
|
}
|
|
}
|
|
|
|
func TestRequireMetadataCurationForItem_NotFoundWhenTargetHasNoLibraries(t *testing.T) {
|
|
user := &models.User{ID: 7, Role: "user", Enabled: true, LibraryIDs: nil, Permissions: []string{"metadata_curation"}}
|
|
code := runMetadataCurationMiddleware(user, nil, "user")
|
|
if code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d", code, http.StatusNotFound)
|
|
}
|
|
}
|