Skip to content

Commit

Permalink
fix user deletion command (#4494)
Browse files Browse the repository at this point in the history
  • Loading branch information
aynsix authored Apr 30, 2024
1 parent 4540b28 commit 7e3fe51
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions lib/Alchemy/Phrasea/Model/Manager/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -300,20 +299,50 @@ 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);
foreach ($oauthCodes as $oauthCode) {
$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);
}
}
}

0 comments on commit 7e3fe51

Please sign in to comment.