import type { IEntityClass } from '../Entity.ts'; import type { EntityPrivacyPolicy, EntityPrivacyPolicyEvaluationContext, } from '../EntityPrivacyPolicy.ts'; import type { EntityQueryContext } from '../EntityQueryContext.ts'; import type { ReadonlyEntity } from '../ReadonlyEntity.ts'; import type { ViewerContext } from '../ViewerContext.ts'; import { PrivacyPolicyRule, RuleEvaluationResult } from './PrivacyPolicyRule.ts'; /** * Directive for specifying the parent relationship in AllowIfInParentCascadeDeletionPrivacyPolicyRule. */ export interface AllowIfInParentCascadeDeletionDirective< TViewerContext extends ViewerContext, TFields, TParentFields extends object, TParentIDField extends keyof NonNullable>, TParentEntity extends ReadonlyEntity< TParentFields, TParentIDField, TViewerContext, TParentSelectedFields >, TParentPrivacyPolicy extends EntityPrivacyPolicy< TParentFields, TParentIDField, TViewerContext, TParentEntity, TParentSelectedFields >, TSelectedFields extends keyof TFields = keyof TFields, TParentSelectedFields extends keyof TParentFields = keyof TParentFields, > { /** * Class (or classes) of parent entity that should trigger a cascade set null update to a field * within the entity being authorized. Pass an array when multiple sibling classes backed by the * same table can be the deletion source. */ parentEntityClass: | IEntityClass< TParentFields, TParentIDField, TViewerContext, TParentEntity, TParentPrivacyPolicy, TParentSelectedFields > | readonly IEntityClass< TParentFields, TParentIDField, TViewerContext, TParentEntity, TParentPrivacyPolicy, TParentSelectedFields >[]; /** * Field of the current entity with references the deleting instace of parentEntityClass. */ fieldIdentifyingParentEntity: keyof Pick; /** * Field in parentEntityClass referenced by the value of fieldIdentifyingParentEntity. * If not provided, ID is assumed. */ parentEntityLookupByField?: keyof Pick; } /** * A generic privacy policy rule that allows when an entity is being authorized * as part of a cascading delete from a parent entity. Handles two cases: * - When the field has not yet been null'ed out due to a cascading set null. This is often * required for read rules to authorize the initial re-read of the entity being update set null'ed. * - When the field has been null'ed out due to a cascading set null. This is often required * the update rules for the field nullification. * * These two cases could theoretically be handled by two separate (stricter) rules, but are combined * to simplify configuration since practically there are few cases where having them be combined would * preset an issue. * * @example * Billing info owned by an account, but records who created the billing info in creating_user_id. User is a member of that account. * User can delete themselves, and the billing info's creating_user_id field is cascade set null'ed when the user is deleted. * * ```ts * class BillingInfoEntityPrivacyPolicy extends EntityPrivacyPolicy<...> { * protected override readonly readRules = [ * ..., * new AllowIfInParentCascadeDeletionPrivacyPolicyRule<...>({ * fieldIdentifyingParentEntity: 'creating_user_id', * parentEntityClass: UserEntity, * }), * ]; * * protected override readonly updateRules = [ * ..., * new AllowIfInParentCascadeDeletionPrivacyPolicyRule<...>({ * fieldIdentifyingParentEntity: 'creating_user_id', * parentEntityClass: UserEntity, * }), * ]; * } * ``` */ export class AllowIfInParentCascadeDeletionPrivacyPolicyRule< TFields extends object, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TFields2 extends object, TIDField2 extends keyof NonNullable>, TEntity2 extends ReadonlyEntity, TPrivacyPolicy2 extends EntityPrivacyPolicy< TFields2, TIDField2, TViewerContext, TEntity2, TSelectedFields2 >, TSelectedFields extends keyof TFields = keyof TFields, TSelectedFields2 extends keyof TFields2 = keyof TFields2, > extends PrivacyPolicyRule { constructor( private readonly directive: AllowIfInParentCascadeDeletionDirective< TViewerContext, TFields, TFields2, TIDField2, TEntity2, TPrivacyPolicy2, TSelectedFields, TSelectedFields2 >, ) { super(); } async evaluateAsync( _viewerContext: TViewerContext, _queryContext: EntityQueryContext, evaluationContext: EntityPrivacyPolicyEvaluationContext< TFields, TIDField, TViewerContext, TEntity, TSelectedFields >, entity: TEntity, ): Promise { const parentEntityClasses = Array.isArray(this.directive.parentEntityClass) ? this.directive.parentEntityClass : [this.directive.parentEntityClass]; const deleteCause = evaluationContext.cascadingDeleteCause; if (!deleteCause || !parentEntityClasses.some((cls) => deleteCause.entity instanceof cls)) { return RuleEvaluationResult.SKIP; } const entityBeingDeleted = deleteCause.entity; // allow if parent foreign key field matches specified field in the entity being authorized const valueInThisEntityReferencingParent = entity.getField( this.directive.fieldIdentifyingParentEntity, ); const valueInParent = this.directive.parentEntityLookupByField ? entityBeingDeleted.getField(this.directive.parentEntityLookupByField) : entityBeingDeleted.getID(); if ( valueInThisEntityReferencingParent && valueInThisEntityReferencingParent === valueInParent ) { return RuleEvaluationResult.ALLOW; } // allow if parent foreign key field matches specified field in the entity being authorized, and the // field in the entity being authorized has been null'ed out due to cascading set null const valueInPreviousValueOfThisEntityReferencingParent = evaluationContext.previousValue?.getField(this.directive.fieldIdentifyingParentEntity); if ( valueInPreviousValueOfThisEntityReferencingParent && valueInPreviousValueOfThisEntityReferencingParent === valueInParent && valueInThisEntityReferencingParent === null ) { return RuleEvaluationResult.ALLOW; } return RuleEvaluationResult.SKIP; } }