mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: add document auth (#1029)
This commit is contained in:
72
packages/lib/utils/document-auth.ts
Normal file
72
packages/lib/utils/document-auth.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import type { Document, Recipient } from '@documenso/prisma/client';
|
||||
|
||||
import type {
|
||||
TDocumentAuthOptions,
|
||||
TRecipientAccessAuthTypes,
|
||||
TRecipientActionAuthTypes,
|
||||
TRecipientAuthOptions,
|
||||
} from '../types/document-auth';
|
||||
import { DocumentAuth } from '../types/document-auth';
|
||||
import { ZDocumentAuthOptionsSchema, ZRecipientAuthOptionsSchema } from '../types/document-auth';
|
||||
|
||||
type ExtractDocumentAuthMethodsOptions = {
|
||||
documentAuth: Document['authOptions'];
|
||||
recipientAuth?: Recipient['authOptions'];
|
||||
};
|
||||
|
||||
/**
|
||||
* Parses and extracts the document and recipient authentication values.
|
||||
*
|
||||
* Will combine the recipient and document auth values to derive the final
|
||||
* auth values for a recipient if possible.
|
||||
*/
|
||||
export const extractDocumentAuthMethods = ({
|
||||
documentAuth,
|
||||
recipientAuth,
|
||||
}: ExtractDocumentAuthMethodsOptions) => {
|
||||
const documentAuthOption = ZDocumentAuthOptionsSchema.parse(documentAuth);
|
||||
const recipientAuthOption = ZRecipientAuthOptionsSchema.parse(recipientAuth);
|
||||
|
||||
const derivedRecipientAccessAuth: TRecipientAccessAuthTypes | null =
|
||||
recipientAuthOption.accessAuth || documentAuthOption.globalAccessAuth;
|
||||
|
||||
const derivedRecipientActionAuth: TRecipientActionAuthTypes | null =
|
||||
recipientAuthOption.actionAuth || documentAuthOption.globalActionAuth;
|
||||
|
||||
const recipientAccessAuthRequired = derivedRecipientAccessAuth !== null;
|
||||
|
||||
const recipientActionAuthRequired =
|
||||
derivedRecipientActionAuth !== DocumentAuth.EXPLICIT_NONE &&
|
||||
derivedRecipientActionAuth !== null;
|
||||
|
||||
return {
|
||||
derivedRecipientAccessAuth,
|
||||
derivedRecipientActionAuth,
|
||||
recipientAccessAuthRequired,
|
||||
recipientActionAuthRequired,
|
||||
documentAuthOption,
|
||||
recipientAuthOption,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Create document auth options in a type safe way.
|
||||
*/
|
||||
export const createDocumentAuthOptions = (options: TDocumentAuthOptions): TDocumentAuthOptions => {
|
||||
return {
|
||||
globalAccessAuth: options?.globalAccessAuth ?? null,
|
||||
globalActionAuth: options?.globalActionAuth ?? null,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Create recipient auth options in a type safe way.
|
||||
*/
|
||||
export const createRecipientAuthOptions = (
|
||||
options: TRecipientAuthOptions,
|
||||
): TRecipientAuthOptions => {
|
||||
return {
|
||||
accessAuth: options?.accessAuth ?? null,
|
||||
actionAuth: options?.actionAuth ?? null,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user