Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: recheck space access while placing owned Caps
Richie McIlroy09b8da2ee06f
diff --git a/apps/web/__tests__/unit/move-owned-videos.test.ts b/apps/web/__tests__/unit/move-owned-videos.test.tsindex 440559814..1aac00a52 100644--- a/apps/web/__tests__/unit/move-owned-videos.test.ts+++ b/apps/web/__tests__/unit/move-owned-videos.test.ts@@ -113,7 +113,7 @@ describe("placing owned Caps in team folders", () => {location: { type: "organization" },}),).resolves.toEqual({ moved: 2 });- expect(locks).toEqual(["update"]);+ expect(locks).toEqual(["update", "update"]);expect(filters[0]?.sql).toContain("`videos`.`ownerId` = ?");expect(filters[0]?.sql).toContain("`videos`.`orgId` = ?");expect(filters[0]?.params).toEqual([...ids, "owner", "org"]);@@ -142,13 +142,23 @@ describe("placing owned Caps in team folders", () => {});it("lets space members place their own Caps without changing other shares", async () => {- results = [[{ id: ids[0] }], [{ id: folderId }], []];+ results = [+ [{ createdById: "creator", memberRole: "member" }],+ [{ id: ids[0] }],+ [{ id: folderId }],+ [],+ ];await placeOwnedVideos({videoIds: [ids[0]],folderId,location: { type: "space", spaceId },});- expect(filters[1]?.params).toEqual([folderId, "org", spaceId]);+ expect(filters[0]).toMatchObject({+ table: "spaces",+ params: [spaceId, "org"],+ });+ expect(locks).toEqual(["update", "update", "update"]);+ expect(filters[2]?.params).toEqual([folderId, "org", spaceId]);expect(writes.map((write) => write.table)).toEqual(["space_videos","space_videos",@@ -158,6 +168,57 @@ describe("placing owned Caps in team folders", () => {]);});+ it.each([+ { name: "the space was deleted", rows: [] },+ {+ name: "space membership was revoked",+ rows: [{ createdById: "creator", memberRole: null }],+ },+ ])(+ "rechecks space access inside the transaction when $name",+ async ({ rows }) => {+ results = [rows];+ await expect(+ placeOwnedVideos({+ videoIds: [ids[0]],+ folderId,+ location: { type: "space", spaceId },+ }),+ ).rejects.toThrow("Space not found");+ expect(mocks.transaction).toHaveBeenCalledOnce();+ expect(filters).toHaveLength(1);+ expect(writes).toEqual([]);+ },+ );++ it.each([+ { role: "admin", createdById: "creator" },+ { role: "member", createdById: "owner" },+ ])(+ "keeps space access for a $role organization role when the space allows it",+ async ({ role, createdById }) => {+ mocks.organization.mockResolvedValue({ role });+ mocks.space.mockResolvedValue({+ organizationId: "org",+ organizationRole: role,+ spaceRole: createdById === "owner" ? "admin" : null,+ canManage: true,+ });+ results = [[{ createdById, memberRole: null }], [{ id: ids[0] }], []];+ await expect(+ placeOwnedVideos({+ videoIds: [ids[0]],+ folderId: null,+ location: { type: "space", spaceId },+ }),+ ).resolves.toEqual({ moved: 1 });+ expect(writes.map((write) => write.table)).toEqual([+ "space_videos",+ "space_videos",+ ]);+ },+ );+it("does not duplicate an existing placement on retry", async () => {results = [[{ id: ids[0] }], [{ videoId: ids[0] }]];await placeOwnedVideos({diff --git a/apps/web/actions/folders/move-items.ts b/apps/web/actions/folders/move-items.tsindex 923df0614..bf940130d 100644--- a/apps/web/actions/folders/move-items.ts+++ b/apps/web/actions/folders/move-items.ts@@ -28,7 +28,10 @@ import {type MoveFolderDestination,type MoveLocation,} from "@/lib/move-items";-import { canManageOrganizationSettings } from "@/lib/permissions/roles";+import {+ canManageOrganizationSettings,+ getEffectiveSpaceRole,+} from "@/lib/permissions/roles";function requireValidLocation(location: MoveLocation) {if (@@ -214,7 +217,10 @@ export async function placeOwnedVideos({if (!user?.activeOrganizationId) throw new Error("Unauthorized");requireValidLocation(location);const ids = normalizeVideoIds(videoIds).sort();- await requireOrganizationAccess(user.id, user.activeOrganizationId);+ const organizationAccess = await requireOrganizationAccess(+ user.id,+ user.activeOrganizationId,+ );if (location.type === "space") {const access = await getSpaceAccess(user.id, location.spaceId);if (@@ -227,6 +233,40 @@ export async function placeOwnedVideos({}}await db().transaction(async (tx) => {+ if (location.type === "space") {+ const [space] = await tx+ .select({+ createdById: spaces.createdById,+ memberRole: spaceMembers.role,+ })+ .from(spaces)+ .leftJoin(+ spaceMembers,+ and(+ eq(spaceMembers.spaceId, spaces.id),+ eq(spaceMembers.userId, user.id),+ ),+ )+ .where(+ and(+ eq(spaces.id, location.spaceId),+ eq(spaces.organizationId, user.activeOrganizationId),+ ),+ )+ .limit(1)+ .for("update");+ if (+ !space ||+ (!canManageOrganizationSettings(organizationAccess.role) &&+ !getEffectiveSpaceRole({+ userId: user.id,+ createdById: space.createdById,+ memberRole: space.memberRole,+ }))+ ) {+ throw new Error("Space not found");+ }+ }const ownedVideos = await tx.select({ id: videos.id }).from(videos)@@ -246,7 +286,8 @@ export async function placeOwnedVideos({.select({ id: folders.id }).from(folders).where(and(eq(folders.id, folderId), getFolderScope(user, location)))- .limit(1);+ .limit(1)+ .for("update");if (!folder) throw new Error("Destination folder not found");}if (location.type === "personal") {