Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHRAS-3960 bin/console clean:user - user deletion issue on --usertype=appowner #4494

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading