Skip to content

Commit

Permalink
Adjust intranet roles when deleting roles
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Oct 18, 2023
1 parent 7d65d25 commit 2259213
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
14 changes: 14 additions & 0 deletions modules/stanford_intranet/stanford_intranet.install
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
29 changes: 23 additions & 6 deletions modules/stanford_intranet/stanford_intranet.module
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Config/ConfigOverrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 2259213

Please sign in to comment.