Skip to content
Merged
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
73 changes: 73 additions & 0 deletions lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCA\Libresign\ResponseDefinitions;
use OCA\Libresign\Service\Certificate\ValidateService;
use OCA\Libresign\Service\CertificatePolicyService;
use OCA\Libresign\Service\FooterService;
use OCA\Libresign\Service\Install\ConfigureCheckService;
use OCA\Libresign\Service\Install\InstallService;
use OCA\Libresign\Service\ReminderService;
Expand All @@ -27,6 +28,7 @@
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\Files\SimpleFS\InMemoryFile;
Expand Down Expand Up @@ -61,6 +63,7 @@ public function __construct(
private CertificatePolicyService $certificatePolicyService,
private ValidateService $validateService,
private ReminderService $reminderService,
private FooterService $footerService,
) {
parent::__construct(Application::APP_ID, $request);
$this->eventSource = $this->eventSourceFactory->create();
Expand Down Expand Up @@ -782,4 +785,74 @@ public function deleteTsaConfig(): DataResponse {

return new DataResponse(['status' => 'success']);
}

/**
* Get footer template
*
* Returns the current footer template if set, otherwise returns the default template.
*
* @return DataResponse<Http::STATUS_OK, array{template: string, isDefault: bool}, array{}>
*
* 200: OK
*/
#[ApiRoute(verb: 'GET', url: '/api/{apiVersion}/admin/footer-template', requirements: ['apiVersion' => '(v1)'])]
public function getFooterTemplate(): DataResponse {
return new DataResponse([
'template' => $this->footerService->getTemplate(),
'isDefault' => $this->footerService->isDefaultTemplate(),
]);
}

/**
* Save footer template and render preview
*
* Saves the footer template and returns the rendered PDF preview.
*
* @param string $template The Twig template to save
* @param int $width Width of preview in points (default: 595 - A4 width)
* @param int $height Height of preview in points (default: 50)
* @return DataDownloadResponse<Http::STATUS_OK, 'application/pdf', array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{error: string}, array{}>
*
* 200: OK
* 400: Bad request
*/
#[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/admin/footer-template', requirements: ['apiVersion' => '(v1)'])]
public function saveFooterTemplate(string $template, int $width = 595, int $height = 50) {
try {
$this->footerService->saveTemplate($template);
$pdf = $this->footerService->renderPreviewPdf(null, $width, $height);

return new DataDownloadResponse($pdf, 'footer-preview.pdf', 'application/pdf');
} catch (\Exception $e) {
return new DataResponse([
'error' => $e->getMessage(),
], Http::STATUS_BAD_REQUEST);
}
}

/**
* Preview footer template as PDF
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $template Template to preview
* @param int $width Width of preview in points (default: 595 - A4 width)
* @param int $height Height of preview in points (default: 50)
* @return DataDownloadResponse<Http::STATUS_OK, 'application/pdf', array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{error: string}, array{}>
*
* 200: OK
* 400: Bad request
*/
#[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/admin/footer-template/preview-pdf', requirements: ['apiVersion' => '(v1)'])]
public function footerTemplatePreviewPdf(string $template = '', int $width = 595, int $height = 50) {
try {
$pdf = $this->footerService->renderPreviewPdf($template ?: null, $width, $height);
return new DataDownloadResponse($pdf, 'footer-preview.pdf', 'application/pdf');
} catch (\Exception $e) {
return new DataResponse([
'error' => $e->getMessage(),
], Http::STATUS_BAD_REQUEST);
}
}
}
50 changes: 50 additions & 0 deletions lib/Service/FooterService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

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

namespace OCA\Libresign\Service;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Handler\FooterHandler;
use OCP\IAppConfig;

class FooterService {
public function __construct(
private IAppConfig $appConfig,
private FooterHandler $footerHandler,
) {
}

public function isDefaultTemplate(): bool {
$customTemplate = $this->appConfig->getValueString(Application::APP_ID, 'footer_template', '');
return empty($customTemplate);
}

public function getTemplate(): string {
return $this->appConfig->getValueString(Application::APP_ID, 'footer_template', '');
}

public function saveTemplate(string $template): void {
$this->appConfig->setValueString(Application::APP_ID, 'footer_template', $template);
}

public function renderPreviewPdf(?string $template = null, int $width = 595, int $height = 50): string {
if ($template !== null) {
$this->saveTemplate($template);
}

return $this->footerHandler
->setTemplateVar('uuid', 'preview-' . bin2hex(random_bytes(8)))
->setTemplateVar('signers', [
[
'displayName' => 'Preview Signer',
'signed' => date('c'),
],
])
->getFooter([['w' => $width, 'h' => $height]]);
}
}
208 changes: 208 additions & 0 deletions openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -2599,6 +2599,214 @@
}
}
},
"/ocs/v2.php/apps/libresign/api/{apiVersion}/admin/footer-template": {
"get": {
"operationId": "admin-get-footer-template",
"summary": "Get footer template",
"description": "Returns the current footer template if set, otherwise returns the default template.\nThis endpoint requires admin access",
"tags": [
"admin"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v1"
],
"default": "v1"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"template",
"isDefault"
],
"properties": {
"template": {
"type": "string"
},
"isDefault": {
"type": "boolean"
}
}
}
}
}
}
}
}
}
}
}
},
"post": {
"operationId": "admin-save-footer-template",
"summary": "Save footer template and render preview",
"description": "Saves the footer template and returns the rendered PDF preview.\nThis endpoint requires admin access",
"tags": [
"admin"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"template"
],
"properties": {
"template": {
"type": "string",
"description": "The Twig template to save"
},
"width": {
"type": "integer",
"format": "int64",
"default": 595,
"description": "Width of preview in points (default: 595 - A4 width)"
},
"height": {
"type": "integer",
"format": "int64",
"default": 50,
"description": "Height of preview in points (default: 50)"
}
}
}
}
}
},
"parameters": [
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v1"
],
"default": "v1"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/pdf": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
"400": {
"description": "Bad request",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"error"
],
"properties": {
"error": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/apps/libresign/api/{apiVersion}/crl/list": {
"get": {
"operationId": "crl_api-list",
Expand Down
Loading
Loading