Skip to content

Commit 7b5991e

Browse files
committed
chore: Implement administration settings of Certificate Policy
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent 9d1060b commit 7b5991e

13 files changed

+1092
-71
lines changed

lib/Controller/AdminController.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\Libresign\Handler\CertificateEngine\IEngineHandler;
1515
use OCA\Libresign\Helper\ConfigureCheckHelper;
1616
use OCA\Libresign\ResponseDefinitions;
17+
use OCA\Libresign\Service\CertificatePolicyService;
1718
use OCA\Libresign\Service\Install\ConfigureCheckService;
1819
use OCA\Libresign\Service\Install\InstallService;
1920
use OCA\Libresign\Service\SignatureBackgroundService;
@@ -51,6 +52,7 @@ public function __construct(
5152
private IL10N $l10n,
5253
protected ISession $session,
5354
private SignatureBackgroundService $signatureBackgroundService,
55+
private CertificatePolicyService $certificatePolicyService,
5456
) {
5557
parent::__construct(Application::APP_ID, $request);
5658
$this->eventSource = $this->eventSourceFactory->create();
@@ -525,4 +527,72 @@ public function signerName(
525527
);
526528
}
527529
}
530+
531+
/**
532+
* Update certificate policy of this instance
533+
*
534+
* @return DataResponse<Http::STATUS_OK, array{status: 'success', url: string}, array{}>|DataResponse<Http::STATUS_UNPROCESSABLE_ENTITY, array{status: 'failure', message: string}, array{}>
535+
*
536+
* 200: OK
537+
* 422: Not found
538+
*/
539+
#[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/admin/certificate-policy', requirements: ['apiVersion' => '(v1)'])]
540+
public function saveCertificatePolicy(): DataResponse {
541+
$pdf = $this->request->getUploadedFile('pdf');
542+
$phpFileUploadErrors = [
543+
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
544+
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
545+
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
546+
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
547+
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
548+
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
549+
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
550+
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
551+
];
552+
if (empty($pdf)) {
553+
$error = $this->l10n->t('No file uploaded');
554+
} elseif (!empty($pdf) && array_key_exists('error', $pdf) && $pdf['error'] !== UPLOAD_ERR_OK) {
555+
$error = $phpFileUploadErrors[$pdf['error']];
556+
}
557+
if ($error !== null) {
558+
return new DataResponse(
559+
[
560+
'message' => $error,
561+
'status' => 'failure',
562+
],
563+
Http::STATUS_UNPROCESSABLE_ENTITY
564+
);
565+
}
566+
try {
567+
$url = $this->certificatePolicyService->updateFile($pdf['tmp_name']);
568+
} catch (\Exception $e) {
569+
return new DataResponse(
570+
[
571+
'message' => $e->getMessage(),
572+
'status' => 'failure',
573+
],
574+
Http::STATUS_UNPROCESSABLE_ENTITY
575+
);
576+
}
577+
return new DataResponse(
578+
[
579+
'url' => $url,
580+
'status' => 'success',
581+
]
582+
);
583+
}
584+
585+
/**
586+
* Delete certificate policy of this instance
587+
*
588+
* @return DataResponse<Http::STATUS_OK, array{}, array{}>
589+
*
590+
* 200: OK
591+
* 404: Not found
592+
*/
593+
#[ApiRoute(verb: 'DELETE', url: '/api/{apiVersion}/admin/certificate-policy', requirements: ['apiVersion' => '(v1)'])]
594+
public function deleteCertificatePolicy(): DataResponse {
595+
$this->certificatePolicyService->deleteFile();
596+
return new DataResponse();
597+
}
528598
}

lib/Controller/CertificatePolicyController.php

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,77 +14,20 @@
1414
use OCP\AppFramework\Http;
1515
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
1616
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
17+
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
1718
use OCP\AppFramework\Http\Attribute\PublicPage;
1819
use OCP\AppFramework\Http\DataResponse;
1920
use OCP\AppFramework\Http\FileDisplayResponse;
20-
use OCP\IL10N;
2121
use OCP\IRequest;
2222

