Resolve merge with master

This commit is contained in:
2024-12-11 10:46:06 +01:00
667 changed files with 37245 additions and 7119 deletions

View File

@@ -1,97 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\WopiBundle\Controller;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Service\StoredObjectManager;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\MainBundle\Entity\User;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class Convert
{
private const LOG_PREFIX = '[convert] ';
private readonly string $collaboraDomain;
/**
* @param StoredObjectManager $storedObjectManager
*/
public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly RequestStack $requestStack,
private readonly Security $security,
private readonly StoredObjectManagerInterface $storedObjectManager,
private readonly LoggerInterface $logger,
ParameterBagInterface $parameters,
) {
$this->collaboraDomain = $parameters->get('wopi')['server'];
}
public function __invoke(StoredObject $storedObject): Response
{
if (!$this->security->getUser() instanceof User) {
throw new AccessDeniedHttpException('User must be authenticated');
}
$content = $this->storedObjectManager->read($storedObject);
$query = [];
if (null !== $request = $this->requestStack->getCurrentRequest()) {
$query['lang'] = $request->getLocale();
}
try {
$url = sprintf('%s/cool/convert-to/pdf', $this->collaboraDomain);
$form = new FormDataPart([
'data' => new DataPart($content, $storedObject->getUuid()->toString(), $storedObject->getType()),
]);
$response = $this->httpClient->request('POST', $url, [
'headers' => $form->getPreparedHeaders()->toArray(),
'query' => $query,
'body' => $form->bodyToString(),
'timeout' => 10,
]);
return new Response($response->getContent(), Response::HTTP_OK, [
'Content-Type' => 'application/pdf',
]);
} catch (ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $exception) {
return $this->onConversionFailed($url, $exception->getResponse());
}
}
private function onConversionFailed(string $url, ResponseInterface $response): JsonResponse
{
$this->logger->error(self::LOG_PREFIX.' could not convert document', [
'response_status' => $response->getStatusCode(),
'message' => $response->getContent(false),
'server' => $this->collaboraDomain,
'url' => $url,
]);
return new JsonResponse(['message' => 'conversion failed : '.$response->getContent(false)], Response::HTTP_SERVICE_UNAVAILABLE);
}
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\WopiBundle\Controller;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\DocStoreBundle\Service\StoredObjectManager;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\WopiBundle\Service\WopiConverter;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
class ConvertController
{
private const LOG_PREFIX = '[convert] ';
/**
* @param StoredObjectManager $storedObjectManager
*/
public function __construct(
private readonly Security $security,
private readonly StoredObjectManagerInterface $storedObjectManager,
private readonly WopiConverter $wopiConverter,
private readonly LoggerInterface $logger,
) {}
public function __invoke(StoredObject $storedObject, Request $request): Response
{
if (!($this->security->isGranted('ROLE_USER') || $this->security->isGranted('ROLE_ADMIN'))) {
throw new AccessDeniedHttpException('User must be authenticated');
}
if (!$this->security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)) {
throw new AccessDeniedHttpException('not allowed to see this document');
}
$content = $this->storedObjectManager->read($storedObject);
if ('application/pdf' === $storedObject->getType()) {
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'application/pdf']);
}
$lang = $request->getLocale();
try {
return new Response($this->wopiConverter->convert($lang, $content, $storedObject->getType()), Response::HTTP_OK, [
'Content-Type' => 'application/pdf',
]);
} catch (\RuntimeException $exception) {
$this->logger->alert(self::LOG_PREFIX.'Could not convert document', ['message' => $exception->getMessage(), 'exception', $exception->getTraceAsString()]);
return new Response('convert server not available', Response::HTTP_SERVICE_UNAVAILABLE);
}
}
}

View File

@@ -19,5 +19,5 @@ return static function (RoutingConfigurator $routes) {
$routes
->add('chill_wopi_object_convert', '/convert/{uuid}')
->controller(Chill\WopiBundle\Controller\Convert::class);
->controller(Chill\WopiBundle\Controller\ConvertController::class);
};

View File

