user.post_update.php

Same filename in other branches
  1. 8.9.x core/modules/user/user.post_update.php
  2. 10 core/modules/user/user.post_update.php
  3. 11.x core/modules/user/user.post_update.php

Post update functions for User module.

File

core/modules/user/user.post_update.php

View source
<?php


/**
 * @file
 * Post update functions for User module.
 */
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\user\Entity\Role;

/**
 * Implements hook_removed_post_updates().
 */
function user_removed_post_updates() {
    return [
        'user_post_update_enforce_order_of_permissions' => '9.0.0',
    ];
}

/**
 * Calculate role dependencies and remove non-existent permissions.
 */
function user_post_update_update_roles(&$sandbox = NULL) {
    $cleaned_roles = [];
    $existing_permissions = array_keys(\Drupal::service('user.permissions')->getPermissions());
    \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'user_role', function (Role $role) use ($existing_permissions, &$cleaned_roles) {
        $removed_permissions = array_diff($role->getPermissions(), $existing_permissions);
        if (!empty($removed_permissions)) {
            $cleaned_roles[] = $role->label();
            \Drupal::logger('update')->notice('The role %role has had the following non-existent permission(s) removed: %permissions.', [
                '%role' => $role->label(),
                '%permissions' => implode(', ', $removed_permissions),
            ]);
        }
        $permissions = array_intersect($role->getPermissions(), $existing_permissions);
        $role->set('permissions', $permissions);
        return TRUE;
    });
    if (!empty($cleaned_roles)) {
        return new PluralTranslatableMarkup(count($cleaned_roles), 'The role %role_list has had non-existent permissions removed. Check the logs for details.', 'The roles %role_list have had non-existent permissions removed. Check the logs for details.', [
            '%role_list' => implode(', ', $cleaned_roles),
        ]);
    }
}

/**
 * No-op update.
 */
function user_post_update_sort_permissions(&$sandbox = NULL) {
}

/**
 * Ensure permissions stored in role configuration are sorted using the schema.
 */
function user_post_update_sort_permissions_again(&$sandbox = NULL) {
    \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'user_role', function (Role $role) {
        $permissions = $role->getPermissions();
        sort($permissions);
        return $permissions !== $role->getPermissions();
    });
}

Functions

Title Deprecated Summary
user_post_update_sort_permissions No-op update.
user_post_update_sort_permissions_again Ensure permissions stored in role configuration are sorted using the schema.
user_post_update_update_roles Calculate role dependencies and remove non-existent permissions.
user_removed_post_updates Implements hook_removed_post_updates().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.