diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 6a8b395574..5178703cef 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -32,6 +32,7 @@ use OCA\Libresign\Service\SessionService; use OCA\Libresign\Service\SignerElementsService; use OCA\Libresign\Service\SignFileService; +use OCA\Libresign\Service\SignRequest\SignerLanguageService; use OCA\Libresign\Service\Validation\SigningRequestValidator; use OCA\Viewer\Event\LoadViewer; use OCP\AppFramework\Db\DoesNotExistException; @@ -84,6 +85,7 @@ public function __construct( private IEventDispatcher $eventDispatcher, private IURLGenerator $urlGenerator, private EmailSignerElementsService $emailSignerElementsService, + private SignerLanguageService $signerLanguageService, ) { parent::__construct( request: $request, @@ -116,6 +118,7 @@ public function index(): TemplateResponse { } catch (LibresignException) { $this->initialState->provideInitialState('can_request_sign', false); } + $this->initialState->provideInitialState('signer_languages', $this->signerLanguageService->getAvailableLanguages()); $this->provideSignerSignatues(); $this->initialState->provideInitialState('effective_policies', [ diff --git a/lib/Controller/RequestSignatureController.php b/lib/Controller/RequestSignatureController.php index 4d27495eca..c00c4c878b 100644 --- a/lib/Controller/RequestSignatureController.php +++ b/lib/Controller/RequestSignatureController.php @@ -60,7 +60,7 @@ public function __construct( * For `nodeType=file`, `filesCount=1` and `files` contains the current file. * For `nodeType=envelope`, `files` contains envelope child files. * - * @param list $signers Collection of signers who must sign the document. Use identifyMethods as the canonical format. Other supported fields: displayName, description, notify, signingOrder, status, deviceGeolocationRequired + * @param list $signers Collection of signers who must sign the document. Use identifyMethods as the canonical format. Other supported fields: displayName, description, notify, signingOrder, status, deviceGeolocationRequired, language (language code for the e-mails to the signer; empty uses the default) * @param string $name The name of file to sign * @param LibresignFolderSettings $settings Settings to define how and where the file should be stored * @param LibresignNewFile $file File object. Supports nodeId (a non-negative integer or its canonical decimal string, as Nextcloud node ids can exceed a JavaScript number), url, base64 or path. diff --git a/lib/Files/TemplateLoader.php b/lib/Files/TemplateLoader.php index 7579267f62..369d4c6054 100644 --- a/lib/Files/TemplateLoader.php +++ b/lib/Files/TemplateLoader.php @@ -15,6 +15,7 @@ use OCA\Libresign\Service\AccountService; use OCA\Libresign\Service\IdentifyMethodService; use OCA\Libresign\Service\Policy\PolicyService; +use OCA\Libresign\Service\SignRequest\SignerLanguageService; use OCA\Libresign\Service\Validation\SigningRequestValidator; use OCP\App\IAppManager; use OCP\AppFramework\Services\IInitialState; @@ -38,6 +39,7 @@ public function __construct( private PolicyService $policyService, private IAppManager $appManager, private TemplateLoaderAssets $assets, + private SignerLanguageService $signerLanguageService, ) { } @@ -71,6 +73,7 @@ protected function getInitialStatePayload(): array { 'policies' => $this->policyService->resolveKnownPolicyStates(), ], 'can_request_sign' => $this->canRequestSign(), + 'signer_languages' => $this->signerLanguageService->getAvailableLanguages(), ]; } diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index 81fe277cdf..f6911c5733 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -110,6 +110,7 @@ * status?: int, * participantRole?: LibresignParticipantRole, * deviceGeolocationRequired?: bool, + * language?: string, * } * @psalm-type LibresignNewFile = array{ * base64?: string, @@ -231,6 +232,7 @@ * geolocation?: LibresignSignerGeolocation, * notify?: list, * certificate_info?: LibresignSignerCertificateInfo, + * language?: string, * } * @psalm-type LibresignSignerDisplayStatus = 'draft'|'ready_to_sign'|'signed'|'rejected'|'observing'|'not_signed' * @psalm-type LibresignSignerSummary = array{ diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index c70a09b23d..00d8f73898 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -16,6 +16,7 @@ use OCA\Libresign\Exception\MailTransportException; use OCA\Libresign\Service\Policy\PolicyService; use OCA\Libresign\Service\Policy\Provider\MailSenderStrategy\MailSenderStrategyPolicy; +use OCA\Libresign\Service\SignRequest\SignerLanguageService; use OCP\IAppConfig; use OCP\IL10N; use OCP\IURLGenerator; @@ -44,6 +45,7 @@ public function __construct( private PolicyService $policyService, private IMailProviderManager $mailProviderManager, private IUserManager $userManager, + private SignerLanguageService $signerLanguageService, ) { } @@ -57,21 +59,39 @@ private function getFileById(int $fileId): File { return $this->files[$fileId]; } + /** + * Translations for a mail to a participant: the language the requester + * chose for them, or the default one. + */ + private function getParticipantL10n(SignRequest $signRequest): IL10N { + return $this->signerLanguageService->getL10n($signRequest) ?? $this->l10n; + } + + private function createParticipantEmailTemplate(IL10N $l10n): IEMailTemplate { + $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail'); + if ($l10n !== $this->l10n) { + $emailTemplate->setLanguage($l10n->getLanguageCode()); + } + return $emailTemplate; + } + /** * @psalm-suppress MixedMethodCall */ public function notifySignDataUpdated(SignRequest $data, string $email, ?string $description = null): void { $file = $this->getFileById($data->getFileId()); $isObserver = $data->isObserver(); + $l10n = $this->getParticipantL10n($data); - $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail'); + $emailTemplate = $this->createParticipantEmailTemplate($l10n); $this->applyParticipantNotificationHeader( $emailTemplate, + $l10n, $isObserver, // TRANSLATORS Email subject notifying an observer that a document changed and should be reviewed again. - $this->l10n->t('LibreSign: Changes were made to a document'), + $l10n->t('LibreSign: Changes were made to a document'), // TRANSLATORS Email subject notifying a signer that a pending signature request changed and should be reviewed again. - $this->l10n->t('LibreSign: Changes were made to a document waiting for your signature'), + $l10n->t('LibreSign: Changes were made to a document waiting for your signature'), ); if (!empty($description)) { @@ -81,13 +101,14 @@ public function notifySignDataUpdated(SignRequest $data, string $email, ?string $this->applyParticipantNotificationAction( $emailTemplate, + $l10n, $data, $file, $isObserver, // TRANSLATORS Email body telling an observer to reopen the document because some request details changed. - $this->l10n->t('Changes were made to a document. Open the link below:'), + $l10n->t('Changes were made to a document. Open the link below:'), // TRANSLATORS Email body telling the signer to reopen the request because some request details changed. - $this->l10n->t('Changes were made to a document you need to sign. Open the link below:'), + $l10n->t('Changes were made to a document you need to sign. Open the link below:'), ); try { $this->sendSignRequestNotification($emailTemplate, $data, $email); @@ -103,15 +124,17 @@ public function notifySignDataUpdated(SignRequest $data, string $email, ?string public function notifyUnsignedUser(SignRequest $data, string $email, ?string $description = null): void { $file = $this->getFileById($data->getFileId()); $isObserver = $data->isObserver(); + $l10n = $this->getParticipantL10n($data); - $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail'); + $emailTemplate = $this->createParticipantEmailTemplate($l10n); $this->applyParticipantNotificationHeader( $emailTemplate, + $l10n, $isObserver, // TRANSLATORS Email subject notifying an observer that a document is available to view. - $this->l10n->t('LibreSign: A document is ready to view'), + $l10n->t('LibreSign: A document is ready to view'), // TRANSLATORS Email subject notifying a signer that a document is ready for their digital signature. - $this->l10n->t('LibreSign: A document is ready for your signature'), + $l10n->t('LibreSign: A document is ready for your signature'), ); if (!empty($description)) { @@ -121,13 +144,14 @@ public function notifyUnsignedUser(SignRequest $data, string $email, ?string $de $this->applyParticipantNotificationAction( $emailTemplate, + $l10n, $data, $file, $isObserver, // TRANSLATORS Email body inviting an observer to open the document and view it. - $this->l10n->t('A document is ready to view. Open the link below:'), + $l10n->t('A document is ready to view. Open the link below:'), // TRANSLATORS Email body inviting the signer to open the document and sign it. - $this->l10n->t('A document is ready for your signature. Open the link below:'), + $l10n->t('A document is ready for your signature. Open the link below:'), ); try { $this->sendSignRequestNotification($emailTemplate, $data, $email); @@ -139,6 +163,7 @@ public function notifyUnsignedUser(SignRequest $data, string $email, ?string $de private function applyParticipantNotificationHeader( IEMailTemplate $emailTemplate, + IL10N $l10n, bool $isObserver, string $observerSubject, string $signerSubject, @@ -148,15 +173,16 @@ private function applyParticipantNotificationHeader( $emailTemplate->addHeading( $isObserver // TRANSLATORS Email heading shown above a document available for viewing. - ? $this->l10n->t('Document to view') + ? $l10n->t('Document to view') // TRANSLATORS Email heading shown above a document awaiting the recipient's signature. - : $this->l10n->t('Document to sign'), + : $l10n->t('Document to sign'), false, ); } private function applyParticipantNotificationAction( IEMailTemplate $emailTemplate, + IL10N $l10n, SignRequest $data, File $file, bool $isObserver, @@ -167,7 +193,7 @@ private function applyParticipantNotificationAction( if ($isObserver) { $emailTemplate->addBodyButton( // TRANSLATORS Email button label that opens the document validation view. %s is the document filename. - $this->l10n->t('View "%s"', [$file->getName()]), + $l10n->t('View "%s"', [$file->getName()]), $this->buildValidationLink($file), ); return; @@ -175,7 +201,7 @@ private function applyParticipantNotificationAction( $emailTemplate->addBodyButton( // TRANSLATORS Email button label that opens the signing page. %s is the document filename. - $this->l10n->t('Sign "%s"', [$file->getName()]), + $l10n->t('Sign "%s"', [$file->getName()]), $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $data->getUuid()]), ); } @@ -357,14 +383,15 @@ public function notifySignedUser(SignRequest $signRequest, string $email, File $ } public function notifyCanceledRequest(SignRequest $signRequest, string $email, File $libreSignFile): void { - $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail'); + $l10n = $this->getParticipantL10n($signRequest); + $emailTemplate = $this->createParticipantEmailTemplate($l10n); // TRANSLATORS Email subject shown when the requester cancels a pending signature request. - $emailTemplate->setSubject($this->l10n->t('LibreSign: A signature request has been canceled')); + $emailTemplate->setSubject($l10n->t('LibreSign: A signature request has been canceled')); $emailTemplate->addHeader(); // TRANSLATORS Email heading shown when a signature request is no longer active. - $emailTemplate->addHeading($this->l10n->t('Signature request canceled'), false); + $emailTemplate->addHeading($l10n->t('Signature request canceled'), false); // TRANSLATORS Email body text shown after cancellation. %s is the document filename that no longer needs a signature. - $emailTemplate->addBodyText($this->l10n->t('The request for you to sign "%s" has been canceled.', [$libreSignFile->getName()])); + $emailTemplate->addBodyText($l10n->t('The request for you to sign "%s" has been canceled.', [$libreSignFile->getName()])); $message = $this->mailer->createMessage(); if ($signRequest->getDisplayName()) { $message->setTo([$email => $signRequest->getDisplayName()]); diff --git a/lib/Service/RequestSignatureService.php b/lib/Service/RequestSignatureService.php index 484bf57c52..acaea127ab 100644 --- a/lib/Service/RequestSignatureService.php +++ b/lib/Service/RequestSignatureService.php @@ -26,6 +26,7 @@ use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod; use OCA\Libresign\Service\Policy\FilePolicyApplier; use OCA\Libresign\Service\SignerGeolocation\SignerGeolocationPolicyService; +use OCA\Libresign\Service\SignRequest\SignerLanguageService; use OCA\Libresign\Service\SignRequest\SignRequestService; use OCA\Libresign\Service\Validation\FileInputValidator; use OCA\Libresign\Service\Validation\SignerValidator; @@ -81,6 +82,7 @@ public function __construct( protected IDBConnection $db, protected NotificationDeliveryService $notifications, protected NativeWebhookEventPublisher $webhooks, + protected SignerLanguageService $signerLanguageService, ) { } @@ -613,6 +615,11 @@ private function associateToSigners(array $data, FileEntity $file): array { FILTER_NULL_ON_FAILURE, ) ?? false; } + // Absent key keeps the stored language; an empty value clears it. + $language = null; + if (array_key_exists('language', $signer)) { + $language = is_string($signer['language']) ? $signer['language'] : ''; + } foreach ($signer['identifyMethods'] as $identifyMethod) { $lastSignRequest = $this->signRequestService->createOrUpdateSignRequest( @@ -627,7 +634,10 @@ private function associateToSigners(array $data, FileEntity $file): array { fileStatus: $fileStatus, signerStatus: $signerStatus, participantRole: $participantRole, - afterPersist: function (SignRequestEntity $signRequest) use ($file, $requesterRequiresGeolocation): void { + afterPersist: function (SignRequestEntity $signRequest) use ($file, $requesterRequiresGeolocation, $language): void { + if ($language !== null) { + $this->signerLanguageService->setLanguage($signRequest, $language); + } $requiresGeolocation = $requesterRequiresGeolocation; if ($requiresGeolocation === null) { if ($this->signerGeolocationPolicyService->getFrozenRequirement($signRequest) !== null) { diff --git a/lib/Service/SignRequest/SignerLanguageService.php b/lib/Service/SignRequest/SignerLanguageService.php new file mode 100644 index 0000000000..29d0c91552 --- /dev/null +++ b/lib/Service/SignRequest/SignerLanguageService.php @@ -0,0 +1,83 @@ + + */ + public function getAvailableLanguages(): array { + $languages = array_values(array_unique($this->l10nFactory->findAvailableLanguages(Application::APP_ID))); + sort($languages); + return $languages; + } + + public function isAvailable(string $language): bool { + return $this->l10nFactory->languageExists(Application::APP_ID, $language); + } + + public function getLanguage(SignRequest $signRequest): ?string { + $language = ($signRequest->getMetadata() ?? [])[self::METADATA_KEY] ?? null; + if (!is_string($language) || $language === '' || !$this->isAvailable($language)) { + return null; + } + return $language; + } + + /** + * Translations for the e-mails sent to the signer, or null to keep the default. + */ + public function getL10n(SignRequest $signRequest): ?IL10N { + $language = $this->getLanguage($signRequest); + return $language === null ? null : $this->l10nFactory->get(Application::APP_ID, $language); + } + + /** + * Stores the language of a persisted sign request. An empty value clears it. + */ + public function setLanguage(SignRequest $signRequest, string $language): void { + $metadata = $signRequest->getMetadata() ?? []; + $current = $metadata[self::METADATA_KEY] ?? null; + if ($language === '') { + if ($current === null) { + return; + } + unset($metadata[self::METADATA_KEY]); + } else { + if ($current === $language) { + return; + } + $metadata[self::METADATA_KEY] = $language; + } + $signRequest->setMetadata($metadata); + $this->signRequestMapper->update($signRequest); + } +} diff --git a/lib/Service/Validation/SignerValidator.php b/lib/Service/Validation/SignerValidator.php index f945ff0f2a..dd9cd8d61b 100644 --- a/lib/Service/Validation/SignerValidator.php +++ b/lib/Service/Validation/SignerValidator.php @@ -24,6 +24,7 @@ use OCA\Libresign\Service\IdentifyMethodService; use OCA\Libresign\Service\Policy\Provider\ObserverProfile\ObserverProfilePolicyService; use OCA\Libresign\Service\SequentialSigningService; +use OCA\Libresign\Service\SignRequest\SignerLanguageService; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; use OCP\IL10N; @@ -40,6 +41,7 @@ public function __construct( private DocMdpValidator $docMdpValidator, private RuntimeRequirementValidator $runtimeRequirementValidator, private ObserverProfilePolicyService $observerProfilePolicyService, + private SignerLanguageService $signerLanguageService, ) { } @@ -61,6 +63,7 @@ public function validateIdentifySigners(array $data): void { throw new LibresignException('Display name must not be longer than 64 characters'); } $this->validateParticipantRole($signer, $data); + $this->validateLanguage($signer); foreach ($this->normalizeSignerIdentifyMethods($signer) as $method) { $this->validateIdentifyMethodForRequest($method['name'], $method['value']); } @@ -254,6 +257,20 @@ private function validateParticipantRole(array $signer, array $data): void { } } + private function validateLanguage(array $signer): void { + if (!array_key_exists('language', $signer)) { + return; + } + $language = $signer['language']; + if ($language === null || $language === '') { + return; + } + if (!is_string($language) || !$this->signerLanguageService->isAvailable($language)) { + // TRANSLATORS Validation error when the language chosen for a signer's e-mails is not available. + throw new LibresignException($this->l10n->t('Invalid language')); + } + } + private function getExistingRequestFile(array $data): ?File { $uuid = $data['uuid'] ?? null; if (!is_string($uuid) || $uuid === '') { diff --git a/openapi-full.json b/openapi-full.json index c7c79e3450..ccff750421 100644 --- a/openapi-full.json +++ b/openapi-full.json @@ -2126,6 +2126,9 @@ }, "deviceGeolocationRequired": { "type": "boolean" + }, + "language": { + "type": "string" } } }, @@ -3395,6 +3398,9 @@ }, "certificate_info": { "$ref": "#/components/schemas/SignerCertificateInfo" + }, + "language": { + "type": "string" } } }, @@ -12248,7 +12254,7 @@ "signers": { "type": "array", "default": [], - "description": "Collection of signers who must sign the document. Use identifyMethods as the canonical format. Other supported fields: displayName, description, notify, signingOrder, status, deviceGeolocationRequired", + "description": "Collection of signers who must sign the document. Use identifyMethods as the canonical format. Other supported fields: displayName, description, notify, signingOrder, status, deviceGeolocationRequired, language (language code for the e-mails to the signer; empty uses the default)", "items": { "$ref": "#/components/schemas/NewSigner" } diff --git a/openapi.json b/openapi.json index 6b4e635e67..abc7e979ea 100644 --- a/openapi.json +++ b/openapi.json @@ -1711,6 +1711,9 @@ }, "deviceGeolocationRequired": { "type": "boolean" + }, + "language": { + "type": "string" } } }, @@ -2921,6 +2924,9 @@ }, "certificate_info": { "$ref": "#/components/schemas/SignerCertificateInfo" + }, + "language": { + "type": "string" } } }, @@ -11511,7 +11517,7 @@ "signers": { "type": "array", "default": [], - "description": "Collection of signers who must sign the document. Use identifyMethods as the canonical format. Other supported fields: displayName, description, notify, signingOrder, status, deviceGeolocationRequired", + "description": "Collection of signers who must sign the document. Use identifyMethods as the canonical format. Other supported fields: displayName, description, notify, signingOrder, status, deviceGeolocationRequired, language (language code for the e-mails to the signer; empty uses the default)", "items": { "$ref": "#/components/schemas/NewSigner" } diff --git a/src/components/Request/IdentifySigner.vue b/src/components/Request/IdentifySigner.vue index 461325c665..4d0dfb597b 100644 --- a/src/components/Request/IdentifySigner.vue +++ b/src/components/Request/IdentifySigner.vue @@ -49,6 +49,16 @@ resize="none" /> +
+ +
+
@@ -74,7 +84,8 @@