Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

8.6 KB
Raw Blame
import { isEmailAllowedByRestriction } from "@cap/utils";
import {
	CurrentUser,
	type DatabaseError,
	type Organisation,
	Policy,
	type User,
	Video,
} from "@cap/web-domain";
import { Array, Effect, Option } from "effect";

import { Database } from "../Database.ts";
import { OrganisationsRepo } from "../Organisations/OrganisationsRepo.ts";
import { SpacesRepo } from "../Spaces/SpacesRepo.ts";
import { collectPasswordHashes } from "./EffectiveVideoRules.ts";
import { VideosRepo } from "./VideosRepo.ts";

export type LoadedVideo = readonly [Video.Video, Option.Option<string>];

export type ViewableVideo = Pick<
	Video.Video,
	"id" | "ownerId" | "orgId" | "public"
>;

export type VideosPolicyDeps = {
	repo: {
		getById: (
			id: Video.VideoId,
		) => Effect.Effect<Option.Option<LoadedVideo>, DatabaseError>;
		hasViewerGrant: (
			id: Video.VideoId,
			email: string,
		) => Effect.Effect<boolean, DatabaseError>;
	};
	orgsRepo: {
		membershipForVideo: (
			userId: User.UserId,
			videoId: Video.VideoId,
		) => Effect.Effect<readonly { membershipId: string }[], DatabaseError>;
		allowedEmailDomain: (
			orgId: Organisation.OrganisationId,
		) => Effect.Effect<Option.Option<string>, DatabaseError>;
	};
	spacesRepo: {
		membershipForVideo: (
			userId: User.UserId,
			videoId: Video.VideoId,
		) => Effect.Effect<Option.Option<{ membershipId: string }>, DatabaseError>;
		passwordsForVideo: (
			videoId: Video.VideoId,
		) => Effect.Effect<readonly { password: string | null }[], DatabaseError>;
	};
};

export type ViewDecisionDeps = Pick<
	VideosPolicyDeps,
	"repo" | "orgsRepo" | "spacesRepo"
>;

const decideCanView = (
	{ repo, orgsRepo, spacesRepo }: ViewDecisionDeps,
	user: Option.Option<CurrentUser["Type"]>,
	video: ViewableVideo,
	password: Option.Option<string>,
) =>
	Effect.gen(function* () {
		if (Option.isSome(user)) {
			const userId = user.value.id;
			if (userId === video.ownerId) return true;
		}

		const spacePasswords = yield* spacesRepo.passwordsForVideo(video.id);
		const passwordHashes = collectPasswordHashes({
			videoPassword: Option.getOrNull(password),
			spacePasswords: [...spacePasswords],
		});

		if (Option.isSome(user)) {
			const userId = user.value.id;
			const [videoOrgShareMembership, videoSpaceShareMembership] =
				yield* Effect.all(
					[
						orgsRepo
							.membershipForVideo(userId, video.id)
							.pipe(Effect.map(Array.get(0))),
						spacesRepo.membershipForVideo(userId, video.id),
					],
					{ concurrency: "unbounded" },
				);

			if (
				Option.isSome(videoOrgShareMembership) ||
				Option.isSome(videoSpaceShareMembership)
			) {
				yield* Effect.log(
					"Explicit org/space membership found. Access granted.",
				);
				yield* Video.verifyPasswordCandidates(video, passwordHashes);
				return true;
			}
		}

		if (!video.public) {
			if (
				Option.isNone(user) ||
				!(yield* repo.hasViewerGrant(video.id, user.value.email))
			) {
				yield* Effect.log(
					"Video is private and user has no explicit access. Access denied.",
				);
				return false;
			}
			yield* Video.verifyPasswordCandidates(video, passwordHashes);
			return true;
		}

		const allowedEmails = yield* orgsRepo.allowedEmailDomain(video.orgId);
		const restriction = Option.isSome(allowedEmails)
			? allowedEmails.value.trim()
			: "";

		if (restriction.length > 0) {
			if (Option.isNone(user)) {
				yield* Effect.log(
					"Email access restriction active and user not logged in. Access denied.",
				);
				return yield* Effect.fail(
					new Policy.PolicyDeniedError({
						reason: "email_restriction_login_required",
					}),
				);
			}
			if (
				Option.isSome(user) &&
				!isEmailAllowedByRestriction(user.value.email, restriction) &&
				!(yield* repo.hasViewerGrant(video.id, user.value.email))
			) {
				yield* Effect.log("Email access restriction active. Access denied.");
				return yield* Effect.fail(
					new Policy.PolicyDeniedError({
						reason: "email_restriction_denied",
					}),
				);
			}
		}

		yield* Video.verifyPasswordCandidates(video, passwordHashes);

		return true;
	});

export function buildCanView(deps: VideosPolicyDeps, videoId: Video.VideoId) {
	return Policy.publicPolicy(
		Effect.fn(function* (user) {
			const res = yield* deps.repo.getById(videoId);

			if (Option.isNone(res)) {
				yield* Effect.log("Video not found. Access granted.");
				return true;
			}

			const [video, password] = res.value;
			return yield* decideCanView(deps, user, video, password);
		}),
	);
}

