forked from apigee/apigee-edge-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapigee_edge.install
248 lines (216 loc) · 8.08 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
<?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.
*/
/**
* @file
* Install, update and uninstall functions for Apigee Edge.
*/
use Apigee\Edge\Utility\OrganizationFeatures;
use Drupal\apigee_edge\OauthTokenFileStorage;
use Drupal\Core\Url;
use Drupal\user\RoleInterface;
/**
* Implements hook_requirements().
*/
function apigee_edge_requirements($phase) {
$requirements = [];
if ($phase === 'install') {
// This should be checked only if Drupal is installed.
if (!drupal_installation_attempted()) {
$missing_mails = \Drupal::entityQuery('user')
->notExists('mail')
->condition('uid', '0', '<>')
->execute();
if (!empty($missing_mails)) {
$requirements['apigee_edge_missing_mail'] = [
'title' => t('Apigee Edge'),
'description' => t('The module can be installed only if all users have emails in Drupal, because email is a required attribute on Apigee Edge.'),
'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 Edge'),
'value' => $exception->getMessage(),
'description' => t('Cannot connect to Apigee Edge server. You have either given wrong credential details or the Apigee Edge server is unreachable. Visit the <a href=":url">Apigee Edge general settings</a> page to get more information.', [
':url' => Url::fromRoute('apigee_edge.settings', ['destination' => 'admin/reports/status'])->toString(),
]),
'severity' => REQUIREMENT_WARNING,
];
}
}
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',
];
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);
}