From 22592136145507a9ac897b724a34e3008eef6e8a Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Wed, 18 Oct 2023 15:14:10 -0700 Subject: [PATCH] Adjust intranet roles when deleting roles --- .../stanford_intranet.install | 14 +++++++++ .../stanford_intranet.module | 29 +++++++++++++++---- src/Config/ConfigOverrides.php | 4 +-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/modules/stanford_intranet/stanford_intranet.install b/modules/stanford_intranet/stanford_intranet.install index 75cfc1e6..4bf3f355 100644 --- a/modules/stanford_intranet/stanford_intranet.install +++ b/modules/stanford_intranet/stanford_intranet.install @@ -63,3 +63,17 @@ function stanford_intranet_update_8002() { \Drupal::moduleHandler()->loadInclude('media', 'install'); media_install(); } + +/** + * Update Role ids state for group access. + */ +function stanford_intranet_update_8003() { + $roles = array_keys(user_role_names()); + $state = \Drupal::state()->get('stanford_intranet.rids', []); + foreach (array_keys($state) as $id) { + if (!in_array($id, $roles)) { + unset($state[$id]); + } + } + \Drupal::state()->set('stanford_intranet.rids', $state); +} diff --git a/modules/stanford_intranet/stanford_intranet.module b/modules/stanford_intranet/stanford_intranet.module index 3d738d50..5a04155e 100644 --- a/modules/stanford_intranet/stanford_intranet.module +++ b/modules/stanford_intranet/stanford_intranet.module @@ -214,12 +214,14 @@ function stanford_intranet_node_access_records(NodeInterface $node) { * Implements hook_node_grants(). */ function stanford_intranet_node_grants(AccountInterface $account, $op) { - $rids = \Drupal::state()->get('stanford_intranet.rids'); + $rids = \Drupal::state()->get('stanford_intranet.rids', []); $gids = []; $roles = $account->getRoles(); foreach ($roles as $role_name) { - $gids[] = $rids[$role_name]; + if (isset($rids[$role_name])) { + $gids[] = $rids[$role_name]; + } } return [ @@ -232,10 +234,25 @@ function stanford_intranet_node_grants(AccountInterface $account, $op) { * Implements hook_ENTITY_TYPE_insert(). */ function stanford_intranet_user_role_insert(RoleInterface $role) { - $rids = array_flip(\Drupal::state()->get('stanford_intranet.rids', [])); - ksort($rids); - $rids[] = $role->id(); - \Drupal::state()->set('stanford_intranet.rids', array_flip($rids)); + $state = \Drupal::state()->get('stanford_intranet.rids', []); + $state = array_flip($state); + + foreach (array_keys(user_role_names()) as $role_id) { + if (!in_array($role_id, $state)) { + $state[] = $role_id; + } + } + + \Drupal::state()->set('stanford_intranet.rids', array_flip($state)); +} + +/** + * Implements hook_ENTITY_TYPE_predelete(). + */ +function stanford_intranet_user_role_predelete(RoleInterface $role){ + $state = \Drupal::state()->get('stanford_intranet.rids', []); + unset($state[$role->id()]); + \Drupal::state()->set('stanford_intranet.rids', $state); } /** diff --git a/src/Config/ConfigOverrides.php b/src/Config/ConfigOverrides.php index 0b857c11..83fe163c 100644 --- a/src/Config/ConfigOverrides.php +++ b/src/Config/ConfigOverrides.php @@ -209,12 +209,12 @@ protected function setRolePermissionOverrides(array $names, array &$overrides) { if (in_array('user.role.site_manager', $names)) { // Arbitrary number that should be larger than the original permission // count. This allows the functionality to ADD permissions but not have - // any affect on existing permissions. If we don't have this number high + // any effect on existing permissions. If we don't have this number high // enough, it will replace permissions instead of adding them. $counter = 500; foreach (array_keys($this->state->get('stanford_intranet.rids', [])) as $role_id) { // We only care about the custom roles. - if (!str_contains($role_id, 'custm_')) { + if (!str_starts_with($role_id, 'custm_')) { continue; } $overrides['user.role.site_manager']['permissions'][$counter] = "assign $role_id role";