export function buildCanViewLoaded(
	deps: ViewDecisionDeps,
	video: ViewableVideo,
	password: Option.Option<string>,
) {
	return Policy.publicPolicy((user) =>
		decideCanView(deps, user, video, password),
	);
}

export class VideosPolicy extends Effect.Service<VideosPolicy>()(
	"VideosPolicy",
	{
		effect: Effect.gen(function* () {
			const repo = yield* VideosRepo;
			const orgsRepo = yield* OrganisationsRepo;
			const spacesRepo = yield* SpacesRepo;

			const deps: VideosPolicyDeps = { repo, orgsRepo, spacesRepo };

			const canView = (videoId: Video.VideoId) => buildCanView(deps, videoId);

			const canViewLoaded = (
				video: ViewableVideo,
				password: Option.Option<string>,
			) => buildCanViewLoaded(deps, video, password);

			const canViewLoadedBulk = Effect.fn("VideosPolicy.canViewLoadedBulk")(
				function* (
					loaded: ReadonlyArray<
						readonly [ViewableVideo, Option.Option<string>]
					>,
				) {
					const user = yield* Effect.serviceOption(CurrentUser);
					const videoIds = loaded
						.filter(
							([video]) =>
								Option.isNone(user) || video.ownerId !== user.value.id,
						)
						.map(([video]) => video.id);
					if (videoIds.length === 0)
						return yield* Effect.forEach(loaded, ([video, password]) =>
							canViewLoaded(video, password).pipe(Effect.exit),
						);

					// Retain lookup failures until each policy runs, including the owner bypass.
					const [passwords, orgMemberships, spaceMemberships] =
						yield* Effect.all(
							[
								spacesRepo.passwordsForVideos(videoIds).pipe(
									Effect.map((rows) =>
										Array.groupBy(rows, (row) => row.videoId),
									),
									Effect.exit,
								),
								(Option.isSome(user)
									? orgsRepo.membershipsForVideos(user.value.id, videoIds)
									: Effect.succeed([])
								).pipe(
									Effect.map((rows) =>
										Array.groupBy(rows, (row) => row.videoId),
									),
									Effect.exit,
								),
								(Option.isSome(user)
									? spacesRepo.membershipsForVideos(user.value.id, videoIds)
									: Effect.succeed([])
								).pipe(
									Effect.map((rows) =>
										Array.groupBy(rows, (row) => row.videoId),
									),
									Effect.exit,
								),
							],
							{ concurrency: "unbounded" },
						);
					const bulkDeps: ViewDecisionDeps = {
						...deps,
						orgsRepo: {
							...orgsRepo,
							membershipForVideo: (_, id) =>
								Effect.map(orgMemberships, (rows) => rows[id] ?? []),
						},
						spacesRepo: {
							...spacesRepo,
							passwordsForVideo: (id) =>
								Effect.map(passwords, (rows) => rows[id] ?? []),
							membershipForVideo: (_, id) =>
								Effect.map(spaceMemberships, (rows) =>
									Option.fromNullable(rows[id]?.[0]),
								),
						},
					};
					return yield* Effect.forEach(
						loaded,
						([video, password]) =>
							buildCanViewLoaded(bulkDeps, video, password).pipe(Effect.exit),
						{ concurrency: 10 },
					);
				},
			);

			const isOwner = (videoId: Video.VideoId) =>
				Policy.policy((user) =>
					repo.getById(videoId).pipe(
						Effect.map(
							Option.match({
								onNone: () => true,
								onSome: ([video]) => video.ownerId === user.id,
							}),
						),
					),
				);

			const isOwnerLoaded = (video: Pick<Video.Video, "ownerId">) =>
				Policy.policy((user) => Effect.succeed(video.ownerId === user.id));

			const getViewableById = (videoId: Video.VideoId) =>
				repo.getById(videoId).pipe(
					Effect.flatMap(
						Option.match({
							onNone: () => Effect.succeed(Option.none<LoadedVideo>()),
							onSome: (loaded) =>
								canViewLoaded(loaded[0], loaded[1]).pipe(
									Effect.as(Option.some(loaded)),
								),
						}),
					),
				);

			const getOwnedById = (videoId: Video.VideoId) =>
				repo.getById(videoId).pipe(
					Effect.flatMap(
						Option.match({
							onNone: () => Effect.succeed(Option.none<LoadedVideo>()),
							onSome: (loaded) =>
								isOwnerLoaded(loaded[0]).pipe(Effect.as(Option.some(loaded))),
						}),
					),
				);

			return {
				canView,
				canViewLoaded,
				canViewLoadedBulk,
				isOwner,
				isOwnerLoaded,
				getViewableById,
				getOwnedById,
			};
		}),
		dependencies: [
			VideosRepo.Default,
			OrganisationsRepo.Default,
			SpacesRepo.Default,
			Database.Default,
		],
	},
) {}