|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * This source file is available under the terms of the |
| 6 | + * Pimcore Open Core License (POCL) |
| 7 | + * Full copyright and license information is available in |
| 8 | + * LICENSE.md which is distributed with this source code. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) |
| 11 | + * @license Pimcore Open Core License (POCL) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\User\Service; |
| 15 | + |
| 16 | +use Codeception\Test\Unit; |
| 17 | +use Pimcore\Bundle\StaticResolverBundle\Lib\CacheResolverInterface; |
| 18 | +use Pimcore\Bundle\StaticResolverBundle\Lib\Tools\Authentication\AuthenticationResolverInterface; |
| 19 | +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException; |
| 20 | +use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; |
| 21 | +use Pimcore\Bundle\StudioBackendBundle\User\MappedParameter\UpdateUserParameter; |
| 22 | +use Pimcore\Bundle\StudioBackendBundle\User\Repository\UserRepositoryInterface; |
| 23 | +use Pimcore\Bundle\StudioBackendBundle\User\Service\UpdateServiceInterface; |
| 24 | +use Pimcore\Bundle\StudioBackendBundle\User\Service\UserPerspectiveServiceInterface; |
| 25 | +use Pimcore\Bundle\StudioBackendBundle\User\Service\UserUpdateService; |
| 26 | +use Pimcore\Model\User; |
| 27 | +use Pimcore\Model\UserInterface; |
| 28 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 29 | + |
| 30 | +/** |
| 31 | + * @internal |
| 32 | + */ |
| 33 | +final class UserUpdateServiceTest extends Unit |
| 34 | +{ |
| 35 | + public function testNonAdminCannotUpdateAdminUser(): void |
| 36 | + { |
| 37 | + $targetUser = $this->createTargetUser(isAdmin: true); |
| 38 | + $currentUser = $this->makeEmpty(UserInterface::class, [ |
| 39 | + 'isAdmin' => false, |
| 40 | + ]); |
| 41 | + |
| 42 | + $service = $this->createService($targetUser, $currentUser); |
| 43 | + |
| 44 | + $this->expectException(ForbiddenException::class); |
| 45 | + $this->expectExceptionMessage('Only admin can update admin user'); |
| 46 | + $service->updateUserById($this->createUpdateParams(), 42); |
| 47 | + } |
| 48 | + |
| 49 | + public function testAdminCanUpdateAdminUser(): void |
| 50 | + { |
| 51 | + $targetUser = $this->createTargetUser(isAdmin: true); |
| 52 | + $currentUser = $this->makeEmpty(UserInterface::class, [ |
| 53 | + 'isAdmin' => true, |
| 54 | + ]); |
| 55 | + |
| 56 | + $service = $this->createService($targetUser, $currentUser); |
| 57 | + |
| 58 | + $service->updateUserById($this->createUpdateParams(), 42); |
| 59 | + } |
| 60 | + |
| 61 | + public function testOnlyAdminCanSetAdminFlag(): void |
| 62 | + { |
| 63 | + $targetUser = $this->createTargetUser(isAdmin: false); |
| 64 | + $currentUser = $this->makeEmpty(UserInterface::class, [ |
| 65 | + 'isAdmin' => false, |
| 66 | + ]); |
| 67 | + |
| 68 | + $service = $this->createService($targetUser, $currentUser); |
| 69 | + |
| 70 | + $params = $this->createUpdateParams(admin: true); |
| 71 | + $service->updateUserById($params, 42); |
| 72 | + |
| 73 | + $this->assertFalse($targetUser->isAdmin()); |
| 74 | + } |
| 75 | + |
| 76 | + public function testAdminCanPromoteUserToAdmin(): void |
| 77 | + { |
| 78 | + $targetUser = $this->createTargetUser(isAdmin: false); |
| 79 | + $currentUser = $this->makeEmpty(UserInterface::class, [ |
| 80 | + 'isAdmin' => true, |
| 81 | + ]); |
| 82 | + |
| 83 | + $service = $this->createService($targetUser, $currentUser); |
| 84 | + |
| 85 | + $params = $this->createUpdateParams(admin: true); |
| 86 | + $service->updateUserById($params, 42); |
| 87 | + |
| 88 | + $this->assertTrue($targetUser->isAdmin()); |
| 89 | + } |
| 90 | + |
| 91 | + public function testNonAdminCanUpdateNonAdminUser(): void |
| 92 | + { |
| 93 | + $targetUser = $this->createTargetUser(isAdmin: false); |
| 94 | + $currentUser = $this->makeEmpty(UserInterface::class, [ |
| 95 | + 'isAdmin' => false, |
| 96 | + ]); |
| 97 | + |
| 98 | + $service = $this->createService($targetUser, $currentUser); |
| 99 | + |
| 100 | + $service->updateUserById($this->createUpdateParams(), 42); |
| 101 | + } |
| 102 | + |
| 103 | + private function createTargetUser(bool $isAdmin): User |
| 104 | + { |
| 105 | + $user = new User(); |
| 106 | + $user->setAdmin($isAdmin); |
| 107 | + $user->setActive(true); |
| 108 | + $user->setName('targetuser'); |
| 109 | + |
| 110 | + return $user; |
| 111 | + } |
| 112 | + |
| 113 | + private function createUpdateParams(bool $admin = false): UpdateUserParameter |
| 114 | + { |
| 115 | + return new UpdateUserParameter( |
| 116 | + email: 'test@example.com', |
| 117 | + firstname: 'Test', |
| 118 | + lastname: 'User', |
| 119 | + active: true, |
| 120 | + admin: $admin, |
| 121 | + classes: [], |
| 122 | + docTypes: [], |
| 123 | + closeWarning: false, |
| 124 | + allowDirtyClose: false, |
| 125 | + contentLanguages: [], |
| 126 | + keyBindings: [], |
| 127 | + language: 'en', |
| 128 | + dateTimeLocale: null, |
| 129 | + memorizeTabs: false, |
| 130 | + parentId: 0, |
| 131 | + permissions: [], |
| 132 | + roles: [], |
| 133 | + twoFactorAuthenticationRequired: false, |
| 134 | + websiteTranslationLanguagesEdit: [], |
| 135 | + websiteTranslationLanguagesView: [], |
| 136 | + welcomeScreen: false, |
| 137 | + assetWorkspaces: [], |
| 138 | + dataObjectWorkspaces: [], |
| 139 | + documentWorkspaces: [], |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + private function createService(User $targetUser, UserInterface $currentUser): UserUpdateService |
| 144 | + { |
| 145 | + $updateService = $this->makeEmpty(UpdateServiceInterface::class, [ |
| 146 | + 'updatePermissions' => $targetUser, |
| 147 | + 'updateRoles' => $targetUser, |
| 148 | + 'updateClasses' => $targetUser, |
| 149 | + 'updateAssetWorkspaces' => $targetUser, |
| 150 | + 'updateDataObjectWorkspaces' => $targetUser, |
| 151 | + 'updateDocumentWorkspaces' => $targetUser, |
| 152 | + 'updatePerspectives' => $targetUser, |
| 153 | + ]); |
| 154 | + |
| 155 | + return new UserUpdateService( |
| 156 | + $this->makeEmpty(AuthenticationResolverInterface::class), |
| 157 | + $this->makeEmpty(CacheResolverInterface::class), |
| 158 | + $this->makeEmpty(SecurityServiceInterface::class, [ |
| 159 | + 'getCurrentUser' => $currentUser, |
| 160 | + ]), |
| 161 | + $this->makeEmpty(UserRepositoryInterface::class, [ |
| 162 | + 'getUserById' => $targetUser, |
| 163 | + ]), |
| 164 | + $updateService, |
| 165 | + $this->makeEmpty(UserPerspectiveServiceInterface::class), |
| 166 | + $this->makeEmpty(ValidatorInterface::class), |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
0 commit comments