You are here

function configuration_check_user_role in Configuration Management 7

User roles are unique in that the name of the config and the name of the role are the same. If you change the name of the role, you are effectively creating a new configuration item. For that reason, we have to check to see what roles are no longer existent in the activestore, and mark those as changed.

2 calls to configuration_check_user_role()
configuration_user_role_update in observers/observer.user.inc
Implements hook_user_role_update().
user_role_configuration_rebuild in includes/configuration.user.inc
Implements hook_configuration_rebuild().

File

includes/configuration.user.inc, line 339

Code

function configuration_check_user_role($identifier) {

  // Get static variable that we can access across this request.
  $from_activestore =& drupal_static('configuration_from_activestore');
  $component = 'user_role';
  if (file_exists("config://configuration.user_role.inc")) {

    // Load the current configuration file on disk
    include_once drupal_realpath("config://configuration.user_role.inc");
    $roles = user_roles();

    // Export just the field we're tracking.
    module_load_include('inc', 'configuration', 'configuration.export');
    $config = configuration_get_configuration('user_role');

    // Export the field we just saved and evaluate the export to $fields
    $code = user_role_configuration_export_render('configuration', array(
      $identifier,
    ));
    eval(array_pop($code));

    // If the activestore doesn't exist it is most likely because this configuration
    // only exists in code.
    if (empty($roles)) {
      configuration_set_status($component, $identifier, CONFIGURATION_TRACKED_DATASTORE_ONLY);
    }

    // Get the permissions on disk.
    $roles_code = configuration_user_default_roles();
    $return = '';
    $component = 'user_role';
    $status = $config[$identifier]['status'];

    // Configs in code are not the same as what was just saved in activestore.
    if ($from_activestore == TRUE) {
      $roles = user_roles();
      foreach (array_keys($config) as $identifier) {
        if (!in_array($identifier, $roles)) {

          // dsm('configs in code are not the same as what was just saved in activestore.');
          $status = $status | CONFIGURATION_DELETE;
          configuration_set_status($component, $identifier, $status);
        }
      }
    }
    $md5_datastore = is_array($roles_code) && array_key_exists($identifier, $roles_code) ? md5(serialize($roles_code[$identifier])) : '';
    $md5_activestore = is_array($roles) && array_key_exists($identifier, $roles) ? md5(serialize($roles[$identifier])) : '';

    // When checking for new configurations, check to see if configurations are
    // the same in datastore as last activestore
    if (!$from_activestore && (isset($config[$identifier]) && isset($roles_code[$identifier])) && $md5_datastore == $config[$identifier]['hash']) {
      $status = $status & ~CONFIGURATION_DATASTORE_OVERRIDDEN;
      configuration_set_status($component, $identifier, $status);
    }

    // Menu in the activestore is the same as what is in code.
    if (isset($roles[$identifier]) && isset($roles_code[$identifier]) && $md5_activestore == $md5_datastore) {
      $status = CONFIGURATION_IN_SYNC;
      configuration_set_status($component, $identifier, $status);
      configuration_set_hash($component, $identifier, $md5_activestore);
    }

    // Compare what was just exported to what is on the file system.
    if (isset($roles[$identifier]) && isset($roles_code[$identifier]) && $md5_activestore != $md5_datastore) {
      $status = $settings['status'] | CONFIGURATION_DATASTORE_OVERRIDDEN;
      configuration_set_status($component, $identifier, $status);
    }

    // Store the config array in cache for easy access
    if ($status != CONFIGURATION_IN_SYNC) {

      // Supress error, there may not be anything in activestore
      $configuration[$component][$identifier]['activestore'] = is_array($roles) && array_key_exists($identifier, $roles) ? $roles[$identifier] : '';
      $configuration[$component][$identifier]['datastore'] = is_array($roles_code) && array_key_exists($identifier, $roles_code) ? $roles_code[$identifier] : '';
      cache_set("{$component}:{$identifier}", $configuration, 'cache_configuration');
    }

    // Look for new configurations that the system doesn't know about
    foreach (array_keys($roles_code) as $name) {
      if (!in_array($name, array_keys($config))) {
        configuration_add_status('user_role', $name, md5(serialize($roles_code[$name])));
      }
    }
  }
}