-
Notifications
You must be signed in to change notification settings - Fork 3
Feature/move digital signature configuration to handler #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jekuaitk
wants to merge
7
commits into
OS2Forms:develop
Choose a base branch
from
itk-dev:feature/move-digital-signature-configuration-to-handler
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09f9b1a
Move digital signature confgurations to handler
jekuaitk dc55487
Merge branch 'develop' into feature/move-digital-signature-configurat…
jekuaitk a335a56
Merge branch 'develop' into feature/move-digital-signature-configurat…
jekuaitk ee0a2a2
Ensured signed documents are fetched when getting file content
jekuaitk 8c9e4b3
Mimic original behaviour when migrating
jekuaitk e1abc9d
Ensured only os2forms_digital_signature_document and os2forms_attachm…
jekuaitk f45f900
Added batching to the migration
jekuaitk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,23 @@ public static function getFileContent(array $element, WebformSubmissionInterface | |
| if ($element['#export_type'] === 'pdf') { | ||
| $file_path = NULL; | ||
|
|
||
| // Digital signature settings live on the digital signature handler. | ||
| // Resolving them here ensures all consumers of the element (email | ||
| // handlers, attachment downloads) serve an already signed document | ||
| // and render the validation text consistently. | ||
| $elementKey = $element['#webform_key'] ?? NULL; | ||
| if ($elementKey !== NULL) { | ||
| foreach ($webform_submission->getWebform()->getHandlers('os2forms_digital_signature') as $handler) { | ||
| $settings = $handler->getConfiguration()['settings'] ?? []; | ||
| if ($handler->isEnabled() && ($settings['attachment_element'] ?? '') === $elementKey) { | ||
| $element['#digital_signature'] = TRUE; | ||
| $element['#digital_signature_position'] = $settings['signature_position'] | ||
| ?? Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to this code, we allow multiple handlers of type Not a bug/issue, but something to be aware about |
||
|
|
||
| // If attachment with digital signatur, check if we already have one. | ||
| if (isset($element['#digital_signature']) && $element['#digital_signature']) { | ||
| // Get scheme. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
modules/os2forms_digital_signature/os2forms_digital_signature.install
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @file | ||
| * Install, update and uninstall for the os2forms_digital_signature module. | ||
| */ | ||
|
|
||
| use Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder; | ||
| use Drupal\webform\Entity\Webform; | ||
|
|
||
| /** | ||
| * Migrate digital signature configuration onto the handler. | ||
| * | ||
| * Previously the os2forms_attachment element exposed #digital_signature and | ||
| * #digital_signature_position properties, and the handler picked the element | ||
| * to sign by type: the first os2forms_digital_signature_document element, | ||
| * falling back to the first os2forms_attachment element. The configuration | ||
| * now lives on the os2forms_digital_signature handler, so this update | ||
| * replicates that selection into the handler config of every webform using | ||
| * the handler, and strips the legacy element properties from all webforms. | ||
| * | ||
| * Processes the webforms in batches of 25 via the update sandbox. | ||
| */ | ||
| function os2forms_digital_signature_update_10001(&$sandbox) { | ||
| $logger = \Drupal::logger('os2forms_digital_signature'); | ||
|
|
||
| if (!isset($sandbox['ids'])) { | ||
| $sandbox['ids'] = array_values(\Drupal::entityQuery('webform')->accessCheck(FALSE)->execute()); | ||
| $sandbox['migrated'] = 0; | ||
| } | ||
|
|
||
| // array_splice() removes the chunk from the sandbox, so the next pass | ||
| // continues with the remaining ids. | ||
| /** @var \Drupal\webform\WebformInterface[] $webforms */ | ||
| $webforms = Webform::loadMultiple(array_splice($sandbox['ids'], 0, 25)); | ||
| foreach ($webforms as $webform) { | ||
| $handlers = $webform->getHandlers(); | ||
| $signatureHandler = NULL; | ||
| foreach ($handlers as $handler) { | ||
| if ($handler->getPluginId() === 'os2forms_digital_signature') { | ||
| $signatureHandler = $handler; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| $elements = $webform->getElementsDecodedAndFlattened(); | ||
| $needsSave = FALSE; | ||
|
|
||
| if ($signatureHandler) { | ||
| // Mirror the pre-update runtime selection: the handler signed the | ||
| // first os2forms_digital_signature_document element, falling back to | ||
| // the first os2forms_attachment element. The #digital_signature | ||
| // property never influenced the selection, only the rendering of the | ||
| // validation text. | ||
| $signedKey = NULL; | ||
| $signedPosition = Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT; | ||
| $candidates = []; | ||
| foreach (['os2forms_digital_signature_document', 'os2forms_attachment'] as $type) { | ||
| foreach ($elements as $key => $element) { | ||
| if (($element['#type'] ?? NULL) === $type) { | ||
| $candidates[] = $key; | ||
| if ($signedKey === NULL) { | ||
| $signedKey = $key; | ||
| if ($type === 'os2forms_attachment') { | ||
| $signedPosition = $element['#digital_signature_position'] ?? $signedPosition; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($signedKey === NULL) { | ||
| $logger->warning('Skipped webform @id during digital signature config migration: the handler is enabled but the webform has no os2forms_digital_signature_document or os2forms_attachment element. Configure the handler manually.', [ | ||
| '@id' => $webform->id(), | ||
| ]); | ||
| } | ||
| else { | ||
| if (count($candidates) > 1) { | ||
| $logger->warning('Webform @id has multiple signable elements (@all); migrating @chosen to match the pre-update type-priority selection.', [ | ||
| '@id' => $webform->id(), | ||
| '@all' => implode(', ', $candidates), | ||
| '@chosen' => $signedKey, | ||
| ]); | ||
| } | ||
| foreach ($elements as $key => $element) { | ||
| if (!empty($element['#digital_signature']) && $key !== $signedKey) { | ||
| $logger->warning('Webform @id: element @key had digital signature enabled, but @chosen is migrated instead to match the pre-update behavior.', [ | ||
| '@id' => $webform->id(), | ||
| '@key' => $key, | ||
| '@chosen' => $signedKey, | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| // Write the new config onto the handler. | ||
| $config = $signatureHandler->getConfiguration(); | ||
| $config['settings']['attachment_element'] = $signedKey; | ||
| $config['settings']['signature_position'] = $signedPosition; | ||
| $signatureHandler->setConfiguration($config); | ||
| $needsSave = TRUE; | ||
| $sandbox['migrated']++; | ||
| } | ||
| } | ||
|
|
||
| // Strip the legacy properties whether or not the handler is present, so | ||
| // no webform is left with inert digital signature configuration. | ||
| foreach ($elements as $element) { | ||
| if (array_key_exists('#digital_signature', $element) || array_key_exists('#digital_signature_position', $element)) { | ||
| $rawElements = $webform->getElementsDecoded(); | ||
| _os2forms_digital_signature_strip_legacy_props($rawElements); | ||
| $webform->setElements($rawElements); | ||
| $needsSave = TRUE; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ($needsSave) { | ||
| $webform->save(); | ||
| } | ||
| } | ||
|
|
||
| // Anything below 1 makes the runner call us again. Done when no ids remain. | ||
| $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : 0; | ||
|
|
||
| if ($sandbox['#finished'] >= 1) { | ||
| return t('Migrated digital signature config on @count webform(s).', ['@count' => $sandbox['migrated']]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Recursively remove legacy digital signature element properties. | ||
| * | ||
| * @param array $elements | ||
| * The decoded elements tree, modified in place. | ||
| */ | ||
| function _os2forms_digital_signature_strip_legacy_props(array &$elements) { | ||
| foreach ($elements as $key => &$value) { | ||
| if (!is_array($value)) { | ||
| continue; | ||
| } | ||
| if (str_starts_with((string) $key, '#')) { | ||
| continue; | ||
| } | ||
| unset($value['#digital_signature'], $value['#digital_signature_position']); | ||
| _os2forms_digital_signature_strip_legacy_props($value); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only checks whether the handler is globally enabled, but Webform also skips handlers whose per-submission conditions do not match. In that case
preSave()does not sign the document, while this code still sets#digital_signature = TRUEand generates an unsigned PDF containing signature-validation text. Please also check$handler->checkConditions($webform_submission)here.suggestion how to change it: