|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2023 Google Inc. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU General Public License version 2 as published by the |
| 8 | + * Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 13 | + * License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License along |
| 16 | + * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +namespace Drupal\apigee_edge_teams\EventSubscriber; |
| 21 | + |
| 22 | +use Drupal\apigee_edge\Entity\Controller\OrganizationControllerInterface; |
| 23 | +use Drupal\apigee_edge\SDKConnectorInterface; |
| 24 | +use Drupal\Core\Messenger\MessengerInterface; |
| 25 | +use Drupal\Core\Session\AccountInterface; |
| 26 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 27 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 28 | +use Symfony\Component\HttpKernel\Event\RequestEvent; |
| 29 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 30 | + |
| 31 | +/** |
| 32 | + * Validates that Apigee X Team is enabled on every Team page request. |
| 33 | + */ |
| 34 | +final class ValidateApigeeXTeamEnabledSubscriber implements EventSubscriberInterface { |
| 35 | + |
| 36 | + use StringTranslationTrait; |
| 37 | + |
| 38 | + /** |
| 39 | + * The current user. |
| 40 | + * |
| 41 | + * @var \Drupal\Core\Session\AccountInterface |
| 42 | + */ |
| 43 | + private AccountInterface $currentUser; |
| 44 | + |
| 45 | + /** |
| 46 | + * The SDK connector service. |
| 47 | + * |
| 48 | + * @var \Drupal\apigee_edge\SDKConnectorInterface |
| 49 | + */ |
| 50 | + private SDKConnectorInterface $connector; |
| 51 | + |
| 52 | + /** |
| 53 | + * The organization controller service. |
| 54 | + * |
| 55 | + * @var \Drupal\apigee_edge\Entity\Controller\OrganizationControllerInterface |
| 56 | + */ |
| 57 | + protected OrganizationControllerInterface $orgController; |
| 58 | + |
| 59 | + /** |
| 60 | + * The messenger service. |
| 61 | + * |
| 62 | + * @var \Drupal\Core\Messenger\MessengerInterface |
| 63 | + */ |
| 64 | + protected MessengerInterface $messenger; |
| 65 | + |
| 66 | + /** |
| 67 | + * ValidateApigeeXTeamEnabledSubscriber constructor. |
| 68 | + * |
| 69 | + * @param \Drupal\Core\Session\AccountInterface $current_user |
| 70 | + * The current user. |
| 71 | + * @param \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector |
| 72 | + * The SDK connector service. |
| 73 | + * @param \Drupal\apigee_edge\Entity\Controller\OrganizationControllerInterface $org_controller |
| 74 | + * The organization controller service. |
| 75 | + * @param \Drupal\Core\Messenger\MessengerInterface $messenger |
| 76 | + * The messenger service. |
| 77 | + */ |
| 78 | + public function __construct(AccountInterface $current_user, SDKConnectorInterface $sdk_connector, OrganizationControllerInterface $org_controller, MessengerInterface $messenger) { |
| 79 | + $this->currentUser = $current_user; |
| 80 | + $this->connector = $sdk_connector; |
| 81 | + $this->orgController = $org_controller; |
| 82 | + $this->messenger = $messenger; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * If monetization enabled in Apigee X org alert the user. |
| 87 | + * |
| 88 | + * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event |
| 89 | + * The event. |
| 90 | + */ |
| 91 | + public function validateApigeexTeamEnabled(RequestEvent $event): void { |
| 92 | + // Check only for html request and admin users. |
| 93 | + if ($this->currentUser->hasPermission('administer modules') && $event->getRequest()->getRequestFormat() === 'html') { |
| 94 | + /** @var \Symfony\Component\Routing\Route $current_route */ |
| 95 | + if (($current_route = $event->getRequest()->get('_route')) && (strpos($current_route, 'entity.team') !== FALSE || strpos($current_route, 'settings.team') !== FALSE)) { |
| 96 | + $organization = $this->orgController->load($this->connector->getOrganization()); |
| 97 | + if ($organization && $this->orgController->isOrganizationApigeeX()) { |
| 98 | + if ($organization->getAddonsConfig() && $organization->getAddonsConfig()->getMonetizationConfig() && TRUE === $organization->getAddonsConfig()->getMonetizationConfig()->getEnabled()) { |
| 99 | + $this->messenger->addError($this->t('The Teams module functionality is not available for monetization enabled org on Apigee X / Hybrid and should be uninstalled.')); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * {@inheritdoc} |
| 108 | + */ |
| 109 | + public static function getSubscribedEvents(): array { |
| 110 | + $events[KernelEvents::REQUEST][] = ['validateApigeexTeamEnabled']; |
| 111 | + return $events; |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments