Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1.3 KB
Raw Blame
import {
  loadPolicies as loadServerPolicies,
  rawStoredPolicies as rawServerPolicies,
} from "@editor-proprietary/services/policyStorage";
export {
  clearPolicies,
  updatePolicy,
  forgetPolicies,
  onPoliciesChange,
  POLICIES_CHANGE_EVENT,
} from "@editor-proprietary/services/policyStorage";
import { connectionModeService } from "@editor/services/connectionModeService";
import { authService } from "@editor/services/authService";
import type { PoliciesByKey } from "@editor/types/policies";

function canReadPolicies(): boolean {
  const mode = connectionModeService.getCachedMode();
  const status = authService.getAuthStatus();
  return (
    mode !== null &&
    mode !== "local" &&
    (status === "authenticated" || status === "refreshing")
  );
}

/** Local-only sessions have no policy enforcement, including cached export policies. */
export function loadPolicies(): PoliciesByKey {
  return canReadPolicies() ? loadServerPolicies() : {};
}

export function rawStoredPolicies(): string | null {
  return canReadPolicies() ? rawServerPolicies() : null;
}

/** Reads through the guarded loadPolicies, so a local-only session enforces nothing. */
export function isEnforcedPolicy(policyKey: string | undefined): boolean {
  return policyKey != null && loadPolicies()[policyKey]?.required === true;
}