-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathDeveloperAppRouteProvider.php
345 lines (313 loc) · 13.5 KB
/
DeveloperAppRouteProvider.php
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
<?php
/**
* 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.
*/
namespace Drupal\apigee_edge\Entity;
use Drupal\apigee_edge\Access\AppsPageAccessCheck;
use Drupal\apigee_edge\Controller\DeveloperAppViewControllerForDeveloper;
use Drupal\apigee_edge\Controller\DeveloperAppKeysController;
use Drupal\apigee_edge\Entity\ListBuilder\DeveloperAppListBuilderForDeveloper;
use Drupal\apigee_edge\Form\DeveloperAppAnalyticsFormForDeveloper;
use Drupal\apigee_edge\Form\DeveloperAppApiKeyDeleteForm;
use Drupal\apigee_edge\Form\DeveloperAppApiKeyAddForm;
use Drupal\apigee_edge\Form\DeveloperAppApiKeyRevokeForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\Routing\Route;
/**
* Default entity routes for developer apps.
*/
class DeveloperAppRouteProvider extends AppRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type->id();
if ($collection_by_developer = $this->getCollectionRouteByDeveloper($entity_type)) {
$collection->add("entity.{$entity_type_id}.collection_by_developer", $collection_by_developer);
}
if ($canonical_by_developer = $this->getCanonicalRouteByDeveloper($entity_type)) {
$collection->add("entity.{$entity_type_id}.canonical_by_developer", $canonical_by_developer);
}
if ($add_form_for_developer = $this->getAddFormRouteForDeveloper($entity_type)) {
$collection->add("entity.{$entity_type_id}.add_form_for_developer", $add_form_for_developer);
}
if ($edit_form_for_developer = $this->getEditFormRouteForDeveloper($entity_type)) {
$collection->add("entity.{$entity_type_id}.edit_form_for_developer", $edit_form_for_developer);
}
if ($delete_form_for_developer = $this->getDeleteFormRouteForDeveloper($entity_type)) {
$collection->add("entity.{$entity_type_id}.delete_form_for_developer", $delete_form_for_developer);
}
if ($analytics_for_developer = $this->getAnalyticsRouteForDeveloper($entity_type)) {
$collection->add("entity.{$entity_type_id}.analytics_for_developer", $analytics_for_developer);
}
if ($api_keys = $this->getApiKeyRouteDeveloperApp($entity_type)) {
$collection->add("entity.{$entity_type_id}.api_keys", $api_keys);
}
if ($add_api_key_form = $this->getAddApiKeyRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.add_api_key_form", $add_api_key_form);
}
if ($delete_api_key_form = $this->getDeleteApiKeyRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.delete_api_key_form", $delete_api_key_form);
}
if ($revoke_api_key_form = $this->getRevokeApiKeyRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.revoke_api_key_form", $revoke_api_key_form);
}
return $collection;
}
/**
* {@inheritdoc}
*/
protected function getAddFormRoute(EntityTypeInterface $entity_type) {
$route = parent::getAddFormRoute($entity_type);
if ($route) {
// We did not want to expose this UI to regular users this the reason
// why route is only available to admins for now.
$route->setRequirement('_permission', 'administer developer_app');
}
return $route;
}
/**
* {@inheritdoc}
*/
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
$route = parent::getCollectionRoute($entity_type);
if ($route) {
// Add "access developer_app overview" to the autogenerated permission
// requirement" (that contains "administer developer_app").
$route->setRequirement('_permission', $route->getRequirement('_permission') . '+access developer_app overview');
}
return $route;
}
/**
* Gets the add-form route for developer.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getAddFormRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('add-form-for-developer')) {
$route = new Route($entity_type->getLinkTemplate('add-form-for-developer'));
$route->setDefault('_entity_form', 'developer_app.add_for_developer');
$route->setDefault('_title_callback', AppTitleProvider::class . '::addTitle');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_custom_access', AppsPageAccessCheck::class . '::createAppAccess');
return $route;
}
}
/**
* Gets the edit-form route for a developer.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getEditFormRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('edit-form-for-developer')) {
$route = new Route($entity_type->getLinkTemplate('edit-form-for-developer'));
$route->setDefault('_entity_form', 'developer_app.edit_for_developer');
$route->setDefault('_title_callback', AppTitleProvider::class . '::editTitle');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'update');
// We must load the entity from Apigee Edge directly and omit cached
// version on edit forms.
$route->setOption('apigee_edge_load_unchanged_entity', 'true');
return $route;
}
}
/**
* Gets the delete-form route for a developer.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getDeleteFormRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('delete-form-for-developer')) {
$route = new Route($entity_type->getLinkTemplate('delete-form-for-developer'));
$route->setDefault('_entity_form', 'developer_app.delete_for_developer');
$route->setDefault('_title_callback', AppTitleProvider::class . '::deleteTitle');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'delete');
return $route;
}
}
/**
* Gets the canonical route for a developer.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getCanonicalRouteByDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('canonical-by-developer')) {
$route = new Route($entity_type->getLinkTemplate('canonical-by-developer'));
$route->setDefault('_controller', DeveloperAppViewControllerForDeveloper::class . '::view');
$route->setDefault('_title_callback', AppTitleProvider::class . ':title');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'view');
return $route;
}
}
/**
* Gets the collection route for a developer.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getCollectionRouteByDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('collection-by-developer')) {
$route = new Route($entity_type->getLinkTemplate('collection-by-developer'));
$route->setDefault('_controller', DeveloperAppListBuilderForDeveloper::class . '::render');
$route->setDefault('_title_callback', DeveloperAppListBuilderForDeveloper::class . '::pageTitle');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_custom_access', AppsPageAccessCheck::class . '::access');
return $route;
}
}
/**
* Gets the app analytics route for a developer.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getAnalyticsRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('analytics-for-developer')) {
$route = new Route($entity_type->getLinkTemplate('analytics-for-developer'));
$route->setDefault('_form', DeveloperAppAnalyticsFormForDeveloper::class);
$route->setDefault('_title_callback', AppTitleProvider::class . '::analyticsTitle');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'analytics');
return $route;
}
}
/**
* Gets the add-api-key-form route for a developer app.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getAddApiKeyRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('add-api-key-form')) {
$route = new Route($entity_type->getLinkTemplate('add-api-key-form'));
$route->setDefault('_form', DeveloperAppApiKeyAddForm::class);
$route->setDefault('_title', 'Add key');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'add_api_key');
return $route;
}
}
/**
* Gets the delete-api-key-form route for a developer app.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getDeleteApiKeyRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('delete-api-key-form')) {
$route = new Route($entity_type->getLinkTemplate('delete-api-key-form'));
$route->setDefault('_form', DeveloperAppApiKeyDeleteForm::class);
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'delete_api_key');
return $route;
}
}
/**
* Gets the revoke-api-key-form route for a developer app.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getRevokeApiKeyRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('revoke-api-key-form')) {
$route = new Route($entity_type->getLinkTemplate('revoke-api-key-form'));
$route->setDefault('_form', DeveloperAppApiKeyRevokeForm::class);
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'revoke_api_key');
return $route;
}
}
/**
* If route contains the {user} parameter add required changes to the route.
*
* @param \Symfony\Component\Routing\Route $route
* The route to be checked and altered if needed.
*/
private function ensureUserParameter(Route $route) {
if (strpos($route->getPath(), '{user}') !== FALSE) {
// Default validation criteria based on Drupal core paths.
$route->setRequirement('user', '\d+');
// Make sure the parameter gets up-casted.
// (This also ensures that we get an "Page not found" page if user with
// uid does not exist.)
$route->setOption('parameters', ['user' => ['type' => 'entity:user', 'converter' => 'paramconverter.entity']]);
}
}
/**
* Gets the api key route for a developer app.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getApiKeyRouteDeveloperApp(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('api-keys')) {
$route = new Route($entity_type->getLinkTemplate('api-keys'));
$route->setDefault('_controller', DeveloperAppKeysController::class . '::developerAppKeys');
$route->setDefault('_title_callback', AppTitleProvider::class . '::title');
$route->setDefault('entity_type_id', $entity_type->id());
$this->ensureUserParameter($route);
$route->setRequirement('_app_access_check_by_app_name', 'view');
return $route;
}
}
}