import type { EntityCascadingDeletionInfo } from './EntityMutationInfo.ts'; import type { EntityQueryContext } from './EntityQueryContext.ts'; import type { ReadonlyEntity } from './ReadonlyEntity.ts'; import type { ViewerContext } from './ViewerContext.ts'; import type { EntityNotAuthorizedDenialReason, EntityNotAuthorizedUserFacingReason } from './errors/EntityNotAuthorizedError.ts'; import { EntityNotAuthorizedError } from './errors/EntityNotAuthorizedError.ts'; import type { IEntityMetricsAdapter } from './metrics/IEntityMetricsAdapter.ts'; import type { PrivacyPolicyRule } from './rules/PrivacyPolicyRule.ts'; /** * Information about the reason this privacy policy is being evaluated. */ export type EntityPrivacyPolicyEvaluationContext, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TSelectedFields extends keyof TFields = keyof TFields> = { /** * When this privacy policy is being evaluated as a result of an update, this will be populated with the value * of the entity before the update. Note that this doesn't only apply to UPDATE authorization actions though: * when an entity is updated it is re-LOADed after the update completes. */ previousValue: TEntity | null; /** * When this privacy policy is being evaluated as a result of a cascading deletion, this will be populated * with information on the cascading delete. */ cascadingDeleteCause: EntityCascadingDeletionInfo | null; }; export type EntityPrivacyPolicyRuleEvaluationContext, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TSelectedFields extends keyof TFields = keyof TFields> = EntityPrivacyPolicyEvaluationContext & { /** * The CRUD action for which the privacy policy rule is being evaluated. */ action: EntityAuthorizationAction; }; /** * Evaluation mode for a EntityPrivacyPolicy. Useful when transitioning to * using Entity for privacy. */ export declare enum EntityPrivacyPolicyEvaluationMode { /** * Enforce this privacy policy. Throw upon denial. */ ENFORCE = 0, /** * Do not enforce this privacy policy. Always allow but log when it would have denied. */ DRY_RUN = 1, /** * Enforce this privacy policy. Throw and log upon denial. */ ENFORCE_AND_LOG = 2 } export type EntityPrivacyPolicyEvaluator, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TSelectedFields extends keyof TFields = keyof TFields> = { mode: EntityPrivacyPolicyEvaluationMode.ENFORCE; } | { mode: EntityPrivacyPolicyEvaluationMode.DRY_RUN; denyHandler: (error: EntityNotAuthorizedError) => void; } | { mode: EntityPrivacyPolicyEvaluationMode.ENFORCE_AND_LOG; denyHandler: (error: EntityNotAuthorizedError) => void; }; export declare enum EntityAuthorizationAction { CREATE = 0, READ = 1, UPDATE = 2, DELETE = 3 } /** * Privacy policy for an entity. * * @remarks * * A privacy policy declares lists of PrivacyPolicyRule for create, read, update, and delete actions * for an entity and provides logic for authorizing an entity against rules. * * Evaluation of a list of rules is performed according the following example. This allows constructing of * complex yet testable permissioning logic for an entity. * * @example * * ``` * foreach rule in rules: * return authorized if rule allows * return not authorized if rule denies * continue to next rule if rule skips * return not authorized if all rules skip * ``` */ export declare abstract class EntityPrivacyPolicy, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TSelectedFields extends keyof TFields = keyof TFields> { /** * List of rules to evaluate for create authorization. */ protected readonly createRules: readonly PrivacyPolicyRule[]; /** * List of rules to evaluate for read authorization. */ protected readonly readRules: readonly PrivacyPolicyRule[]; /** * List of rules to evaluate for update authorization. */ protected readonly updateRules: readonly PrivacyPolicyRule[]; /** * List of rules to evaluate for delete authorization. */ protected readonly deleteRules: readonly PrivacyPolicyRule[]; /** * Get the privacy policy evaluation mode and deny handler for this policy. * Defaults to normal enforcing policy. * * DRY_RUN mode is useful for testing and logging the effects of a policy without actually enforcing it, such as when * first rolling out a new policy. Entities that fail the policy will be allowed so caution should be take when using. * * @remarks * * Override to enable dry run evaluation of the policy. */ protected getPrivacyPolicyEvaluator(_viewerContext: TViewerContext): EntityPrivacyPolicyEvaluator; /** * Produce an end-user-safe explanation for a denial of this policy. The result is attached to the * thrown EntityNotAuthorizedError as `userFacingReason`. * * @remarks * * The default returns null, meaning no user-facing explanation is available and callers should * display a generic message. Override to map denials to messages appropriate for end users. * When all rules skip, `denialReason.skippedRules` holds each skipped rule with the reason codes it * contributed, so the policy can select a message for a specific reason, for example "You must be a * member of this account to view this project." when a rule skipped with a NOT_ACCOUNT_MEMBER reason. * * The evaluation context and the denied entity are passed so that the message can be selected based * on them (for example, the entity type or whether the denial is part of a cascading deletion). The * viewer is not authorized for the entity, and the evaluation context may contain it (as the previous * value) or other entities (as the cascading deletion cause), so take care that the message does not * reveal any data that the viewer may not see. Rules should communicate what is needed through reason codes. * * @param _viewerContext - viewer context that was denied * @param _evaluationContext - context in which the policy was evaluated * @param _notAuthorizedEntity - entity for which the action was denied * @param _action - the action that was denied * @param _denialReason - structured, developer-facing reason for the denial */ protected getUserFacingDenialReason(_viewerContext: TViewerContext, _evaluationContext: EntityPrivacyPolicyEvaluationContext, _notAuthorizedEntity: TEntity, _action: EntityAuthorizationAction, _denialReason: EntityNotAuthorizedDenialReason): EntityNotAuthorizedUserFacingReason | null; /** * Authorize an entity against creation policy. * @param viewerContext - viewer context of user creating the entity * @param queryContext - query context in which to perform the create authorization * @param entity - entity to authorize * @returns entity if authorized * @throws EntityNotAuthorizedError when not authorized */ authorizeCreateAsync(viewerContext: TViewerContext, queryContext: EntityQueryContext, evaluationContext: EntityPrivacyPolicyEvaluationContext, entity: TEntity, metricsAdapter: IEntityMetricsAdapter): Promise; /** * Authorize an entity against read policy. * @param viewerContext - viewer context of user reading the entity * @param queryContext - query context in which to perform the read authorization * @param evaluationContext - context about the reason for this privacy policy evaluation * @param entity - entity to authorize * @param metricsAdapter - adapter for logging metrics about this authorization * @returns entity if authorized * @throws EntityNotAuthorizedError when not authorized */ authorizeReadAsync(viewerContext: TViewerContext, queryContext: EntityQueryContext, evaluationContext: EntityPrivacyPolicyEvaluationContext, entity: TEntity, metricsAdapter: IEntityMetricsAdapter): Promise; /** * Authorize an entity against update policy. * @param viewerContext - viewer context of user updating the entity * @param queryContext - query context in which to perform the update authorization * @param evaluationContext - context about the reason for this privacy policy evaluation * @param entity - entity to authorize * @param metricsAdapter - adapter for logging metrics about this authorization * @returns entity if authorized * @throws EntityNotAuthorizedError when not authorized */ authorizeUpdateAsync(viewerContext: TViewerContext, queryContext: EntityQueryContext, evaluationContext: EntityPrivacyPolicyEvaluationContext, entity: TEntity, metricsAdapter: IEntityMetricsAdapter): Promise; /** * Authorize an entity against deletion policy. * @param viewerContext - viewer context of user deleting the entity * @param queryContext - query context in which to perform the delete authorization * @param evaluationContext - context about the reason for this privacy policy evaluation * @param entity - entity to authorize * @param metricsAdapter - adapter for logging metrics about this authorization * @returns entity if authorized * @throws EntityNotAuthorizedError when not authorized */ authorizeDeleteAsync(viewerContext: TViewerContext, queryContext: EntityQueryContext, evaluationContext: EntityPrivacyPolicyEvaluationContext, entity: TEntity, metricsAdapter: IEntityMetricsAdapter): Promise; private authorizeForRulesetAsync; private authorizeForRulesetInnerAsync; private createNotAuthorizedError; }