summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/rest/rest.install
blob: 5fdb5cc9e28130e6c5a74fbb63157f95f9d5837b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

/**
 * @file
 * Install, update and uninstall functions for the rest module.
 */

use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Install the REST config entity type and fix old settings-based config.
 *
 * @see rest_post_update_create_rest_resource_config_entities()
 */
function rest_update_8201() {
  \Drupal::entityDefinitionUpdateManager()->installEntityType(new ConfigEntityType([
    'id' => 'rest_resource_config',
    'label' => new TranslatableMarkup('REST resource configuration'),
    'config_prefix' => 'resource',
    'admin_permission' => 'administer rest resources',
    'label_callback' => 'getLabelFromPlugin',
    'entity_keys' => ['id' => 'id'],
    'config_export' => [
      'id',
      'plugin_id',
      'granularity',
      'configuration',
    ],
  ]));
  \Drupal::state()->set('rest_update_8201_resources', \Drupal::config('rest.settings')->get('resources'));
  \Drupal::configFactory()->getEditable('rest.settings')
    ->clear('resources')
    ->save();
}

/**
 * Re-save all views with a REST display to add new auth defaults.
 */
function rest_update_8202() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory->listAll('views.view.') as $view_config_name) {
    $save = FALSE;
    $view = $config_factory->getEditable($view_config_name);
    $displays = $view->get('display');
    foreach ($displays as $display_name => &$display) {
      if ($display['display_plugin'] == 'rest_export') {
        if (!isset($display['display_options']['auth'])) {
          $display['display_options']['auth'] = [];
          $save = TRUE;
        }
      }
    }
    if ($save) {
      $view->set('display', $displays);
      $view->save(TRUE);
    }
  }
}

/**
 * Enable BC for EntityResource: continue to use permissions.
 */
function rest_update_8203() {
  $config_factory = \Drupal::configFactory();
  $rest_settings = $config_factory->getEditable('rest.settings');
  $rest_settings->set('bc_entity_resource_permissions', TRUE)
    ->save(TRUE);
}

/**
 * Ensure the right REST authentication method is used.
 *
 * This fixes the bug in https://www.drupal.org/node/2825204.
 */
function rest_update_8401() {
  $config_factory = \Drupal::configFactory();
  $auth_providers = \Drupal::service('authentication_collector')->getSortedProviders();
  $process_auth = function ($auth_option) use ($auth_providers) {
    foreach ($auth_providers as $provider_id => $provider_data) {
      // The provider belongs to the module that declares it as a service.
      if (strtok($provider_data->_serviceId, '.') === $auth_option) {
        return $provider_id;
      }
    }

    return $auth_option;
  };

  foreach ($config_factory->listAll('views.view.') as $view_config_name) {
    $save = FALSE;
    $view = $config_factory->getEditable($view_config_name);
    $displays = $view->get('display');
    foreach ($displays as $display_name => $display) {
      if ('rest_export' === $display['display_plugin'] && !empty($display['display_options']['auth'])) {
        $displays[$display_name]['display_options']['auth'] = array_map($process_auth, $display['display_options']['auth']);
        $save = TRUE;
      }
    }
    if ($save) {
      $view->set('display', $displays);
      $view->save(TRUE);
    }
  }
}