Open History to see the last commit
1/**
2 * DesktopManagedPolicy - the administrator's managed policy as the desktop app reads it, at most
3 * once: every desktop check needs the same answer.
4 *
5 * @module DesktopManagedPolicy
6 */
7import type { ManagedPolicyDocument } from "@t3tools/contracts";
8import {
9 layerWindowsRegistry,
10 ManagedPolicyFileError,
11 readManagedPolicy,
12 WindowsRegistry,
13} from "@t3tools/shared/managedPolicyFile";
14import * as Context from "effect/Context";
15import * as Effect from "effect/Effect";
16import * as FileSystem from "effect/FileSystem";
17import * as Layer from "effect/Layer";
18import * as Schema from "effect/Schema";
19import { ChildProcessSpawner } from "effect/process";
20
21import * as DesktopEnvironment from "./DesktopEnvironment.ts";
22
23type FoundPolicy = Effect.Success<ReturnType<typeof readManagedPolicy>>;
24const isManagedPolicyFileError = Schema.is(ManagedPolicyFileError);
25
26export class DesktopManagedPolicy extends Context.Service<
27 DesktopManagedPolicy,
28 {
29 /** The policy on this machine, undefined without one; fails when it cannot be used. */
30 readonly read: Effect.Effect<FoundPolicy, ManagedPolicyFileError>;
31 }
32>()("@t3tools/desktop/app/DesktopManagedPolicy") {}
33
34export const layer = Layer.effect(
35 DesktopManagedPolicy,
36 Effect.gen(function* () {
37 const { platform } = yield* DesktopEnvironment.DesktopEnvironment;
38 const read = yield* Effect.cached(
39 readManagedPolicy(platform).pipe(
40 Effect.provideService(FileSystem.FileSystem, yield* FileSystem.FileSystem),
41 Effect.provideService(
42 ChildProcessSpawner.ChildProcessSpawner,
43 yield* ChildProcessSpawner.ChildProcessSpawner,
44 ),
45 Effect.provideService(WindowsRegistry, yield* WindowsRegistry),
46 ),
47 );
48 return DesktopManagedPolicy.of({ read });
49 }),
50).pipe(Layer.provide(layerWindowsRegistry(() => import("ffi-rs"))));
51
52/** A machine with `policy` deployed, one whose policy cannot be used, or none. */
53export const layerTest = (policy?: ManagedPolicyDocument | ManagedPolicyFileError) =>
54 Layer.succeed(
55 DesktopManagedPolicy,
56 DesktopManagedPolicy.of({
57 read:
58 policy === undefined
59 ? Effect.undefined
60 : isManagedPolicyFileError(policy)
61 ? Effect.fail(policy)
62 : Effect.succeed({ source: "/test/policy.json", document: policy }),
63 }),
64 );