feat: add editorial album permissions
This commit is contained in:
1 parent
0fe2c80cda
commit
4af86f6eb9
5 files changed
+633
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { expect, test, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
grantAlbum: vi.fn(),
|
||||
headers: vi.fn(),
|
||||
requireAdmin: vi.fn(),
|
||||
revalidatePath: vi.fn(),
|
||||
revokeAlbum: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("next/headers", () => ({ headers: mocks.headers }));
|
||||
vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
|
||||
vi.mock("@/modules/auth/principal", () => ({ requireAdmin: mocks.requireAdmin }));
|
||||
vi.mock("@/modules/albums/grants", () => ({
|
||||
albumGrantService: { grantAlbum: mocks.grantAlbum, revokeAlbum: mocks.revokeAlbum },
|
||||
}));
|
||||
|
||||
import { grantAlbumAction } from "./actions";
|
||||
|
||||
test("the grant action rejects unauthenticated callers before accepting submitted identifiers", async () => {
|
||||
mocks.headers.mockResolvedValue(new Headers());
|
||||
mocks.requireAdmin.mockRejectedValue(new Error("ADMIN_REQUIRED"));
|
||||
const formData = new FormData();
|
||||
formData.set("immichAlbumId", "11111111-1111-4111-8111-111111111111");
|
||||
formData.set("adminUserId", "forged-user");
|
||||
|
||||
await expect(
|
||||
grantAlbumAction("00000000-0000-4000-8000-000000000001", formData),
|
||||
).rejects.toThrow("ADMIN_REQUIRED");
|
||||
expect(mocks.grantAlbum).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("the grant action uses the authenticated principal instead of a submitted admin user ID", async () => {
|
||||
mocks.headers.mockResolvedValue(new Headers());
|
||||
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
|
||||
mocks.grantAlbum.mockResolvedValue(undefined);
|
||||
const formData = new FormData();
|
||||
formData.set("immichAlbumId", "11111111-1111-4111-8111-111111111111");
|
||||
formData.set("adminUserId", "forged-user");
|
||||
|
||||
await grantAlbumAction("00000000-0000-4000-8000-000000000001", formData);
|
||||
|
||||
expect(mocks.grantAlbum).toHaveBeenCalledWith({
|
||||
editorialId: "00000000-0000-4000-8000-000000000001",
|
||||
immichAlbumId: "11111111-1111-4111-8111-111111111111",
|
||||
adminUserId: "admin-1",
|
||||
});
|
||||
expect(mocks.revalidatePath).toHaveBeenCalledWith(
|
||||
"/admin/redaktionen/00000000-0000-4000-8000-000000000001/alben",
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { headers } from "next/headers";
|
||||
|
||||
import { albumGrantService } from "@/modules/albums/grants";
|
||||
import { requireAdmin } from "@/modules/auth/principal";
|
||||
|
||||
export async function grantAlbumAction(editorialId: string, formData: FormData): Promise<void> {
|
||||
const principal = await getAdminPrincipal();
|
||||
const safeEditorialId = requiredId(editorialId);
|
||||
const immichAlbumId = requiredId(formData.get("immichAlbumId"));
|
||||
await albumGrantService.grantAlbum({
|
||||
editorialId: safeEditorialId,
|
||||
immichAlbumId,
|
||||
adminUserId: principal.userId,
|
||||
});
|
||||
revalidatePath(`/admin/redaktionen/${safeEditorialId}/alben`);
|
||||
}
|
||||
|
||||
export async function revokeAlbumAction(editorialId: string, formData: FormData): Promise<void> {
|
||||
const principal = await getAdminPrincipal();
|
||||
const safeEditorialId = requiredId(editorialId);
|
||||
const immichAlbumId = requiredId(formData.get("immichAlbumId"));
|
||||
await albumGrantService.revokeAlbum({
|
||||
editorialId: safeEditorialId,
|
||||
immichAlbumId,
|
||||
adminUserId: principal.userId,
|
||||
});
|
||||
revalidatePath(`/admin/redaktionen/${safeEditorialId}/alben`);
|
||||
}
|
||||
|
||||
async function getAdminPrincipal() {
|
||||
return requireAdmin(new Headers(await headers()));
|
||||
}
|
||||
|
||||
function requiredId(value: unknown): string {
|
||||
if (
|
||||
typeof value !== "string" ||
|
||||
!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
|
||||
) {
|
||||
throw new Error("INVALID_ID");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { notFound } from "next/navigation";
|
||||
|
||||
import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
import { albumGrantService } from "@/modules/albums/grants";
|
||||
|
||||
import { grantAlbumAction, revokeAlbumAction } from "./actions";
|
||||
|
||||
export default async function EditorialAlbumsPage({
|
||||
params,
|
||||
}: Readonly<{ params: Promise<{ id: string }> }>) {
|
||||
const { id } = await params;
|
||||
const [editorial] = await db
|
||||
.select({ id: editorialAccounts.id, name: editorialAccounts.name })
|
||||
.from(editorialAccounts)
|
||||
.where(eq(editorialAccounts.id, id))
|
||||
.limit(1);
|
||||
if (!editorial) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
const albumResult = await albumGrantService
|
||||
.listAssignableAlbums(editorial.id)
|
||||
.then((albums) => ({ albums, unavailable: false }))
|
||||
.catch(() => ({ albums: [], unavailable: true }));
|
||||
|
||||
if (albumResult.unavailable) {
|
||||
return (
|
||||
<section className="content-section" aria-labelledby="albums-title">
|
||||
<p className="eyebrow">Redaktion · Albumrechte</p>
|
||||
<h1 id="albums-title">Alben für {editorial.name}</h1>
|
||||
<p role="status">
|
||||
Die Immich-Alben sind momentan nicht erreichbar. Bereits bestehende Zuweisungen bleiben
|
||||
unverändert.
|
||||
</p>
|
||||
<p>
|
||||
<Link href={`/admin/redaktionen/${editorial.id}`}>Zurück zur Redaktion</Link>
|
||||
</p>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="content-section" aria-labelledby="albums-title">
|
||||
<p className="eyebrow">Redaktion · Albumrechte</p>
|
||||
<h1 id="albums-title">Alben für {editorial.name}</h1>
|
||||
<p>
|
||||
Nur Alben, die der technische Immich-Zugang lesen kann, lassen sich zuweisen.
|
||||
</p>
|
||||
<p>
|
||||
<Link href={`/admin/redaktionen/${editorial.id}`}>Zurück zur Redaktion</Link>
|
||||
</p>
|
||||
|
||||
{albumResult.albums.length === 0 ? (
|
||||
<p>Derzeit sind keine zuweisbaren Alben vorhanden.</p>
|
||||
) : (
|
||||
<ul aria-label="Zuweisbare Immich-Alben">
|
||||
{albumResult.albums.map((album) => {
|
||||
const action = album.isGranted ? revokeAlbumAction : grantAlbumAction;
|
||||
return (
|
||||
<li key={album.id}>
|
||||
<article aria-labelledby={`album-${album.id}`}>
|
||||
{album.albumThumbnailAssetId ? (
|
||||
<Image
|
||||
alt={`Titelbild von ${album.albumName}`}
|
||||
height={120}
|
||||
src={`/api/assets/${encodeURIComponent(album.albumThumbnailAssetId)}/thumbnail`}
|
||||
width={160}
|
||||
/>
|
||||
) : (
|
||||
<div aria-label="Kein Albumcover verfügbar" role="img">
|
||||
Kein Cover
|
||||
</div>
|
||||
)}
|
||||
<h2 id={`album-${album.id}`}>{album.albumName}</h2>
|
||||
<p>{album.assetCount} Fotos</p>
|
||||
<p>
|
||||
Status: <strong>{album.isGranted ? "Zugewiesen" : "Nicht zugewiesen"}</strong>
|
||||
</p>
|
||||
<form action={action.bind(null, editorial.id)}>
|
||||
<input name="immichAlbumId" type="hidden" value={album.id} />
|
||||
<button type="submit">
|
||||
{album.isGranted ? "Zuweisung aufheben" : "Album zuweisen"}
|
||||
</button>
|
||||
</form>
|
||||
</article>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user