fix: harden legacy normalization and grants

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 11:53:28 +02:00
1 parent 3cdca5c82c
commit 74ab2eb85f
5 files changed
+198 -20

No files matched your search

+35 -5
View File
@@ -33,13 +33,16 @@ function createRepository(initial: Grant[] = []) {
.map((grant) => grant.albumId);
},
async grant(input) {
const active = grants.find(
const active = grants.filter(
(grant) =>
grant.editorialId === input.editorialId &&
grant.albumId.toLowerCase() === input.immichAlbumId &&
grant.revokedAt === null,
);
if (active) return false;
if (active.length > 0) {
for (const grant of active) grant.albumId = input.immichAlbumId;
return false;
}
const revoked = grants.find(
(grant) =>
@@ -48,6 +51,7 @@ function createRepository(initial: Grant[] = []) {
grant.revokedAt !== null,
);
if (revoked) {
revoked.albumId = input.immichAlbumId;
revoked.revokedAt = null;
} else {
grants.push({ editorialId: input.editorialId, albumId: input.immichAlbumId, revokedAt: null });
@@ -63,14 +67,17 @@ function createRepository(initial: Grant[] = []) {
return true;
},
async revoke(input) {
const active = grants.find(
const active = grants.filter(
(grant) =>
grant.editorialId === input.editorialId &&
grant.albumId.toLowerCase() === input.immichAlbumId &&
grant.revokedAt === null,
);
if (!active) return false;
active.revokedAt = new Date();
if (active.length === 0) return false;
for (const grant of active) {
grant.albumId = input.immichAlbumId;
grant.revokedAt = new Date();
}
audit.push({
eventType: "album_revoked",
actorUserId: input.adminUserId,
@@ -158,6 +165,29 @@ test("treats an uppercase historical album grant as the canonical active grant",
expect(audit).toHaveLength(0);
});
test("revokes and regrants case-duplicate historical rows as one canonical grant", async () => {
const { service, grants, audit } = createService([
{ editorialId: editorialUuid, albumId: albumUuid.toUpperCase(), revokedAt: null },
{ editorialId: editorialUuid, albumId: albumUuid, revokedAt: null },
]);
await service.revokeAlbum({
editorialId: editorialUuid,
immichAlbumId: albumUuid,
adminUserId: adminUuid,
});
expect(grants.filter((grant) => grant.revokedAt === null)).toHaveLength(0);
expect(grants.every((grant) => grant.albumId === albumUuid)).toBe(true);
await service.grantAlbum({
editorialId: editorialUuid,
immichAlbumId: albumUuid,
adminUserId: adminUuid,
});
expect(grants.filter((grant) => grant.revokedAt === null)).toHaveLength(1);
expect(audit.map((event) => event.eventType)).toEqual(["album_revoked", "album_granted"]);
});
test("rejects a non-UUID grant before external side effects", async () => {
const { service, immich, grants } = createService();
+24 -9
View File
@@ -1,6 +1,6 @@
import "server-only";
import { and, eq, isNull, sql } from "drizzle-orm";
import { and, eq, inArray, isNull, sql } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { db } from "@/db/client";
@@ -102,17 +102,23 @@ export function createAlbumGrantRepository(
await lockEditorialAlbumMutation(transaction, input.editorialId);
const editorial = await lockEditorial(transaction, input.editorialId);
const grants = await lockAlbumGrants(transaction, input.editorialId, input.immichAlbumId);
const activeGrant = grants.find((grant) => grant.revokedAt === null);
if (activeGrant) {
const activeGrants = grants.filter((grant) => grant.revokedAt === null);
if (activeGrants.length > 0) {
await transaction
.update(albumGrants)
.set({ immichAlbumId: input.immichAlbumId })
.where(inArray(albumGrants.id, activeGrants.map((grant) => grant.id)));
return false;
}
const changedAt = new Date();
const previousGrant = grants[0];
const previousGrant =
grants.find((grant) => grant.immichAlbumId === input.immichAlbumId) ?? grants[0];
if (previousGrant) {
await transaction
.update(albumGrants)
.set({
immichAlbumId: input.immichAlbumId,
grantedAt: changedAt,
grantedByUserId: input.adminUserId,
revokedAt: null,
@@ -154,16 +160,21 @@ export function createAlbumGrantRepository(
await lockEditorialAlbumMutation(transaction, input.editorialId);
const editorial = await lockEditorial(transaction, input.editorialId);
const grants = await lockAlbumGrants(transaction, input.editorialId, input.immichAlbumId);
const activeGrant = grants.find((grant) => grant.revokedAt === null);
if (!activeGrant) {
const activeGrants = grants.filter((grant) => grant.revokedAt === null);
if (activeGrants.length === 0) {
return false;
}
const changedAt = new Date();
await transaction
.update(albumGrants)
.set({ revokedAt: changedAt })
.where(and(eq(albumGrants.id, activeGrant.id), isNull(albumGrants.revokedAt)));
.set({ immichAlbumId: input.immichAlbumId, revokedAt: changedAt })
.where(
and(
inArray(albumGrants.id, activeGrants.map((grant) => grant.id)),
isNull(albumGrants.revokedAt),
),
);
await transaction.insert(auditEvents).values({
eventType: "album_revoked",
actorUserId: input.adminUserId,
@@ -257,7 +268,11 @@ async function lockAlbumGrants(
immichAlbumId: string,
) {
return transaction
.select({ id: albumGrants.id, revokedAt: albumGrants.revokedAt })
.select({
id: albumGrants.id,
immichAlbumId: albumGrants.immichAlbumId,
revokedAt: albumGrants.revokedAt,
})
.from(albumGrants)
.where(
and(