123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { createMessage } from '@formkit/core'
- import { i18n } from '#shared/i18n.ts'
- import { FormHandlerExecution } from '#shared/components/Form/types.ts'
- import type { FormKitNode } from '@formkit/core'
- import type {
- ChangedField,
- FormHandler,
- FormHandlerFunction,
- ReactiveFormSchemData,
- } from '#shared/components/Form/types.ts'
- export const useSSLVerificationWarningHandler = (): FormHandler => {
- const addWarning = (formNode?: FormKitNode) => {
- formNode?.store.set(
- createMessage({
- blocking: false,
- key: 'sslVerificationWarning',
- type: 'warning',
- value: i18n.t(
- 'Turning off SSL verification is a security risk and should be used only temporary. Use this option at your own risk!',
- ),
- visible: true,
- }),
- )
- }
- const clearWarning = (formNode?: FormKitNode) => {
- formNode?.store.remove('sslVerificationWarning')
- }
- const initializeSSLVerifyDisabledNodeEvent = (
- sslFieldNode: FormKitNode,
- formNode: FormKitNode,
- ) => {
- sslFieldNode.on('prop:disabled', ({ origin }) => {
- const { props, value } = origin
- if (props.disabled) clearWarning(formNode)
- else if (value === false) addWarning(formNode)
- })
- }
- const initializeFormNodeEvents = (formNode: FormKitNode) => {
- formNode.on('child.deep', ({ payload }) => {
- const childNode = payload as FormKitNode
- if (childNode.name !== 'sslVerify') return
- initializeSSLVerifyDisabledNodeEvent(childNode, formNode)
- childNode.on('destroying', () => {
- clearWarning(formNode)
- })
- })
- }
- const initializeSSLVerifyNodeEvents = (
- execution: FormHandlerExecution,
- getNodeByName: (id: string) => FormKitNode | undefined,
- formNode?: FormKitNode,
- ) => {
- if (execution === FormHandlerExecution.InitialSettled && formNode) {
- const sslFieldNode = getNodeByName('sslVerify')
- if (sslFieldNode) {
- initializeSSLVerifyDisabledNodeEvent(sslFieldNode, formNode)
- sslFieldNode.on('destroying', () => {
- clearWarning(formNode)
- initializeFormNodeEvents(formNode)
- })
- } else {
- initializeFormNodeEvents(formNode)
- }
- }
- }
- const executeHandler = (
- execution: FormHandlerExecution,
- schemaData: ReactiveFormSchemData,
- changedField?: ChangedField,
- formNode?: FormKitNode,
- ) => {
- if (
- schemaData.fields.sslVerify === undefined ||
- schemaData.fields.sslVerify === null ||
- (execution === FormHandlerExecution.FieldChange &&
- (!changedField || changedField.name !== 'sslVerify')) ||
- (typeof formNode?.find === 'function' && !formNode?.find('sslVerify'))
- ) {
- return false
- }
- return true
- }
- const handleSSLVerificationWarning: FormHandlerFunction = (
- execution,
- reactivity,
- data,
- ) => {
- const { changedField, formNode, getNodeByName } = data
- const { schemaData } = reactivity
- initializeSSLVerifyNodeEvents(execution, getNodeByName, formNode)
- if (!executeHandler(execution, schemaData, changedField, formNode)) return
- if (
- !schemaData.fields.sslVerify.props.disabled &&
- ((execution === FormHandlerExecution.InitialSettled &&
- schemaData.fields.sslVerify.props.value === false) ||
- (execution === FormHandlerExecution.FieldChange &&
- changedField?.newValue === false))
- ) {
- addWarning(formNode)
- return
- }
- clearWarning(formNode)
- }
- return {
- execution: [
- FormHandlerExecution.InitialSettled,
- FormHandlerExecution.FieldChange,
- ],
- callback: handleSSLVerificationWarning,
- }
- }
|