@@ -12,6 +12,8 @@ declare(strict_types=1);
namespace Chill\WopiBundle\Service\Wopi;
use ChampsLibres\WopiLib\Contract\Entity\Document;
use Chill\DocStoreBundle\Repository\StoredObjectRepository;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\MainBundle\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Psr\Http\Message\RequestInterface;
@@ -19,13 +21,18 @@ use Symfony\Component\Security\Core\Security;
class AuthorizationManager implements \ChampsLibres\WopiBundle\Contracts\AuthorizationManagerInterface
{
public function __construct(private readonly JWTTokenManagerInterface $tokenManager, private readonly Security $security) {}
public function __construct(private readonly JWTTokenManagerInterface $tokenManager, private readonly Security $security, private readonly StoredObjectRepository $storedObjectRepository) {}
public function isRestrictedWebViewOnly(string $accessToken, Document $document, RequestInterface $request): bool
{
return false;
}
public function getRelatedStoredObject(Document $document)
{
return $this->storedObjectRepository->findOneBy(['uuid' => $document->getWopiDocId()]);
}
public function isTokenValid(string $accessToken, Document $document, RequestInterface $request): bool
{
$metadata = $this->tokenManager->parse($accessToken);
@@ -60,12 +67,23 @@ class AuthorizationManager implements \ChampsLibres\WopiBundle\Contracts\Authori
public function userCanPresent(string $accessToken, Document $document, RequestInterface $request): bool
{
return $this->isTokenValid($accessToken, $document, $request);
$storedObject = $this->getRelatedStoredObject($document);
if ($this->security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)) {
return $this->isTokenValid($accessToken, $document, $request);
}
return false;
}
public function userCanRead(string $accessToken, Document $document, RequestInterface $request): bool
{
return $this->isTokenValid($accessToken, $document, $request);
$storedObject = $this->getRelatedStoredObject($document);
if ($this->security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)) {
return $this->isTokenValid($accessToken, $document, $request);
}
return false;
}
public function userCanRename(string $accessToken, Document $document, RequestInterface $request): bool
@@ -75,6 +93,12 @@ class AuthorizationManager implements \ChampsLibres\WopiBundle\Contracts\Authori
public function userCanWrite(string $accessToken, Document $document, RequestInterface $request): bool
{
return $this->isTokenValid($accessToken, $document, $request);
$storedObject = $this->getRelatedStoredObject($document);
if ($this->security->isGranted(StoredObjectRoleEnum::EDIT->value, $storedObject)) {
return $this->isTokenValid($accessToken, $document, $request);
}
return false;
}
}

View File

@@ -50,17 +50,15 @@ final readonly class ChillDocumentManager implements DocumentManagerInterface
// Mime types / extension handling.
$mimeTypes = new MimeTypes();
$mimeTypes->getMimeTypes($data['extension']);
$document->setType(reset($mimeTypes));
$document->setFilename($data['name']);
$types = $mimeTypes->getMimeTypes($data['extension']);
$mimeType = array_values($types)[0] ?? '';
$this->entityManager->persist($document);
$this->entityManager->flush();
// TODO : Ask proper mapping.
// Available: basename, name, extension, content, size
$this->setContent($document, $data['content']);
$this->storedObjectManager->write($document, $data['content'], $mimeType);
return $document;
}
@@ -106,7 +104,7 @@ final readonly class ChillDocumentManager implements DocumentManagerInterface
throw new \Error('Unknown mimetype for stored document.');
}
return sprintf('%s.%s', $document->getFilename(), reset($exts));
return sprintf('%s.%s', $document->getPrefix(), reset($exts));
}
/**
@@ -194,5 +192,7 @@ final readonly class ChillDocumentManager implements DocumentManagerInterface
private function setContent(StoredObject $storedObject, string $content): void
{
$this->storedObjectManager->write($storedObject, $content);
$this->entityManager->flush();
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\WopiBundle\Service\Wopi;
use ChampsLibres\WopiLib\Contract\Service\ProofValidatorInterface;
use Psr\Http\Message\RequestInterface;
/**
* Create a proof validator for WOPI services which always return true.
*
* Useful for debugging purposes.
*/
final readonly class NullProofValidator implements ProofValidatorInterface
{
public function isValid(RequestInterface $request): bool
{
return true;
}
}

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\WopiBundle\Service;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Handles the conversion of documents to PDF using the Collabora Online server.
*/
class WopiConverter
{
private readonly string $collaboraDomain;
private const LOG_PREFIX = '[WopiConverterPDF] ';
public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly LoggerInterface $logger,
ParameterBagInterface $parameters,
) {
$this->collaboraDomain = $parameters->get('wopi')['server'];
}
public function convert(string $lang, string $content, string $contentType, $convertTo = 'pdf'): string
{
try {
$url = sprintf('%s/cool/convert-to/%s', $this->collaboraDomain, $convertTo);
$form = new FormDataPart([
'data' => new DataPart($content, uniqid('temp-file-'), contentType: $contentType),
]);
$response = $this->httpClient->request('POST', $url, [
'headers' => $form->getPreparedHeaders()->toArray(),
'query' => ['lang' => $lang],
'body' => $form->bodyToString(),
'timeout' => 10,
]);
if (200 === $response->getStatusCode()) {
$this->logger->info(self::LOG_PREFIX.'document converted successfully', ['size' => strlen($content)]);
}
return $response->getContent();
} catch (ClientExceptionInterface $e) {
throw new \LogicException('no correct request to collabora online', previous: $e);
} catch (RedirectionExceptionInterface $e) {
throw new \RuntimeException('no redirection expected', previous: $e);
} catch (ServerExceptionInterface|TransportExceptionInterface $e) {
throw new \RuntimeException('error while converting document', previous: $e);
}
}
}

View File

