From 20fed80763ddb15ea51f4fe3b0698b227b744940 Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Thu, 21 Nov 2024 09:01:34 +0100 Subject: [PATCH 1/7] Refactor RequestBody to OpenAPI namespace. --- src/Asset/Controller/Upload/AddController.php | 4 ++-- src/Asset/Controller/Upload/ReplaceController.php | 4 ++-- src/Asset/Controller/Upload/ZipController.php | 4 ++-- .../Attribute/Request/MultipartFormDataRequestBody.php} | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) rename src/{Asset/Attribute/Request/AddAssetRequestBody.php => OpenApi/Attribute/Request/MultipartFormDataRequestBody.php} (88%) diff --git a/src/Asset/Controller/Upload/AddController.php b/src/Asset/Controller/Upload/AddController.php index 3a908348c..dc30fcbf0 100644 --- a/src/Asset/Controller/Upload/AddController.php +++ b/src/Asset/Controller/Upload/AddController.php @@ -19,7 +19,6 @@ use League\Flysystem\FilesystemException; use OpenApi\Attributes\Post; use OpenApi\Attributes\Property; -use Pimcore\Bundle\StudioBackendBundle\Asset\Attribute\Request\AddAssetRequestBody; use Pimcore\Bundle\StudioBackendBundle\Asset\Service\UploadServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\AccessDeniedException; @@ -29,6 +28,7 @@ use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\UserNotFoundException; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Request\MultipartFormDataRequestBody; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\Content\IdJson; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; @@ -80,7 +80,7 @@ public function __construct( content: new IdJson('ID of created asset') )] #[IdParameter(type: ElementTypes::TYPE_ASSET, name: 'parentId')] - #[AddAssetRequestBody( + #[MultipartFormDataRequestBody( [ new Property( property: 'file', diff --git a/src/Asset/Controller/Upload/ReplaceController.php b/src/Asset/Controller/Upload/ReplaceController.php index aae41c7d6..07122dc55 100644 --- a/src/Asset/Controller/Upload/ReplaceController.php +++ b/src/Asset/Controller/Upload/ReplaceController.php @@ -18,7 +18,6 @@ use OpenApi\Attributes\Post; use OpenApi\Attributes\Property; -use Pimcore\Bundle\StudioBackendBundle\Asset\Attribute\Request\AddAssetRequestBody; use Pimcore\Bundle\StudioBackendBundle\Asset\Service\UploadServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\AccessDeniedException; @@ -29,6 +28,7 @@ use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\UserNotFoundException; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Request\MultipartFormDataRequestBody; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; @@ -77,7 +77,7 @@ public function __construct( description: 'asset_replace_success_response', )] #[IdParameter(type: ElementTypes::TYPE_ASSET)] - #[AddAssetRequestBody( + #[MultipartFormDataRequestBody( [ new Property( property: 'file', diff --git a/src/Asset/Controller/Upload/ZipController.php b/src/Asset/Controller/Upload/ZipController.php index 687b3aff5..a616ff595 100644 --- a/src/Asset/Controller/Upload/ZipController.php +++ b/src/Asset/Controller/Upload/ZipController.php @@ -18,7 +18,6 @@ use OpenApi\Attributes\Post; use OpenApi\Attributes\Property; -use Pimcore\Bundle\StudioBackendBundle\Asset\Attribute\Request\AddAssetRequestBody; use Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\ZipServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\AccessDeniedException; @@ -27,6 +26,7 @@ use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\UserNotFoundException; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Request\MultipartFormDataRequestBody; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\Content\IdJson; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\CreatedResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; @@ -78,7 +78,7 @@ public function __construct( content: new IdJson('ID of created jobRun', 'jobRunId') )] #[IdParameter(type: ElementTypes::TYPE_ASSET, name: 'parentId')] - #[AddAssetRequestBody( + #[MultipartFormDataRequestBody( [ new Property( property: self::FILE_KEY, diff --git a/src/Asset/Attribute/Request/AddAssetRequestBody.php b/src/OpenApi/Attribute/Request/MultipartFormDataRequestBody.php similarity index 88% rename from src/Asset/Attribute/Request/AddAssetRequestBody.php rename to src/OpenApi/Attribute/Request/MultipartFormDataRequestBody.php index 320594444..871558f7c 100644 --- a/src/Asset/Attribute/Request/AddAssetRequestBody.php +++ b/src/OpenApi/Attribute/Request/MultipartFormDataRequestBody.php @@ -14,7 +14,7 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace Pimcore\Bundle\StudioBackendBundle\Asset\Attribute\Request; +namespace Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Request; use Attribute; use OpenApi\Attributes\MediaType; @@ -25,7 +25,7 @@ * @internal */ #[Attribute(Attribute::TARGET_METHOD)] -final class AddAssetRequestBody extends RequestBody +final class MultipartFormDataRequestBody extends RequestBody { public function __construct(array $properties, array $required = []) { From 416134c43604d4f5714a7ed0a14a5b10af6735fe Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Thu, 21 Nov 2024 11:44:45 +0100 Subject: [PATCH 2/7] Add upload user image endpoint. --- config/users.yaml | 4 + src/User/Controller/UpdateUserController.php | 2 +- .../Controller/UploadUserImageController.php | 111 ++++++++++++++++++ src/User/Service/ImageUploadService.php | 59 ++++++++++ .../Service/ImageUploadServiceInterface.php | 25 ++++ 5 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 src/User/Controller/UploadUserImageController.php create mode 100644 src/User/Service/ImageUploadService.php create mode 100644 src/User/Service/ImageUploadServiceInterface.php diff --git a/config/users.yaml b/config/users.yaml index 956043aff..605760de2 100644 --- a/config/users.yaml +++ b/config/users.yaml @@ -47,6 +47,10 @@ services: Pimcore\Bundle\StudioBackendBundle\User\Service\KeyBindingServiceInterface: class: Pimcore\Bundle\StudioBackendBundle\User\Service\KeyBindingService + Pimcore\Bundle\StudioBackendBundle\User\Service\ImageUploadServiceInterface: + class: Pimcore\Bundle\StudioBackendBundle\User\Service\ImageUploadService + + Pimcore\Bundle\StudioBackendBundle\User\Service\MailServiceInterface: class: Pimcore\Bundle\StudioBackendBundle\User\Service\MailService diff --git a/src/User/Controller/UpdateUserController.php b/src/User/Controller/UpdateUserController.php index a32914608..f40d66b66 100644 --- a/src/User/Controller/UpdateUserController.php +++ b/src/User/Controller/UpdateUserController.php @@ -60,7 +60,7 @@ public function __construct( /** * @throws NotFoundException|DatabaseException|ForbiddenException|ParseException */ - #[Route('/user/{id}', name: 'pimcore_studio_api_user_update', methods: ['PUT'])] + #[Route('/user/{id}', name: 'pimcore_studio_api_user_update', requirements: ['id' => '\d+'], methods: ['PUT'])] #[IsGranted(UserPermissions::USER_MANAGEMENT->value)] #[Put( path: self::PREFIX . '/user/{id}', diff --git a/src/User/Controller/UploadUserImageController.php b/src/User/Controller/UploadUserImageController.php new file mode 100644 index 000000000..284cd00f6 --- /dev/null +++ b/src/User/Controller/UploadUserImageController.php @@ -0,0 +1,111 @@ +value)] + #[Put( + path: self::PREFIX . '/user/upload-image/{id}', + operationId: 'user_upload_image', + summary: 'user_upload_image_summary', + tags: [Tags::User->value] + )] + #[IdParameter(type: 'User')] + #[SuccessResponse] + #[MultipartFormDataRequestBody( + [ + new Property( + property: 'userImage', + description: 'User image to upload', + type: 'string', + format: 'binary' + ), + ], + ['userImage'] + )] + #[DefaultResponses([ + HttpResponseCodes::NOT_FOUND, + HttpResponseCodes::FORBIDDEN, + ])] + public function uploadUserImage( + int $id, + // TODO: Symfony 7.1 change to https://symfony.com/blog/new-in-symfony-7-1-mapuploadedfile-attribute + Request $request + ): Response + { + $file = $request->files->get('userImage'); + if (!$file instanceof UploadedFile) { + throw new EnvironmentException('Invalid file found in the request'); + } + + $this->imageUploadService->uploadUserImage($file, $id); + + + return new Response(); + } +} diff --git a/src/User/Service/ImageUploadService.php b/src/User/Service/ImageUploadService.php new file mode 100644 index 000000000..988e7288c --- /dev/null +++ b/src/User/Service/ImageUploadService.php @@ -0,0 +1,59 @@ +userRepository->getUserById($userId); + $currentUser = $this->securityService->getCurrentUser(); + + if ($user->isAdmin() && !$currentUser->isAdmin()) { + throw new ForbiddenException('You are not allowed to upload an image for an admin user'); + } + + $fileType = $this->assetResolver->getTypeFromMimeMapping($file->getMimeType(), $file->getFilename()); + + if ($fileType !== 'image') { + throw new ForbiddenException('Only images are allowed'); + } + + + $user->setImage($file->getPathname()); + } +} \ No newline at end of file diff --git a/src/User/Service/ImageUploadServiceInterface.php b/src/User/Service/ImageUploadServiceInterface.php new file mode 100644 index 000000000..eb5d85cdc --- /dev/null +++ b/src/User/Service/ImageUploadServiceInterface.php @@ -0,0 +1,25 @@ + Date: Thu, 21 Nov 2024 11:45:05 +0100 Subject: [PATCH 3/7] Add Image UploadService Test. --- .../User/Service/ImageUploadServiceTest.php | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 tests/Unit/User/Service/ImageUploadServiceTest.php diff --git a/tests/Unit/User/Service/ImageUploadServiceTest.php b/tests/Unit/User/Service/ImageUploadServiceTest.php new file mode 100644 index 000000000..41145c9ba --- /dev/null +++ b/tests/Unit/User/Service/ImageUploadServiceTest.php @@ -0,0 +1,127 @@ +makeEmpty(UserInterface::class, [ + 'isAdmin' => true + ]); + + $currentUserMock = $this->makeEmpty(UserInterface::class, [ + 'isAdmin' => false + ]); + + $userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [ + 'getUserById' => $userMock + ]); + + $securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [ + 'getCurrentUser' => $currentUserMock + ]); + + $assetResolver = $this->makeEmpty(AssetResolverInterface::class); + + $imageUploadService = new ImageUploadService($userRepositoryMock, $securityServiceMock, $assetResolver); + + $this->expectException(ForbiddenException::class); + $this->expectExceptionMessage('You are not allowed to upload an image for an admin user'); + $imageUploadService->uploadUserImage($this->makeEmpty(UploadedFile::class), 1); + } + + public function testWrongFileType(): void + { + $userMock = $this->makeEmpty(UserInterface::class, [ + 'isAdmin' => true + ]); + + $currentUserMock = $this->makeEmpty(UserInterface::class, [ + 'isAdmin' => true + ]); + + $userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [ + 'getUserById' => $userMock + ]); + + $securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [ + 'getCurrentUser' => $currentUserMock + ]); + + $assetResolver = $this->makeEmpty(AssetResolverInterface::class, [ + 'getTypeFromMimeMapping' => 'document' + ]); + + $fileMock = $this->makeEmpty(UploadedFile::class, [ + 'getMimeType' => 'application/pdf', + 'getFilename' => 'test.pdf' + ]); + + $imageUploadService = new ImageUploadService($userRepositoryMock, $securityServiceMock, $assetResolver); + + $this->expectException(ForbiddenException::class); + $this->expectExceptionMessage('Only images are allowed'); + $imageUploadService->uploadUserImage($fileMock, 1); + } + + public function testSetImageOfUserIsCalled(): void + { + $userMock = $this->makeEmpty(UserInterface::class, [ + 'isAdmin' => true, + 'setImage' => Expected::once(function (string $path) { + $this->assertSame('/tmp/test.png', $path); + }) + ]); + + $currentUserMock = $this->makeEmpty(UserInterface::class, [ + 'isAdmin' => true + ]); + + $userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [ + 'getUserById' => $userMock + ]); + + $securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [ + 'getCurrentUser' => $currentUserMock + ]); + + $assetResolver = $this->makeEmpty(AssetResolverInterface::class, [ + 'getTypeFromMimeMapping' => 'image' + ]); + + $fileMock = $this->makeEmpty(UploadedFile::class, [ + 'getMimeType' => 'image/png', + 'getFilename' => 'test.png', + 'getPathname' => '/tmp/test.png' + ]); + + $imageUploadService = new ImageUploadService($userRepositoryMock, $securityServiceMock, $assetResolver); + + $imageUploadService->uploadUserImage($fileMock, 1); + } +} \ No newline at end of file From 738e6f716a659948765f6fe31101198b9a4d4a8e Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Thu, 21 Nov 2024 11:48:50 +0100 Subject: [PATCH 4/7] Translation. --- src/User/Controller/UploadUserImageController.php | 12 ++---------- translations/studio_api_docs.en.yaml | 3 ++- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/User/Controller/UploadUserImageController.php b/src/User/Controller/UploadUserImageController.php index 284cd00f6..0419441b7 100644 --- a/src/User/Controller/UploadUserImageController.php +++ b/src/User/Controller/UploadUserImageController.php @@ -16,10 +16,8 @@ namespace Pimcore\Bundle\StudioBackendBundle\User\Controller; -use OpenApi\Attributes\JsonContent; +use OpenApi\Attributes\Post; use OpenApi\Attributes\Property; -use OpenApi\Attributes\Put; -use OpenApi\Attributes\RequestBody; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\EnvironmentException; @@ -31,20 +29,14 @@ use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; -use Pimcore\Bundle\StudioBackendBundle\User\MappedParameter\UpdateUserParameter; -use Pimcore\Bundle\StudioBackendBundle\User\Schema\UpdateUser; -use Pimcore\Bundle\StudioBackendBundle\User\Schema\User as UserSchema; use Pimcore\Bundle\StudioBackendBundle\User\Service\ImageUploadServiceInterface; -use Pimcore\Bundle\StudioBackendBundle\User\Service\UserServiceInterface; use Pimcore\Bundle\StudioBackendBundle\User\Service\UserUpdateServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions; use Pimcore\Bundle\StudioBackendBundle\Util\Trait\PaginatedResponseTrait; use Symfony\Component\HttpFoundation\File\UploadedFile; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Security\Http\Attribute\IsGranted; use Symfony\Component\Serializer\SerializerInterface; @@ -69,7 +61,7 @@ public function __construct( */ #[Route('/user/upload-image/{id}', name: 'pimcore_studio_api_user_upload_image', methods: ['POST'])] #[IsGranted(UserPermissions::USER_MANAGEMENT->value)] - #[Put( + #[Post( path: self::PREFIX . '/user/upload-image/{id}', operationId: 'user_upload_image', summary: 'user_upload_image_summary', diff --git a/translations/studio_api_docs.en.yaml b/translations/studio_api_docs.en.yaml index 709fc860d..307e92b98 100644 --- a/translations/studio_api_docs.en.yaml +++ b/translations/studio_api_docs.en.yaml @@ -596,4 +596,5 @@ user_search_response: List of users user_default_key_bindings_description: | Get default key bindings for user management user_default_key_bindings_summary: Get default key bindings -user_default_key_bindings_response: List of default key bindings \ No newline at end of file +user_default_key_bindings_response: List of default key bindings +user_upload_image_summary: Upload user image \ No newline at end of file From ed388af3082c01f9ccbbc5aa944388f137c56571 Mon Sep 17 00:00:00 2001 From: martineiber Date: Thu, 21 Nov 2024 10:50:22 +0000 Subject: [PATCH 5/7] Apply php-cs-fixer changes --- .../Controller/UploadUserImageController.php | 4 +- src/User/Service/ImageUploadService.php | 17 ++++---- .../Service/ImageUploadServiceInterface.php | 12 +++--- .../User/Service/ImageUploadServiceTest.php | 43 ++++++++++--------- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/User/Controller/UploadUserImageController.php b/src/User/Controller/UploadUserImageController.php index 0419441b7..02a914c66 100644 --- a/src/User/Controller/UploadUserImageController.php +++ b/src/User/Controller/UploadUserImageController.php @@ -88,8 +88,7 @@ public function uploadUserImage( int $id, // TODO: Symfony 7.1 change to https://symfony.com/blog/new-in-symfony-7-1-mapuploadedfile-attribute Request $request - ): Response - { + ): Response { $file = $request->files->get('userImage'); if (!$file instanceof UploadedFile) { throw new EnvironmentException('Invalid file found in the request'); @@ -97,7 +96,6 @@ public function uploadUserImage( $this->imageUploadService->uploadUserImage($file, $id); - return new Response(); } } diff --git a/src/User/Service/ImageUploadService.php b/src/User/Service/ImageUploadService.php index 988e7288c..6fd24b78a 100644 --- a/src/User/Service/ImageUploadService.php +++ b/src/User/Service/ImageUploadService.php @@ -4,20 +4,21 @@ /** * Pimcore * - * This source file is available under following license: + * This source file is available under two different licenses: + * - GNU General Public License version 3 (GPLv3) * - Pimcore Commercial License (PCL) + * Full copyright and license information is available in + * LICENSE.md which is distributed with this source code. * - * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) - * @license http://www.pimcore.org/license PCL + * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) + * @license http://www.pimcore.org/license GPLv3 and PCL */ - namespace Pimcore\Bundle\StudioBackendBundle\User\Service; use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; -use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityService; use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; use Pimcore\Bundle\StudioBackendBundle\User\Repository\UserRepositoryInterface; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -31,8 +32,7 @@ public function __construct( private UserRepositoryInterface $userRepository, private SecurityServiceInterface $securityService, private AssetResolverInterface $assetResolver - ) - { + ) { } /** @@ -53,7 +53,6 @@ public function uploadUserImage(UploadedFile $file, int $userId): void throw new ForbiddenException('Only images are allowed'); } - $user->setImage($file->getPathname()); } -} \ No newline at end of file +} diff --git a/src/User/Service/ImageUploadServiceInterface.php b/src/User/Service/ImageUploadServiceInterface.php index eb5d85cdc..182f39bdb 100644 --- a/src/User/Service/ImageUploadServiceInterface.php +++ b/src/User/Service/ImageUploadServiceInterface.php @@ -4,14 +4,16 @@ /** * Pimcore * - * This source file is available under following license: + * This source file is available under two different licenses: + * - GNU General Public License version 3 (GPLv3) * - Pimcore Commercial License (PCL) + * Full copyright and license information is available in + * LICENSE.md which is distributed with this source code. * - * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) - * @license http://www.pimcore.org/license PCL + * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) + * @license http://www.pimcore.org/license GPLv3 and PCL */ - namespace Pimcore\Bundle\StudioBackendBundle\User\Service; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -22,4 +24,4 @@ interface ImageUploadServiceInterface { public function uploadUserImage(UploadedFile $file, int $userId): void; -} \ No newline at end of file +} diff --git a/tests/Unit/User/Service/ImageUploadServiceTest.php b/tests/Unit/User/Service/ImageUploadServiceTest.php index 41145c9ba..512fd94cf 100644 --- a/tests/Unit/User/Service/ImageUploadServiceTest.php +++ b/tests/Unit/User/Service/ImageUploadServiceTest.php @@ -4,11 +4,14 @@ /** * Pimcore * - * This source file is available under following license: + * This source file is available under two different licenses: + * - GNU General Public License version 3 (GPLv3) * - Pimcore Commercial License (PCL) + * Full copyright and license information is available in + * LICENSE.md which is distributed with this source code. * - * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) - * @license http://www.pimcore.org/license PCL + * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) + * @license http://www.pimcore.org/license GPLv3 and PCL */ namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\User\Service; @@ -31,19 +34,19 @@ final class ImageUploadServiceTest extends Unit public function testNonAdminCanNotEditAdminUser(): void { $userMock = $this->makeEmpty(UserInterface::class, [ - 'isAdmin' => true + 'isAdmin' => true, ]); $currentUserMock = $this->makeEmpty(UserInterface::class, [ - 'isAdmin' => false + 'isAdmin' => false, ]); $userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [ - 'getUserById' => $userMock + 'getUserById' => $userMock, ]); $securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [ - 'getCurrentUser' => $currentUserMock + 'getCurrentUser' => $currentUserMock, ]); $assetResolver = $this->makeEmpty(AssetResolverInterface::class); @@ -58,28 +61,28 @@ public function testNonAdminCanNotEditAdminUser(): void public function testWrongFileType(): void { $userMock = $this->makeEmpty(UserInterface::class, [ - 'isAdmin' => true + 'isAdmin' => true, ]); $currentUserMock = $this->makeEmpty(UserInterface::class, [ - 'isAdmin' => true + 'isAdmin' => true, ]); $userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [ - 'getUserById' => $userMock + 'getUserById' => $userMock, ]); $securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [ - 'getCurrentUser' => $currentUserMock + 'getCurrentUser' => $currentUserMock, ]); $assetResolver = $this->makeEmpty(AssetResolverInterface::class, [ - 'getTypeFromMimeMapping' => 'document' + 'getTypeFromMimeMapping' => 'document', ]); $fileMock = $this->makeEmpty(UploadedFile::class, [ 'getMimeType' => 'application/pdf', - 'getFilename' => 'test.pdf' + 'getFilename' => 'test.pdf', ]); $imageUploadService = new ImageUploadService($userRepositoryMock, $securityServiceMock, $assetResolver); @@ -95,33 +98,33 @@ public function testSetImageOfUserIsCalled(): void 'isAdmin' => true, 'setImage' => Expected::once(function (string $path) { $this->assertSame('/tmp/test.png', $path); - }) + }), ]); $currentUserMock = $this->makeEmpty(UserInterface::class, [ - 'isAdmin' => true + 'isAdmin' => true, ]); $userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [ - 'getUserById' => $userMock + 'getUserById' => $userMock, ]); $securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [ - 'getCurrentUser' => $currentUserMock + 'getCurrentUser' => $currentUserMock, ]); $assetResolver = $this->makeEmpty(AssetResolverInterface::class, [ - 'getTypeFromMimeMapping' => 'image' + 'getTypeFromMimeMapping' => 'image', ]); $fileMock = $this->makeEmpty(UploadedFile::class, [ 'getMimeType' => 'image/png', 'getFilename' => 'test.png', - 'getPathname' => '/tmp/test.png' + 'getPathname' => '/tmp/test.png', ]); $imageUploadService = new ImageUploadService($userRepositoryMock, $securityServiceMock, $assetResolver); $imageUploadService->uploadUserImage($fileMock, 1); } -} \ No newline at end of file +} From 583fc212ecca2d0d4476484b5f7fd42a78b1189b Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Thu, 21 Nov 2024 12:04:10 +0100 Subject: [PATCH 6/7] Remove unused service. --- src/User/Controller/UploadUserImageController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/User/Controller/UploadUserImageController.php b/src/User/Controller/UploadUserImageController.php index 02a914c66..a2f10741e 100644 --- a/src/User/Controller/UploadUserImageController.php +++ b/src/User/Controller/UploadUserImageController.php @@ -50,7 +50,6 @@ final class UploadUserImageController extends AbstractApiController public function __construct( SerializerInterface $serializer, - private readonly UserUpdateServiceInterface $userUpdateService, private readonly ImageUploadServiceInterface $imageUploadService ) { parent::__construct($serializer); From bfb909cd84b92b11669e110a3c53a4cf6eb7cc19 Mon Sep 17 00:00:00 2001 From: martineiber Date: Thu, 21 Nov 2024 11:04:48 +0000 Subject: [PATCH 7/7] Apply php-cs-fixer changes --- src/User/Controller/UploadUserImageController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/User/Controller/UploadUserImageController.php b/src/User/Controller/UploadUserImageController.php index a2f10741e..78b942ed5 100644 --- a/src/User/Controller/UploadUserImageController.php +++ b/src/User/Controller/UploadUserImageController.php @@ -30,7 +30,6 @@ use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; use Pimcore\Bundle\StudioBackendBundle\User\Service\ImageUploadServiceInterface; -use Pimcore\Bundle\StudioBackendBundle\User\Service\UserUpdateServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions; use Pimcore\Bundle\StudioBackendBundle\Util\Trait\PaginatedResponseTrait;