-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathapigee_kickstart_enhancement.module
313 lines (287 loc) · 10.4 KB
/
apigee_kickstart_enhancement.module
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
<?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
* Main module file for Apigee Kickstart Enhancement.
*/
use Apigee\Edge\Api\Management\Entity\AppCredentialInterface;
use Apigee\Edge\Structure\CredentialProduct;
use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Entity\EdgeEntityViewBuilder;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
/*
* Load all theme functions.
*/
require_once __DIR__ . '/apigee_kickstart_enhancement.theme.inc';
/**
* Implements hook_theme().
*/
function apigee_kickstart_enhancement_theme($existing, $type, $theme, $path) {
return [
'secret' => [
'render element' => 'elements',
'file' => 'apigee_kickstart_enhancement.theme.inc',
],
'pseudo_field' => [
'variables' => ['field' => NULL],
'file' => 'apigee_kickstart_enhancement.theme.inc',
],
];
}
/**
* Implements hook_local_tasks_alter().
*/
function apigee_kickstart_enhancement_local_tasks_alter(&$local_tasks) {
// Rename the user local tasks and show these first.
if (isset($local_tasks['entity.user.canonical'])) {
$local_tasks['entity.user.canonical']['title'] = t('Profile home');
$local_tasks['entity.user.canonical']['weight'] = -10;
}
if (isset($local_tasks['entity.user.edit_form'])) {
$local_tasks['entity.user.edit_form']['title'] = t('Edit profile');
$local_tasks['entity.user.edit_form']['weight'] = -9;
}
}
/**
* Implements hook_entity_type_alter().
*/
function apigee_kickstart_enhancement_entity_type_alter(array &$entity_types) {
// Update the view builder for apidoc.
if (isset($entity_types['apidoc'])) {
$entity_types['apidoc']->setViewBuilderClass(EdgeEntityViewBuilder::class);
}
}
/**
* Implements hook_preprocess().
*/
function apigee_kickstart_enhancement_preprocess_menu(&$variables) {
// Hide the registration link for authenticated users.
foreach ($variables['items'] as $key => &$item) {
if ($item['url']->isRouted() && $item['url']->getRouteName() === 'user.register' && \Drupal::currentUser()->isAuthenticated()) {
unset($variables['items'][$key]);
}
}
}
/**
* Implements hook_entity_extra_field_info().
*/
function apigee_kickstart_enhancement_entity_extra_field_info() {
$fields = [];
// Make the user fields configurable.
foreach (_apigee_kickstart_user_extra_fields() as $field_name => $field_info) {
$fields['user']['user']['display'][$field_name] = [
'label' => $field_info['label'],
'weight' => 10,
'visible' => FALSE,
];
}
// Add warnings to app.
foreach (\Drupal::service('apigee_kickstart.enhancer')->getAppEntityTypes() as $entity_type_id => $app_entity_type) {
$fields[$entity_type_id][$entity_type_id]['display']['warnings'] = [
'label' => t('Warnings'),
'weight' => 10,
'visible' => FALSE,
];
}
return $fields;
}
/**
* Implements hook_entity_view().
*/
function apigee_kickstart_enhancement_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
$entity_type = $entity->getEntityType();
if (\Drupal::service('apigee_kickstart.enhancer')
->isAppEntityType($entity_type)) {
$warnings = _apigee_kickstart_enhancement_check_app_warnings($entity, $entity_type);
if (count($warnings)) {
$build['warnings'] = [
'#theme' => 'status_messages',
'#message_list' => [
'warning' => $warnings,
],
];
}
}
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function apigee_kickstart_enhancement_user_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
foreach (_apigee_kickstart_user_extra_fields() as $field_name => $field) {
if ($display->getComponent($field_name)) {
$value = is_callable($field['value_callback']) ? $field['value_callback']($entity) : NULL;
if ($value) {
// Render as pseudo-fields.
$build[$field_name] = [
'#theme' => 'pseudo_field',
'#field' => [
'name' => $field_name,
'label' => $field['label'],
'value' => $value,
],
];
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function apigee_kickstart_enhancement_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Alter the billing details form.
if (strpos($form_id, '_billing_details_form')) {
$form['#attributes']['class'] = 'form-editable';
// Add a form heading.
$form['heading'] = [
'#type' => 'html_tag',
'#value' => t('Billing Details'),
'#tag' => 'h4',
'#weight' => -100,
];
// Wrap all elements in a div.
$form['heading']['#suffix'] = '<div class="wrapper">';
$form['actions']['#suffix'] = '</div>';
// Render fieldsets as simple containers.
foreach (Element::children($form) as $child) {
if ($form[$child]['#type'] === 'fieldset') {
$form[$child]['#type'] = 'container';
}
}
// Fix for billing type.
$form['billing']['billing_type']['#prefix'] = '<strong>' . $form['billing']['#title'] . ': </strong><span>';
$form['billing']['billing_type']['#suffix'] = '</span>';
$form['billing']['billing_type']['#markup'] = ucfirst(strtolower($form['billing']['billing_type']['#markup']));
}
}
/**
* Implements hook_apigee_m10n_prepaid_balance_list_alter().
*/
function apigee_kickstart_enhancement_apigee_m10n_prepaid_balance_list_alter(array &$build, EntityInterface $entity) {
// Add a title to the list.
$build['title'] = [
'#type' => 'html_tag',
'#value' => t('Prepaid Balance'),
'#tag' => 'h3',
'#weight' => -100,
];
// Remove the table caption.
$build['table']['#caption'] = '';
}
/**
* Returns an array of extra fields for the user profile.
*/
function _apigee_kickstart_user_extra_fields() {
return [
'display_name' => [
'label' => t('Name'),
'value_callback' => function (UserInterface $user) {
$first_name_last_name = trim($user->get('first_name')->value) . ' ' . trim($user->get('last_name')->value);
// If both fields were empty this string still could be empty.
$first_name_last_name = trim($first_name_last_name);
if (!empty($first_name_last_name)) {
return $first_name_last_name;
} else {
return $user->label();
}
},
],
'username' => [
'label' => t('Username'),
'value_callback' => function (UserInterface $user) {
return $user->getAccountName();
},
],
'email' => [
'label' => t('Email'),
'value_callback' => function (UserInterface $user) {
return $user->getEmail();
},
],
'roles' => [
'label' => t('Roles'),
'value_callback' => function (UserInterface $user) {
return implode(', ', $user->getRoles(TRUE));
},
],
'timezone' => [
'label' => t('Timezone'),
'value_callback' => function (UserInterface $user) {
return $user->getTimeZone();
},
],
];
}
/**
* Checks credentials of an app and returns warnings about them.
*
* TODO: Abstract
* \Drupal\apigee_edge\Entity\ListBuilder\AppListBuilder::checkAppCredentialWarnings.
*
* @param \Drupal\apigee_edge\Entity\AppInterface $app
* The app entity to be checked.
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The app entity type.
*
* @return array
* An associative array that contains information about the revoked
* credentials and revoked or pending API products in a credential.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function _apigee_kickstart_enhancement_check_app_warnings(AppInterface $app, EntityTypeInterface $entity_type) {
$warnings = [];
$warnings['revokedCred'] = FALSE;
$warnings['revokedOrPendingCredProduct'] = FALSE;
$warnings['expiredCred'] = FALSE;
foreach ($app->getCredentials() as $credential) {
// Check for expired credentials.
if (($expired_date = $credential->getExpiresAt()) && Drupal::time()->getRequestTime() - $expired_date->getTimestamp() > 0) {
$warnings['expiredCred'] = t('At least one of the credentials associated with this @app is expired.', [
'@app' => mb_strtolower($entity_type->getSingularLabel()),
]);
}
if ($credential->getStatus() === AppCredentialInterface::STATUS_APPROVED) {
foreach ($credential->getApiProducts() as $cred_product) {
if ($cred_product->getStatus() == CredentialProduct::STATUS_REVOKED || $cred_product->getStatus() == CredentialProduct::STATUS_PENDING) {
$args = [
'@app' => mb_strtolower($entity_type->getSingularLabel()),
'@api_product' => mb_strtolower(Drupal::entityTypeManager()->getDefinition('api_product')->getSingularLabel()),
'@status' => $cred_product->getStatus() == CredentialProduct::STATUS_REVOKED ? t('revoked') : t('pending'),
];
if (count($app->getCredentials()) === 1) {
/** @var \Drupal\apigee_edge\Entity\ApiProductInterface $apiProduct */
$api_product = Drupal::entityTypeManager()->getStorage('api_product')->load($cred_product->getApiproduct());
$args['%name'] = $api_product->label();
$warnings['revokedOrPendingCredProduct'] = t('%name @api_product associated with this @app is in @status status.', $args);
} else {
$warnings['revokedOrPendingCredProduct'] = t('At least one @api_product associated with one of the credentials of this @app is in @status status.', $args);
}
break;
}
}
}
}
return array_filter($warnings);
}