-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract user deletion into a new service
Introduce a programming API for downstream developers that allows customizing what should happen when a user is deleted in Drupal. Co-authored-by: Dezső BICZÓ <[email protected]>
- Loading branch information
1 parent
cdb5be1
commit f5e55a8
Showing
4 changed files
with
142 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2023 Google Inc. | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* version 2 as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace Drupal\apigee_edge\User; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Utility\Error; | ||
use Drupal\user\UserInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Handler that removes the related developer when a user is deleted. | ||
* | ||
* ATTENTION!!! Removing a developer from Apigee is a dangerous operation | ||
* because it also destroys all API keys the developer pwns. If that is not | ||
* an intended behavior, use the service decorator pattern to customize | ||
* this process. | ||
* | ||
* @see \apigee_edge_user_delete() | ||
*/ | ||
final class RemoveDeveloperWithUserSynchronousUserRemovalHandler implements UserRemovalHandlerInterface { | ||
|
||
/** | ||
* Entity type manager service. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
private EntityTypeManagerInterface $entityTypeManager; | ||
|
||
/** | ||
* Logger interface. | ||
* | ||
* @var \Psr\Log\LoggerInterface | ||
*/ | ||
private LoggerInterface $logger; | ||
|
||
/** | ||
* RemoveDeveloperWithUserSynchronousUserRemovalHandler constructor. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* Entity type manager service. | ||
* @param \Psr\Log\LoggerInterface $logger | ||
* The logger. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger) { | ||
$this->entityTypeManager = $entity_type_manager; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __invoke(UserInterface $account): void { | ||
// Do not try to delete developer of the anonymous user because it does | ||
// not exist. | ||
if ($account->isAnonymous()) { | ||
return; | ||
} | ||
|
||
try { | ||
/** @var \Drupal\apigee_edge\Entity\DeveloperInterface|null $developer */ | ||
$developer = $this->entityTypeManager->getStorage('developer')->load($account->getEmail()); | ||
// Sanity check, the developer may not exist in Apigee Edge. | ||
if ($developer) { | ||
$developer->delete(); | ||
$this->logger->info('The @developer developer has been deleted as a reaction to removing its user account.', [ | ||
'@developer' => $account->getEmail(), | ||
]); | ||
} | ||
} | ||
catch (\Exception $exception) { | ||
$context = [ | ||
'@developer' => $account->getEmail(), | ||
]; | ||
Error::logException($this->logger, $exception, 'The @developer developer could not be deleted as a reaction to removing its user account. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2023 Google Inc. | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* version 2 as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace Drupal\apigee_edge\User; | ||
|
||
use Drupal\user\UserInterface; | ||
|
||
/** | ||
* Contract for triggering an Apigee specific reaction on user removal. | ||
*/ | ||
interface UserRemovalHandlerInterface { | ||
|
||
/** | ||
* React on user removal. | ||
* | ||
* @param \Drupal\user\UserInterface $account | ||
* The deleted user account. | ||
* | ||
* @see \hook_ENTITY_TYPE_delete() | ||
*/ | ||
public function __invoke(UserInterface $account): void; | ||
|
||
} |