From 7e3fe51a2c56ea42bd541907e47cb21942d1394a Mon Sep 17 00:00:00 2001 From: Aina Sitraka <35221835+aynsix@users.noreply.github.com> Date: Tue, 30 Apr 2024 09:48:49 +0300 Subject: [PATCH] fix user deletion command (#4494) --- .../Phrasea/Model/Manager/UserManager.php | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/Alchemy/Phrasea/Model/Manager/UserManager.php b/lib/Alchemy/Phrasea/Model/Manager/UserManager.php index 691137e75c..86cb8c3f80 100644 --- a/lib/Alchemy/Phrasea/Model/Manager/UserManager.php +++ b/lib/Alchemy/Phrasea/Model/Manager/UserManager.php @@ -11,14 +11,13 @@ namespace Alchemy\Phrasea\Model\Manager; +use Alchemy\Phrasea\Model\Entities\ApiAccount; +use Alchemy\Phrasea\Model\Entities\ApiApplication; use Alchemy\Phrasea\Model\Entities\ApiLog; -use Alchemy\Phrasea\Model\Entities\UsrList; use Alchemy\Phrasea\Model\Entities\UsrListOwner; use Doctrine\Common\Persistence\ObjectManager; use Alchemy\Phrasea\Model\Entities\User; -use Alchemy\Phrasea\Model\Entities\UserSetting; use Doctrine\DBAL\Driver\Connection; -use Doctrine\ORM\UnitOfWork AS UOW; class UserManager { @@ -300,6 +299,36 @@ private function cleanOauthApplication(User $user) { $accounts = $this->objectManager->getRepository('Phraseanet:ApiAccount')->findByUser($user); + $this->cleanByAccounts($accounts); + + $apps = $this->objectManager->getRepository('Phraseanet:ApiApplication')->findByCreator($user); + + /** @var ApiApplication $app */ + foreach ($apps as $app) { + // make sure all apiaccounts linked by apiApplication are also deleted + $accts = $this->objectManager->getRepository('Phraseanet:ApiAccount')->findBy(['application' => $app]); + + $this->cleanByAccounts($accts); + + $deliveries = $this->objectManager->getRepository('Phraseanet:WebhookEventDelivery')->findBy(['application' => $app]); + + foreach ($deliveries as $delivery) { + $payloads = $this->objectManager->getRepository('Phraseanet:WebhookEventPayload')->findBy(['delivery' => $delivery]); + + foreach ($payloads as $payload) { + $this->objectManager->remove($payload); + } + + $this->objectManager->remove($delivery); + } + + $this->objectManager->remove($app); + } + } + + private function cleanByAccounts(array $accounts) + { + /** @var ApiAccount $account */ foreach ($accounts as $account) { // remove ApiOauthCodes before ApiAccount $oauthCodes = $this->objectManager->getRepository('Phraseanet:ApiOauthCode')->findByAccount($account); @@ -307,13 +336,13 @@ private function cleanOauthApplication(User $user) $this->objectManager->remove($oauthCode); } - $this->objectManager->remove($account); - } - - $apps = $this->objectManager->getRepository('Phraseanet:ApiApplication')->findByCreator($user); + // remove ApiOauthToken before ApiAccount + $oauthTokens = $this->objectManager->getRepository('Phraseanet:ApiOauthToken')->findOauthTokens($account); + foreach ($oauthTokens as $oauthToken) { + $this->objectManager->remove($oauthToken); + } - foreach ($apps as $app) { - $this->objectManager->remove($app); + $this->objectManager->remove($account); } } }