View source
<?php
namespace Drupal\apigee_edge_teams\Entity;
use Drupal\apigee_edge\Entity\AppRouteProvider;
use Drupal\apigee_edge\Entity\AppTitleProvider;
use Drupal\apigee_edge_teams\Entity\ListBuilder\TeamAppListByTeam;
use Drupal\apigee_edge_teams\Controller\TeamAppKeysController;
use Drupal\apigee_edge_teams\Form\TeamAppApiKeyDeleteForm;
use Drupal\apigee_edge_teams\Form\TeamAppApiKeyAddForm;
use Drupal\apigee_edge_teams\Form\TeamAppApiKeyRevokeForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\Routing\Route;
class TeamAppRouteProvider extends AppRouteProvider {
use TeamRoutingHelperTrait;
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type
->id();
foreach ($collection as $route) {
$this
->alterRoutesWithAppName($route);
}
if ($add_form_for_team = $this
->getAddFormRouteForTeam($entity_type)) {
$collection
->add("entity.{$entity_type_id}.add_form_for_team", $add_form_for_team);
}
if ($collection_by_team = $this
->getCollectionRouteByTeam($entity_type)) {
$collection
->add("entity.{$entity_type_id}.collection_by_team", $collection_by_team);
}
if ($api_keys = $this
->getTeamApiKeysRoute($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;
}
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
$route = parent::getCollectionRoute($entity_type);
if ($route) {
$requirements = $route
->getRequirements();
$permission = TeamAppPermissionProvider::MANAGE_TEAM_APPS_PERMISSION;
if (isset($requirements['_permission'])) {
$requirements['_permission'] .= "+{$permission}";
}
else {
$requirements['_permission'] = $permission;
}
$route
->setRequirements($requirements);
}
return $route;
}
protected function getAddFormRouteForTeam(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('add-form-for-team')) {
$route = new Route($entity_type
->getLinkTemplate('add-form-for-team'));
$route
->setDefault('_entity_form', 'team_app.add_for_team');
$route
->setDefault('_title_callback', AppTitleProvider::class . '::addTitle');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureTeamParameter($route);
$route
->setRequirement('_entity_create_access', $entity_type
->id());
return $route;
}
}
protected function getCollectionRouteByTeam(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('collection-by-team')) {
$route = new Route($entity_type
->getLinkTemplate('collection-by-team'));
$route
->setDefault('_controller', TeamAppListByTeam::class . '::render');
$route
->setDefault('_title_callback', TeamAppListByTeam::class . '::pageTitle');
$this
->ensureTeamParameter($route);
$route
->setRequirement('_apigee_edge_teams_team_app_list_by_team_access', 'TRUE');
return $route;
}
}
protected function getTeamApiKeysRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('api-keys')) {
$route = new Route($entity_type
->getLinkTemplate('api-keys'));
$route
->setDefault('_controller', TeamAppKeysController::class . '::teamAppKeys');
$route
->setDefault('_title_callback', AppTitleProvider::class . '::title');
$this
->ensureTeamParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'view');
return $route;
}
}
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', TeamAppApiKeyAddForm::class);
$route
->setDefault('_title', 'Add key');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureTeamParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'add_api_key');
return $route;
}
}
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', TeamAppApiKeyDeleteForm::class);
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureTeamParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'delete_api_key');
return $route;
}
}
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', TeamAppApiKeyRevokeForm::class);
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureTeamParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'revoke_api_key');
return $route;
}
}
private function alterRoutesWithAppName(Route $route) : void {
if (strpos($route
->getPath(), '{app}') !== FALSE) {
$options = $route
->getOptions();
unset($options['parameters']['team_app']);
$route
->setOptions($options);
$requirements = $route
->getRequirements();
list(, $operation) = explode('.', $requirements['_entity_access']);
$requirements['_app_access_check_by_app_name'] = $operation;
unset($requirements['_entity_access']);
$route
->setRequirements($requirements);
}
}
}