@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\WopiBundle\Tests\Controller;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\WopiBundle\Controller\ConvertController;
use Chill\WopiBundle\Service\WopiConverter;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
/**
* @internal
*
* @coversNothing
*/
final class ConvertControllerTest extends TestCase
{
use ProphecyTrait;
public function testConversionFailed(): void
{
$storedObject = new StoredObject();
$storedObject->registerVersion(type: 'application/vnd.oasis.opendocument.text');
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)->willReturn(true);
$storeManager = $this->prophesize(StoredObjectManagerInterface::class);
$storeManager->read($storedObject)->willReturn('content');
$wopiConverter = $this->prophesize(WopiConverter::class);
$wopiConverter->convert('fr', 'content', 'application/vnd.oasis.opendocument.text')
->willThrow(new \RuntimeException());
$controller = new ConvertController(
$security->reveal(),
$storeManager->reveal(),
$wopiConverter->reveal(),
new NullLogger(),
);
$request = new Request();
$request->setLocale('fr');
$response = $controller($storedObject, $request);
$this->assertNotEquals(200, $response->getStatusCode());
}
public function testEverythingWentFine(): void
{
$storedObject = new StoredObject();
$storedObject->registerVersion(type: 'application/vnd.oasis.opendocument.text');
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)->willReturn(true);
$storeManager = $this->prophesize(StoredObjectManagerInterface::class);
$storeManager->read($storedObject)->willReturn('content');
$wopiConverter = $this->prophesize(WopiConverter::class);
$wopiConverter->convert('fr', 'content', 'application/vnd.oasis.opendocument.text')
->willReturn('1234');
$controller = new ConvertController(
$security->reveal(),
$storeManager->reveal(),
$wopiConverter->reveal(),
new NullLogger(),
);
$request = new Request();
$request->setLocale('fr');
$response = $controller($storedObject, $request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('1234', $response->getContent());
}
}

View File

@@ -1,105 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\WopiBundle\Tests\Controller;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\MainBundle\Entity\User;
use Chill\WopiBundle\Controller\Convert;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
/**
* @internal
*
* @coversNothing
*/
final class ConvertTest extends TestCase
{
use ProphecyTrait;
public function testConversionFailed(): void
{
$storedObject = (new StoredObject())->setType('application/vnd.oasis.opendocument.text');
$httpClient = new MockHttpClient([
new MockResponse('not authorized', ['http_code' => 401]),
], 'http://collabora:9980');
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn(new User());
$storeManager = $this->prophesize(StoredObjectManagerInterface::class);
$storeManager->read($storedObject)->willReturn('content');
$parameterBag = new ParameterBag(['wopi' => ['server' => 'http://collabora:9980']]);
$convert = new Convert(
$httpClient,
$this->makeRequestStack(),
$security->reveal(),
$storeManager->reveal(),
new NullLogger(),
$parameterBag
);
$response = $convert($storedObject);
$this->assertNotEquals(200, $response->getStatusCode());
}
public function testEverythingWentFine(): void
{
$storedObject = (new StoredObject())->setType('application/vnd.oasis.opendocument.text');
$httpClient = new MockHttpClient([
new MockResponse('1234', ['http_code' => 200]),
], 'http://collabora:9980');
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn(new User());
$storeManager = $this->prophesize(StoredObjectManagerInterface::class);
$storeManager->read($storedObject)->willReturn('content');
$parameterBag = new ParameterBag(['wopi' => ['server' => 'http://collabora:9980']]);
$convert = new Convert(
$httpClient,
$this->makeRequestStack(),
$security->reveal(),
$storeManager->reveal(),
new NullLogger(),
$parameterBag
);
$response = $convert($storedObject);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('1234', $response->getContent());
}
private function makeRequestStack(): RequestStack
{
$requestStack = new RequestStack();
$requestStack->push(new Request());
return $requestStack;
}
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\WopiBundle\Tests\Service;
use Chill\WopiBundle\Service\WopiConverter;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* @internal
*
* @coversNothing
*/
class WopiConvertToPdfTest extends TestCase
{
/**
* @group collabora-integration
*/
public function testConvertToPdfWithRealServer(): void
{
$content = file_get_contents(__DIR__.'/fixtures/test-document.odt');
$client = HttpClient::create();
$parameters = new ParameterBag([
'wopi' => ['server' => $_ENV['EDITOR_SERVER']],
]);
$converter = new WopiConverter($client, new NullLogger(), $parameters);
$actual = $converter->convert('fr', $content, 'application/vnd.oasis.opendocument.text');
self::assertIsString($actual);
}
public function testConvertToPdfWithMock(): void
{
$httpClient = new MockHttpClient([
new MockResponse('1234', ['http_code' => 200]),
], 'http://collabora:9980');
$parameters = new ParameterBag([
'wopi' => ['server' => 'http://collabora:9980'],
]);
$converter = new WopiConverter($httpClient, new NullLogger(), $parameters);
$actual = $converter->convert('fr', 'content-string', 'application/vnd.oasis.opendocument.text');
self::assertEquals('1234', $actual);
}
}