From e273bda69d6933fe0a42fdfc709294d9746f23e5 Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Wed, 12 Jun 2024 14:42:02 +0200 Subject: [PATCH] #179 Update User Clone Logic (#119) * Minor changes. * Minor changes. --- src/User/Service/UserCloneService.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/User/Service/UserCloneService.php b/src/User/Service/UserCloneService.php index 1f0608523..fb2565948 100644 --- a/src/User/Service/UserCloneService.php +++ b/src/User/Service/UserCloneService.php @@ -20,6 +20,7 @@ use Exception; use Pimcore\Bundle\StudioBackendBundle\Exception\DatabaseException; use Pimcore\Bundle\StudioBackendBundle\Exception\NotFoundException; +use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; use Pimcore\Bundle\StudioBackendBundle\User\Event\UserTreeNodeEvent; use Pimcore\Bundle\StudioBackendBundle\User\Hydrator\UserTreeNodeHydratorInterface; use Pimcore\Bundle\StudioBackendBundle\User\Schema\UserTreeNode; @@ -34,7 +35,8 @@ final class UserCloneService implements UserCloneServiceInterface public function __construct( private readonly WorkspaceCloneServiceInterface $workspaceCloneService, private readonly UserTreeNodeHydratorInterface $userTreeNodeHydrator, - private readonly EventDispatcherInterface $eventDispatcher + private readonly EventDispatcherInterface $eventDispatcher, + private readonly SecurityServiceInterface $securityService ) { } @@ -57,8 +59,9 @@ public function cloneUser(int $userId, string $userName): UserTreeNode $this->setUserProperties($userName); try { - $this->user->save(); + $this->user->save(); // save user to get id $this->cloneAndAssignWorkspaces(); + $this->user->save(); // save user with workspaces } catch (Exception $e) { throw new DatabaseException("Could not save user: " . $e->getMessage()); } @@ -85,7 +88,10 @@ private function setUserProperties(string $userName): void $this->user->setActive($this->userToClone->getActive()); $this->user->setPerspectives($this->userToClone->getPerspectives()); $this->user->setPermissions($this->userToClone->getPermissions()); - $this->user->setAdmin($this->userToClone->isAdmin()); + $this->user->setAdmin(false); + if ($this->securityService->getCurrentUser()->isAdmin()) { + $this->user->setAdmin($this->userToClone->isAdmin()); + } $this->user->setRoles($this->userToClone->getRoles()); $this->user->setWelcomeScreen($this->userToClone->getWelcomescreen()); $this->user->setMemorizeTabs($this->userToClone->getMemorizeTabs()); @@ -110,7 +116,6 @@ private function cloneAndAssignWorkspaces(): void $this->cloneAndAssignAssetWorkspace(); $this->cloneAndAssignDocumentWorkspace(); $this->cloneAndAssignDataObjectWorkspace(); - $this->user->save(); } /**