|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Controller; |
| 10 | + |
| 11 | +use OCA\Libresign\AppInfo\Application; |
| 12 | +use OCA\Libresign\Service\CertificatePolicyService; |
| 13 | +use OCP\AppFramework\Controller; |
| 14 | +use OCP\AppFramework\Http; |
| 15 | +use OCP\AppFramework\Http\Attribute\AnonRateLimit; |
| 16 | +use OCP\AppFramework\Http\Attribute\FrontpageRoute; |
| 17 | +use OCP\AppFramework\Http\Attribute\PublicPage; |
| 18 | +use OCP\AppFramework\Http\DataResponse; |
| 19 | +use OCP\AppFramework\Http\FileDisplayResponse; |
| 20 | +use OCP\IL10N; |
| 21 | +use OCP\IRequest; |
| 22 | + |
| 23 | +class CertificatePolicyController extends Controller { |
| 24 | + public function __construct( |
| 25 | + IRequest $request, |
| 26 | + private CertificatePolicyService $certificatePolicyService, |
| 27 | + private IL10N $l10n, |
| 28 | + ) { |
| 29 | + parent::__construct(Application::APP_ID, $request); |
| 30 | + } |
| 31 | + |
| 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 | + |
| 88 | + /** |
| 89 | + * Certificate policy of this instance |
| 90 | + * |
| 91 | + * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Disposition: 'inline; filename="certificate-policy.pdf"', Content-Type: 'application/pdf'}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}> |
| 92 | + * |
| 93 | + * 200: OK |
| 94 | + * 404: Not found |
| 95 | + */ |
| 96 | + #[PublicPage] |
| 97 | + #[AnonRateLimit(limit: 10, period: 60)] |
| 98 | + #[FrontpageRoute(verb: 'GET', url: '/certificate-policy.pdf')] |
| 99 | + public function getCertificatePolicy(): FileDisplayResponse { |
| 100 | + $file = $this->certificatePolicyService->getFile(); |
| 101 | + return new FileDisplayResponse($file, Http::STATUS_OK, [ |
| 102 | + 'Content-Disposition' => 'inline; filename="certificate-policy.pdf"', |
| 103 | + 'Content-Type' => 'application/pdf', |
| 104 | + ]); |
| 105 | + } |
| 106 | +} |
0 commit comments