Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +85,7 @@ public function __construct(
private IEventDispatcher $eventDispatcher,
private IURLGenerator $urlGenerator,
private EmailSignerElementsService $emailSignerElementsService,
private SignerLanguageService $signerLanguageService,
) {
parent::__construct(
request: $request,
Expand Down Expand Up @@ -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', [
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/RequestSignatureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<LibresignNewSigner> $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<LibresignNewSigner> $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.
Expand Down
3 changes: 3 additions & 0 deletions lib/Files/TemplateLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +39,7 @@ public function __construct(
private PolicyService $policyService,
private IAppManager $appManager,
private TemplateLoaderAssets $assets,
private SignerLanguageService $signerLanguageService,
) {
}

Expand Down Expand Up @@ -71,6 +73,7 @@ protected function getInitialStatePayload(): array {
'policies' => $this->policyService->resolveKnownPolicyStates(),
],
'can_request_sign' => $this->canRequestSign(),
'signer_languages' => $this->signerLanguageService->getAvailableLanguages(),
];
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
* status?: int,
* participantRole?: LibresignParticipantRole,
* deviceGeolocationRequired?: bool,
* language?: string,
* }
* @psalm-type LibresignNewFile = array{
* base64?: string,
Expand Down Expand Up @@ -231,6 +232,7 @@
* geolocation?: LibresignSignerGeolocation,
* notify?: list<LibresignNotify>,
* certificate_info?: LibresignSignerCertificateInfo,
* language?: string,
* }
* @psalm-type LibresignSignerDisplayStatus = 'draft'|'ready_to_sign'|'signed'|'rejected'|'observing'|'not_signed'
* @psalm-type LibresignSignerSummary = array{
Expand Down
63 changes: 45 additions & 18 deletions lib/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function __construct(
private PolicyService $policyService,
private IMailProviderManager $mailProviderManager,
private IUserManager $userManager,
private SignerLanguageService $signerLanguageService,
) {
}

Expand All @@ -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)) {
Expand All @@ -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);
Expand All @@ -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)) {
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -167,15 +193,15 @@ 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;
}

$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()]),
);
}
Expand Down Expand Up @@ -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()]);
Expand Down
12 changes: 11 additions & 1 deletion lib/Service/RequestSignatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,6 +82,7 @@ public function __construct(
protected IDBConnection $db,
protected NotificationDeliveryService $notifications,
protected NativeWebhookEventPublisher $webhooks,
protected SignerLanguageService $signerLanguageService,
) {
}

Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
83 changes: 83 additions & 0 deletions lib/Service/SignRequest/SignerLanguageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Service\SignRequest;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Db\SignRequestMapper;
use OCP\IL10N;
use OCP\L10N\IFactory;

/**
* Language the requester picked for the e-mails sent to one signer.
*
* The choice is stored in the sign request metadata. Without a choice the
* e-mails keep using the default language of the instance.
*/
class SignerLanguageService {
public const METADATA_KEY = 'language';

public function __construct(
private IFactory $l10nFactory,
private SignRequestMapper $signRequestMapper,
) {
}

/**
* Language codes LibreSign ships translations for.
*
* @return list<string>
*/
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);
}
}
Loading