From bcb287146f2667f6d3389418c89b673dd9c1e271 Mon Sep 17 00:00:00 2001 From: "ferenc.csongradi" Date: Fri, 25 Aug 2023 11:29:33 +0200 Subject: [PATCH] Extract user deletion into a new service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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Ó --- apigee_edge.module | 26 +---- apigee_edge.services.yml | 4 + ...rWithUserSynchronousUserRemovalHandler.php | 97 +++++++++++++++++++ src/User/UserRemovalHandlerInterface.php | 40 ++++++++ 4 files changed, 143 insertions(+), 24 deletions(-) create mode 100644 src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php create mode 100644 src/User/UserRemovalHandlerInterface.php diff --git a/apigee_edge.module b/apigee_edge.module index ddd2311d..e0fd0e41 100644 --- a/apigee_edge.module +++ b/apigee_edge.module @@ -447,7 +447,7 @@ function apigee_edge_entity_view(array &$build, EntityInterface $entity, EntityV ], ], ]))->toRenderable(); - + $build['#attached']['library'][] = 'core/drupal.dialog.ajax'; } } @@ -1490,29 +1490,7 @@ function apigee_edge_user_cancel_methods_alter(&$methods) { * Implements hook_user_delete(). */ function apigee_edge_user_delete(UserInterface $account) { - // 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\Developer $developer */ - $developer = Developer::load($account->getEmail()); - // Sanity check, the developer may not exist in Apigee Edge. - if ($developer) { - $developer->delete(); - } - } - catch (\Exception $exception) { - $context = [ - '@developer' => $account->getEmail(), - '@message' => (string) $exception, - ]; - // @todo watchdog_exception() function has been deprecated for Drupal 10.1 https://www.drupal.org/node/2932520 - // @phpstan-ignore-next-line - watchdog_exception('apigee_edge', $exception, 'Could not delete @developer developer entity. @message %function (line %line of %file).
@backtrace_string
', $context); - } + (\Drupal::service('apigee_edge.user_removal_handler'))($account); } /** diff --git a/apigee_edge.services.yml b/apigee_edge.services.yml index 60218130..f09bc2bb 100644 --- a/apigee_edge.services.yml +++ b/apigee_edge.services.yml @@ -213,3 +213,7 @@ services: apigee_edge.authentication.oauth_token_storage: class: Drupal\apigee_edge\OauthTokenFileStorage arguments: ['@config.factory', '@file_system', '@logger.channel.apigee_edge'] + + apigee_edge.user_removal_handler: + class: Drupal\apigee_edge\User\RemoveDeveloperWithUserSynchronousUserRemovalHandler + arguments: ['@entity_type.manager', '@logger.channel.apigee_edge'] diff --git a/src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php b/src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php new file mode 100644 index 00000000..dcab85f8 --- /dev/null +++ b/src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php @@ -0,0 +1,97 @@ +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 has been deleted as a reaction to removing its user account.', [ + '@developer' => $account->getEmail(), + ]); + } + } + catch (\Exception $exception) { + $context = [ + '@developer' => $account->getEmail(), + '@message' => (string) $exception, + ]; + Error::logException($this->logger, $exception, 'Could not delete @developer developer entity. @message %function (line %line of %file).
@backtrace_string
', $context); + } + } + +} diff --git a/src/User/UserRemovalHandlerInterface.php b/src/User/UserRemovalHandlerInterface.php new file mode 100644 index 00000000..ee99ec49 --- /dev/null +++ b/src/User/UserRemovalHandlerInterface.php @@ -0,0 +1,40 @@ +