Skip to content

Commit

Permalink
Merge pull request #19 from flownative/task/remove-authorization-on-u…
Browse files Browse the repository at this point in the history
…ser-deletion

Remove related OAuth authorization data upon user deletion
  • Loading branch information
kdambekalns authored Oct 27, 2021
2 parents f0b6af4 + 06a8c6b commit 75b9ff1
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Classes/Package.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace Flownative\Canto;

/*
* This file is part of the Flownative.Canto package.
*
* (c) Karsten Dambekalns, Flownative GmbH - www.flownative.com
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Doctrine\ORM\EntityManagerInterface;
use Flownative\Canto\Domain\Repository\AccountAuthorizationRepository;
use Flownative\OAuth2\Client\Authorization;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Package\Package as BasePackage;
use Neos\Neos\Domain\Model\User;
use Neos\Neos\Domain\Service\UserService;

class Package extends BasePackage
{
/**
* @param Bootstrap $bootstrap The current bootstrap
* @return void
*/
public function boot(Bootstrap $bootstrap)
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();
$dispatcher->connect(
UserService::class,
'userDeleted',
function (User $user) use ($bootstrap) {
$accountAuthorizationRepository = $bootstrap->getObjectManager()->get(AccountAuthorizationRepository::class);
$entityManager = $bootstrap->getObjectManager()->get(EntityManagerInterface::class);

foreach ($user->getAccounts() as $account) {
$accountAuthorization = $accountAuthorizationRepository->findOneByFlowAccountIdentifier($account->getAccountIdentifier());
if ($accountAuthorization !== null) {
$authorizationId = $accountAuthorization->getAuthorizationId();
$authorization = $entityManager->find(Authorization::class, ['authorizationId' => $authorizationId]);
if ($authorization instanceof Authorization) {
$entityManager->remove($authorization);
}

$accountAuthorizationRepository->remove($accountAuthorization);
}
}
},
'',
false
);
}
}

0 comments on commit 75b9ff1

Please sign in to comment.