Skip to content

Commit

Permalink
#179 Update User Clone Logic (#119)
Browse files Browse the repository at this point in the history
* Minor changes.

* Minor changes.
  • Loading branch information
martineiber authored Jun 12, 2024
1 parent 1bd195e commit e273bda
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/User/Service/UserCloneService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
)
{
}
Expand All @@ -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());
}
Expand All @@ -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());
Expand All @@ -110,7 +116,6 @@ private function cloneAndAssignWorkspaces(): void
$this->cloneAndAssignAssetWorkspace();
$this->cloneAndAssignDocumentWorkspace();
$this->cloneAndAssignDataObjectWorkspace();
$this->user->save();
}

/**
Expand Down

0 comments on commit e273bda

Please sign in to comment.