forked from apigee/apigee-edge-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apigee_edge.install
391 lines (344 loc) · 14.1 KB
/
apigee_edge.install
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* @file
* Copyright 2018 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Install, update and uninstall functions for Apigee.
*/
use Drupal\apigee_edge\OauthTokenFileStorage;
use Drupal\apigee_edge\Plugin\EdgeKeyTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Url;
use Drupal\user\RoleInterface;
use Drupal\Core\Config\FileStorage;
/**
* Implements hook_requirements().
*/
function apigee_edge_requirements($phase) {
$requirements = [];
if ($phase === 'install') {
// This should be checked only if Drupal is installed.
if (!InstallerKernel::installationAttempted()) {
$missing_mails = \Drupal::entityQuery('user')
->notExists('mail')
->accessCheck(TRUE)
->condition('uid', '0', '<>')
->execute();
if (!empty($missing_mails)) {
$requirements['apigee_edge_missing_mail'] = [
'title' => t('Apigee'),
'description' => t('The module can be installed only if all users have emails in Drupal, because email is a required attribute on Apigee.'),
'severity' => REQUIREMENT_ERROR,
];
}
}
}
elseif ($phase === 'runtime') {
/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
try {
$sdk_connector->testConnection();
}
catch (\Exception $exception) {
$requirements['apigee_edge_connection_error'] = [
'title' => t('Apigee'),
'value' => $exception->getMessage(),
'description' => t('Cannot connect to Apigee server. You have either given wrong credential details or the Apigee server is unreachable. Visit the <a href=":url">Apigee general settings</a> page to get more information.', [
':url' => Url::fromRoute('apigee_edge.settings', ['destination' => 'admin/reports/status'])->toString(),
]),
'severity' => REQUIREMENT_WARNING,
];
}
$auth_config = \Drupal::config('apigee_edge.auth');
if ($key_id = $auth_config->get('active_key')) {
// Warning message if insecure Configuration Key provider is being used.
$key = \Drupal::service('key.repository')->getKey($key_id);
if ($key && $key->getKeyProvider()->getPluginId() === "config") {
$requirements['apigee_edge_insecure_config_key_provider'] = [
'title' => t('Apigee'),
'description' => t('Edge connection settings are stored in Drupal’s configuration system, which is not designed to store sensitive information. When installing Kickstart for uses other than local development, we highly recommend changing the Apigee connection key provider to a more secure storage location. <a href="https://www.drupal.org/docs/8/modules/apigee-developer-portal-kickstart/apigee-kickstart-faqs#s-during-installation-a-warning-is-displayed-that-the-apigee-edge-connection-key-provider-is-not-considered-secure-what-should-i-do" target="_blank">Learn more.</a>'),
'severity' => REQUIREMENT_WARNING,
];
}
// Warning message in status report if using basic auth.
try {
if ($key && $key->getKeyType() instanceof EdgeKeyTypeInterface &&
$key->getKeyType()->getAuthenticationType($key) === EdgeKeyTypeInterface::EDGE_AUTH_TYPE_BASIC &&
$key->getKeyType()->getInstanceType($key) === EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC) {
$requirements['apigee_edge_http_basic_auth'] = [
'title' => t('Apigee'),
'description' => t('Apigee Edge HTTP basic authentication will be deprecated. Please choose another authentication method. Visit the <a href=":url">Apigee general settings</a> page to get more information.', [
':url' => Url::fromRoute('apigee_edge.settings', ['destination' => 'admin/reports/status'])->toString(),
]),
'severity' => REQUIREMENT_WARNING,
];
}
}
catch (Exception $e) {
// Do nothing.
}
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function apigee_edge_install() {
if (\Drupal::moduleHandler()->moduleExists('user')) {
$authenticated_user_permissions = [
'view own developer_app',
'create developer_app',
'update own developer_app',
'delete own developer_app',
'analytics own developer_app',
'add_api_key own developer_app',
'revoke_api_key own developer_app',
'edit_api_products developer_app',
];
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, $authenticated_user_permissions);
}
}
/**
* Implements hook_uninstall().
*/
function apigee_edge_uninstall() {
// Remove OAuth token data file.
// We do not check whether OAuth was the authentication method in use
// because this function does not trigger an error if it was not.
$storage = \Drupal::service('apigee_edge.authentication.oauth_token_storage');
if ($storage instanceof OauthTokenFileStorage) {
$storage->removeTokenFile();
}
}
/**
* Implements hook_schema().
*/
function apigee_edge_schema() {
$schema = [];
$schema['apigee_edge_job'] = [
'fields' => [
'id' => [
'type' => 'varchar',
'length' => 36,
'not null' => TRUE,
],
'status' => [
'type' => 'int',
'not null' => TRUE,
],
'tag' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'job' => [
'type' => 'blob',
'not null' => TRUE,
],
'created' => [
'type' => 'int',
'not null' => TRUE,
],
'updated' => [
'type' => 'int',
'not null' => TRUE,
],
],
'primary key' => ['id'],
'indexes' => [
'updated_idx' => ['updated'],
'status_idx' => ['status'],
'tag_idx' => ['tag'],
],
];
return $schema;
}
/**
* Initialize apigee_edge.dangerzone settings with default values.
*/
function apigee_edge_update_8001() {
$config_factory = \Drupal::configFactory();
$dangerzone_settings = $config_factory->getEditable('apigee_edge.dangerzone');
$dangerzone_settings
->set('skip_developer_app_settings_validation', FALSE)
->set('do_not_alter_key_entity_forms', FALSE)
->save(TRUE);
}
/**
* Update defaults on Apigee Auth Keys.
*/
function apigee_edge_update_8002() {
// Empty.
// Removed because Hybrid support makes this update hook unneeded.
}
/**
* Increase the max_length for first_name and last_name fields.
*/
function apigee_edge_update_8101() {
$entity_type_id = 'user';
$fields = ['first_name', 'last_name'];
$field_length = 64;
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions */
$field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
$entity_type = $definition_update_manager->getEntityType($entity_type_id);
$definition = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
$data_table = $definition->getDataTable();
$revision_data_table = $definition->getRevisionDataTable();
$entity_storage_schema_sql = \Drupal::keyValue('entity.storage_schema.sql');
$schema = \Drupal::database()->schema();
// Update the field storage definition.
foreach ($fields as $field_name) {
$field_storage_definitions[$field_name]->setSetting('max_length', $field_length);
}
$definition_update_manager->updateFieldableEntityType($entity_type, $field_storage_definitions);
// Update table schema.
foreach ($fields as $field_name) {
$schema_key = "$entity_type_id.field_schema_data.$field_name";
if ($field_schema_data = $entity_storage_schema_sql->get($schema_key)) {
// Update storage schema for data table.
$field_schema_data[$data_table]['fields'][$field_name]['length'] = $field_length;
// Update storage schema for the revision table.
if ($revision_data_table) {
$field_schema_data[$revision_data_table]['fields'][$field_name]['length'] = $field_length;
}
$entity_storage_schema_sql->set($schema_key, $field_schema_data);
// Update the base database field.
$schema->changeField($data_table, $field_name, $field_name, $field_schema_data[$data_table]['fields'][$field_name]);
// Update schema for the revision table.
if ($revision_data_table) {
$schema->changeField($revision_data_table, $field_name, $field_name, $field_schema_data[$revision_data_table]['fields'][$field_name]);
}
}
}
}
/**
* Update the field storage definitions for first_name and last_name fields.
*/
function apigee_edge_update_8102() {
$entity_type_id = 'user';
$fields = ['first_name', 'last_name'];
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = Drupal::service('entity_field.manager');
/** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
$last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$field_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
$original_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
foreach ($fields as $field) {
$original_storage_definitions[$field] = $field_definitions[$field];
}
// The schema updates are already handled in apigee_edge_update_8101().
// This updates the last installed definition.
$last_installed_schema_repository->setLastInstalledFieldStorageDefinitions($entity_type_id, $original_storage_definitions);
}
/**
* Assign the "add_api_key own developer_app" permission to authenticated users.
*/
function apigee_edge_update_8103() {
if (\Drupal::moduleHandler()->moduleExists('user')) {
$authenticated_user_permissions = [
'add_api_key own developer_app',
'revoke_api_key own developer_app',
'edit_api_products developer_app',
];
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, $authenticated_user_permissions);
}
}
/**
* Enable warnings field in default app view displays.
*/
function apigee_edge_update_8104() {
foreach (['developer_app', 'team_app'] as $entity_type_id) {
if (!\Drupal::entityTypeManager()->getDefinition($entity_type_id, FALSE)) {
continue;
}
/** @var \Drupal\Core\Entity\EntityDisplayRepository $entity_display_repository */
$entity_display_repository = \Drupal::service('entity_display.repository');
$entity_view_display = $entity_display_repository->getViewDisplay($entity_type_id, $entity_type_id);
if ($entity_view_display->getComponent('warnings')) {
continue;
}
$entity_view_display->setComponent('warnings', [
'region' => 'content',
'weight' => -100,
])->save();
}
}
/**
* Deprecated update function.
*/
function apigee_edge_update_8105() {
}
/**
* Update the default environment config for app analytics.
*/
function apigee_edge_update_8106() {
$config_factory = \Drupal::configFactory();
$app_settings = $config_factory->getEditable('apigee_edge.common_app_settings');
$app_settings
->set('analytics_available_environments', [$app_settings->get('analytics_environment')])
->save(TRUE);
}
/**
* Remove OAuth token data file.
*/
function apigee_edge_update_8107() {
// Replaced unserialize with Json function.
// We do not want the serialize data so deleting the file.
$storage = \Drupal::service('apigee_edge.authentication.oauth_token_storage');
if ($storage instanceof OauthTokenFileStorage) {
$storage->removeTokenFile();
}
}
/**
* Update the field storage defination of Developer App.
*/
function apigee_edge_update_9001() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
/** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
$last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$entity_type_id = 'developer_app';
$entity_type = $definition_update_manager->getEntityType($entity_type_id);
$field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
$field_storage_definitions['apiProducts'] = BaseFieldDefinition::create('string')
->setName('apiproducts')
->setTargetEntityTypeId($entity_type_id)
->setTargetBundle(NULL)
->setStorageRequired(FALSE)
->setInternal(TRUE)
->setTranslatable(FALSE)
->setRevisionable(FALSE);
$definition_update_manager->updateFieldableEntityType($entity_type, $field_storage_definitions);
}
/**
* Install Configs for Core Entity View Display for Default Team App
*/
function apigee_edge_update_9002() {
// Update existing config.
/** @var \Drupal\Core\Config\StorageInterface $config_storage */
$config_storage = \Drupal::service('config.storage');
$module_path = \Drupal::service('extension.list.module')->getPath('apigee_edge');
$source = new FileStorage("$module_path/config/install");
$new_edge_settings = $source->read('core.entity_view_display.developer_app.developer_app.default');
$edge_settings = $config_storage->read('core.entity_view_display.developer_app.developer_app.default');
$edge_settings['content']['callbackUrl'] = $new_edge_settings['content']['callbackUrl'];
$config_storage->write('core.entity_view_display.developer_app.developer_app.default', $edge_settings);
}