Skip to content

Commit

Permalink
Leverage the new user removal handler API for cascade deleting team r…
Browse files Browse the repository at this point in the history
…oles
  • Loading branch information
mxr576 committed Aug 30, 2023
1 parent 29ac329 commit 1d145ba
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
23 changes: 0 additions & 23 deletions modules/apigee_edge_teams/apigee_edge_teams.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand Down
5 changes: 5 additions & 0 deletions modules/apigee_edge_teams/apigee_edge_teams.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.user_removal_handler:
class: Drupal\apigee_edge_teams\User\RemoveTeamRolesWithUserSynchronousUserRemovalHandler
decorates: apigee_edge.user_removal_handler
arguments: [ '@apigee_edge_teams.user_removal_handler.inner', '@entity_type.manager', '@logger.channel.apigee_edge_teams' ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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_teams\User;

use Drupal\apigee_edge\User\UserRemovalHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Utility\Error;
use Drupal\user\UserInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

/**
* Ensures team roles of the removed user also get deleted.
*/
final class RemoveTeamRolesWithUserSynchronousUserRemovalHandler implements UserRemovalHandlerInterface {

/**
* RemoveTeamRolesWithUserSynchronousUserRemovalHandler constructor.
*/
public function __construct(private readonly UserRemovalHandlerInterface $decorated, private readonly EntityTypeManagerInterface $entityTypeManager, private readonly LoggerInterface $logger) {}

/**
* {@inheritdoc}
*/
public function __invoke(UserInterface $account): void {
($this->decorated)($account);

/** @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($account) 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' => $account->getEmail(),
'%team' => $team_member_roles_in_team->getTeam()->id(),
], LogLevel::CRITICAL);
}
}
}

}

0 comments on commit 1d145ba

Please sign in to comment.