2323
class CertificatePolicyController extends Controller {
2424
public function __construct(
2525
IRequest $request,
2626
private CertificatePolicyService $certificatePolicyService,
27-
private IL10N $l10n,
2827
) {
2928
parent::__construct(Application::APP_ID, $request);
3029
}
3130

32-
/**
33-
* Update certificate policy of this instance
34-
*
35-
* @return DataResponse<Http::STATUS_OK, array{status: 'success'}, array{}>|DataResponse<Http::STATUS_UNPROCESSABLE_ENTITY, array{status: 'failure', message: string}, array{}>
36-
*
37-
* 200: OK
38-
* 404: Not found
39-
*/
40-
#[PublicPage]
41-
#[AnonRateLimit(limit: 10, period: 60)]
42-
#[FrontpageRoute(verb: 'POST', url: '/certificate-policy.pdf')]
43-
public function saveCertificatePolicy(): DataResponse {
44-
$pdf = $this->request->getUploadedFile('pdf');
45-
$phpFileUploadErrors = [
46-
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
47-
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
48-
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
49-
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
50-
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
51-
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
52-
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
53-
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
54-
];
55-
if (empty($pdf)) {
56-
$error = $this->l10n->t('No file uploaded');
57-
} elseif (!empty($pdf) && array_key_exists('error', $pdf) && $pdf['error'] !== UPLOAD_ERR_OK) {
58-
$error = $phpFileUploadErrors[$pdf['error']];
59-
}
60-
if ($error !== null) {
61-
return new DataResponse(
62-
[
63-
'message' => $error,
64-
'status' => 'failure',
65-
],
66-
Http::STATUS_UNPROCESSABLE_ENTITY
67-
);
68-
}
69-
try {
70-
$this->certificatePolicyService->updateFile($pdf['tmp_name']);
71-
} catch (\Exception $e) {
72-
return new DataResponse(
73-
[
74-
'message' => $e->getMessage(),
75-
'status' => 'failure',
76-
],
77-
Http::STATUS_UNPROCESSABLE_ENTITY
78-
);
79-
}
80-
81-
return new DataResponse(
82-
[
83-
'status' => 'success',
84-
]
85-
);
86-
}
87-
8831
/**
8932
* Certificate policy of this instance
9033
*
@@ -94,6 +37,7 @@ public function saveCertificatePolicy(): DataResponse {
9437
* 404: Not found
9538
*/
9639
#[PublicPage]
40+
#[NoCSRFRequired]
9741
#[AnonRateLimit(limit: 10, period: 60)]
9842
#[FrontpageRoute(verb: 'GET', url: '/certificate-policy.pdf')]
9943
public function getCertificatePolicy(): FileDisplayResponse {

lib/Service/CertificatePolicyService.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
use OCP\Files\IAppData;
1212
use OCP\Files\NotFoundException;
1313
use OCP\Files\SimpleFS\ISimpleFile;
14+
use OCP\IURLGenerator;
1415

1516
class CertificatePolicyService {
1617
public function __construct(
1718
private IAppData $appData,
19+
private IURLGenerator $urlGenerator,
1820
) {
1921
}
2022

21-
public function updateFile(string $tmpFile): void {
23+
public function updateFile(string $tmpFile): string {
2224
$detectedMimeType = mime_content_type($tmpFile);
2325
if (!in_array($detectedMimeType, ['application/pdf'], true)) {
2426
throw new \Exception('Unsupported image type: ' . $detectedMimeType);
@@ -32,13 +34,30 @@ public function updateFile(string $tmpFile): void {
3234
$file = $rootFolder->getFile('certificate-policy.pdf');
3335
$file->putContent($blob);
3436
}
37+
return $this->urlGenerator->linkToRouteAbsolute('libresign.CertificatePolicy.getCertificatePolicy');
3538
}
3639

3740
public function getFile(): ISimpleFile {
3841
return $this->appData->getFolder('/')->getFile('certificate-policy.pdf');
3942
}
4043

4144
public function deleteFile(): void {
42-
$this->appData->getFolder('/')->getFile('certificate-policy.pdf')->delete();
45+
try {
46+
$this->appData->getFolder('/')->getFile('certificate-policy.pdf')->delete();
47+
} catch (NotFoundException $e) {
48+
}
49+
}
50+
51+
public function getOid(): string {
52+
return '';
53+
}
54+
55+
public function getUrl(): string {
56+
try {
57+
$this->getFile();
58+
} catch (NotFoundException $e) {
59+
return '';
60+
}
61+
return $this->urlGenerator->linkToRouteAbsolute('libresign.CertificatePolicy.getCertificatePolicy');
4362
}
4463
}

lib/Settings/Admin.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCA\Libresign\AppInfo\Application;
1212
use OCA\Libresign\Exception\LibresignException;
1313
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
14+
use OCA\Libresign\Service\CertificatePolicyService;
1415
use OCA\Libresign\Service\IdentifyMethodService;
1516
use OCA\Libresign\Service\SignatureBackgroundService;
1617
use OCA\Libresign\Service\SignatureTextService;
@@ -25,6 +26,7 @@ public function __construct(
2526
private IInitialState $initialState,
2627
private IdentifyMethodService $identifyMethodService,
2728
private CertificateEngineFactory $certificateEngineFactory,
29+
private CertificatePolicyService $certificatePolicyService,
2830
private IAppConfig $appConfig,
2931
private SignatureTextService $signatureTextService,
3032
private SignatureBackgroundService $signatureBackgroundService,
@@ -40,6 +42,8 @@ public function getForm(): TemplateResponse {
4042
$this->initialState->provideInitialState('signature_text_template_error', $e->getMessage());
4143
}
4244
$this->initialState->provideInitialState('certificate_engine', $this->certificateEngineFactory->getEngine()->getName());
45+
$this->initialState->provideInitialState('certificate_policies_oid', $this->certificatePolicyService->getOid());
46+
$this->initialState->provideInitialState('certificate_policies_url', $this->certificatePolicyService->getUrl());
4347
$this->initialState->provideInitialState('config_path', $this->appConfig->getValueString(Application::APP_ID, 'config_path'));
4448
$this->initialState->provideInitialState('default_signature_font_size', SignatureTextService::SIGNATURE_DEFAULT_FONT_SIZE);
4549
$this->initialState->provideInitialState('default_signature_height', SignatureTextService::DEFAULT_SIGNATURE_HEIGHT);

0 commit comments

Comments
 (0)