diff --git a/apigee_edge.module b/apigee_edge.module index ddd2311d..304f9021 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.post_user_delete_action_performer'))($account); } /** diff --git a/apigee_edge.services.yml b/apigee_edge.services.yml index 60218130..82334a68 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.post_user_delete_action_performer: + class: Drupal\apigee_edge\User\RemoveRelatedDeveloperAccountSynchronousPostUserDeleteActionPerformer + arguments: ['@entity_type.manager', '@logger.channel.apigee_edge'] diff --git a/modules/apigee_edge_teams/apigee_edge_teams.module b/modules/apigee_edge_teams/apigee_edge_teams.module index b274567e..1bda20e5 100644 --- a/modules/apigee_edge_teams/apigee_edge_teams.module +++ b/modules/apigee_edge_teams/apigee_edge_teams.module @@ -117,29 +117,6 @@ function _apigee_edge_teams_team_app_entity_form_display_edit_form_validate(arra } } -/** - * Implements hook_ENTITY_TYPE_delete(). - */ -function apigee_edge_teams_user_delete(EntityInterface $entity) { - /** @var \Drupal\user\UserInterface $entity */ - /** @var \Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorageInterface $team_member_role_storage */ - $team_member_role_storage = \Drupal::entityTypeManager()->getStorage('team_member_role'); - // When a user gets deleted then its developer account also gets deleted - // from Apigee Edge which removes its (team) company memberships. - // We must delete this user's team roles from Drupal as well. - foreach ($team_member_role_storage->loadByDeveloper($entity) as $team_member_roles_in_team) { - try { - $team_member_roles_in_team->delete(); - } - catch (EntityStorageException $e) { - \Drupal::logger('apigee_edge_teams')->critical("Integrity check: Failed to remove %developer team member's roles in %team team when its Drupal user got deleted.", [ - '%developer' => $entity->getEmail(), - '%team' => $team_member_roles_in_team->getTeam()->id(), - ]); - } - } -} - /** * Implements hook_ENTITY_TYPE_delete(). */ diff --git a/modules/apigee_edge_teams/apigee_edge_teams.services.yml b/modules/apigee_edge_teams/apigee_edge_teams.services.yml index 933b644c..0db1d3f9 100644 --- a/modules/apigee_edge_teams/apigee_edge_teams.services.yml +++ b/modules/apigee_edge_teams/apigee_edge_teams.services.yml @@ -146,3 +146,8 @@ services: apigee_edge_teams.cli: class: Drupal\apigee_edge_teams\CliService arguments: ['@apigee_edge.apigee_edge_mgmt_cli_service'] + + apigee_edge_teams.post_user_delete_action_performer: + class: Drupal\apigee_edge_teams\User\RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer + decorates: apigee_edge.post_user_delete_action_performer + arguments: [ '@apigee_edge_teams.post_user_delete_action_performer.inner', '@entity_type.manager', '@logger.channel.apigee_edge_teams' ] diff --git a/modules/apigee_edge_teams/src/User/RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer.php b/modules/apigee_edge_teams/src/User/RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer.php new file mode 100644 index 00000000..286267c7 --- /dev/null +++ b/modules/apigee_edge_teams/src/User/RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer.php @@ -0,0 +1,64 @@ +decorated)($user); + + /** @var \Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorageInterface $team_member_role_storage */ + $team_member_role_storage = $this->entityTypeManager->getStorage('team_member_role'); + // When a user gets deleted then its developer account also gets deleted + // from Apigee Edge which removes its (team) company memberships. + // We must delete this user's team roles from Drupal as well. + foreach ($team_member_role_storage->loadByDeveloper($user) as $team_member_roles_in_team) { + try { + $team_member_roles_in_team->delete(); + } + catch (\Exception $e) { + Error::logException($this->logger, $e, "Integrity check: Failed to remove %developer team member's roles in %team team when its Drupal user got deleted. Reason: @message", [ + '%developer' => $user->getEmail(), + '%team' => $team_member_roles_in_team->getTeam()->id(), + ], LogLevel::CRITICAL); + } + } + } + +} diff --git a/src/User/PostUserDeleteActionPerformerInterface.php b/src/User/PostUserDeleteActionPerformerInterface.php new file mode 100644 index 00000000..4a3110ba --- /dev/null +++ b/src/User/PostUserDeleteActionPerformerInterface.php @@ -0,0 +1,44 @@ +entityTypeManager = $entity_type_manager; + $this->logger = $logger; + } + + /** + * {@inheritdoc} + */ + public function __invoke(UserInterface $user): void { + // Do not try to delete developer of the anonymous user because it does + // not exist. + if ($user->isAnonymous()) { + return; + } + + try { + /** @var \Drupal\apigee_edge\Entity\DeveloperInterface|null $developer */ + $developer = $this->entityTypeManager->getStorage('developer')->load($user->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' => $user->getEmail(), + ]); + } + } + catch (\Exception $exception) { + $context = [ + '@developer' => $user->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).
@backtrace_string
', $context); + } + } + +}