|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2020 Google Inc. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License |
| 8 | + * version 2 as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | + * MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Drupal\apigee_edge_teams\EventSubscriber; |
| 22 | + |
| 23 | +use Drupal\apigee_edge_teams\Entity\TeamInterface; |
| 24 | +use Drupal\Core\DependencyInjection\ClassResolverInterface; |
| 25 | +use Drupal\Core\Routing\RouteMatchInterface; |
| 26 | +use Drupal\Core\Session\AccountInterface; |
| 27 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 28 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 29 | +use Symfony\Component\HttpFoundation\Response; |
| 30 | +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
| 31 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 32 | + |
| 33 | +/** |
| 34 | + * Displays an error message on team pages if team is inactive. |
| 35 | + */ |
| 36 | +class TeamInactiveStatusSubscriber implements EventSubscriberInterface { |
| 37 | + |
| 38 | + use StringTranslationTrait; |
| 39 | + |
| 40 | + /** |
| 41 | + * The class resolver service. |
| 42 | + * |
| 43 | + * @var \Drupal\Core\Controller\ControllerResolverInterface |
| 44 | + */ |
| 45 | + protected $classResolver; |
| 46 | + |
| 47 | + /** |
| 48 | + * The route match service. |
| 49 | + * |
| 50 | + * @var \Drupal\Core\Routing\RouteMatchInterface |
| 51 | + */ |
| 52 | + protected $routeMatch; |
| 53 | + |
| 54 | + /** |
| 55 | + * The available main content renderer services, keyed per format. |
| 56 | + * |
| 57 | + * @var array |
| 58 | + */ |
| 59 | + protected $mainContentRenderers; |
| 60 | + |
| 61 | + /** |
| 62 | + * The current user. |
| 63 | + * |
| 64 | + * @var \Drupal\Core\Session\AccountInterface |
| 65 | + */ |
| 66 | + protected $currentUser; |
| 67 | + |
| 68 | + /** |
| 69 | + * TeamInactiveStatusSubscriber constructor. |
| 70 | + * |
| 71 | + * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver |
| 72 | + * The class resolver service. |
| 73 | + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match |
| 74 | + * The route match service. |
| 75 | + * @param array $main_content_renderers |
| 76 | + * The available main content renderer service IDs. |
| 77 | + * @param \Drupal\Core\Session\AccountInterface $current_user |
| 78 | + * The current user. |
| 79 | + */ |
| 80 | + public function __construct(ClassResolverInterface $class_resolver, RouteMatchInterface $route_match, array $main_content_renderers, AccountInterface $current_user) { |
| 81 | + $this->classResolver = $class_resolver; |
| 82 | + $this->routeMatch = $route_match; |
| 83 | + $this->mainContentRenderers = $main_content_renderers; |
| 84 | + $this->currentUser = $current_user; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Display an error message on inactive team routes. |
| 89 | + * |
| 90 | + * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event |
| 91 | + * The event to process. |
| 92 | + */ |
| 93 | + public function onRespond(FilterResponseEvent $event) { |
| 94 | + if ($this->currentUser->isAnonymous() || !in_array($this->routeMatch->getRouteName(), $this->getDisabledRoutes())) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */ |
| 99 | + $team = $this->routeMatch->getParameter('team'); |
| 100 | + if (!$team || $team->getStatus() !== TeamInterface::STATUS_INACTIVE) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + $content = [ |
| 105 | + 'content' => [ |
| 106 | + '#theme' => 'status_messages', |
| 107 | + '#message_list' => [ |
| 108 | + 'error' => [ |
| 109 | + $this->t('The %team_name @team is inactive. This operation is now allowed.', [ |
| 110 | + '%team_name' => $team->label(), |
| 111 | + '@team' => $team->getEntityType()->getSingularLabel(), |
| 112 | + ]), |
| 113 | + ], |
| 114 | + ], |
| 115 | + ], |
| 116 | + ]; |
| 117 | + |
| 118 | + $renderer = $this->classResolver->getInstanceFromDefinition($this->mainContentRenderers['html']); |
| 119 | + /* @var \Symfony\Component\HttpFoundation\Response $response */ |
| 120 | + $response = $renderer->renderResponse($content, $event->getRequest(), $this->routeMatch); |
| 121 | + $response->setStatusCode(Response::HTTP_FORBIDDEN); |
| 122 | + |
| 123 | + $event->setResponse($response); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * {@inheritdoc} |
| 128 | + */ |
| 129 | + public static function getSubscribedEvents() { |
| 130 | + $events[KernelEvents::RESPONSE][] = ['onRespond', 5]; |
| 131 | + return $events; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns an array of route names for which the warning message should be displayed. |
| 136 | + * |
| 137 | + * @return array |
| 138 | + * An array of route names. |
| 139 | + */ |
| 140 | + public static function getDisabledRoutes(): array { |
| 141 | + return [ |
| 142 | + // Team. |
| 143 | + 'entity.team.edit_form', |
| 144 | + 'entity.team.delete_form', |
| 145 | + |
| 146 | + // Team app. |
| 147 | + 'entity.team_app.add_form_for_team', |
| 148 | + 'entity.team_app.edit_form', |
| 149 | + 'entity.team_app.delete_form', |
| 150 | + 'entity.team_app.analytics', |
| 151 | + |
| 152 | + // Team member. |
| 153 | + 'entity.team.add_members', |
| 154 | + 'entity.team.member.edit', |
| 155 | + 'entity.team.member.remove', |
| 156 | + |
| 157 | + // Team invitation. |
| 158 | + 'entity.team_invitation.resend_form', |
| 159 | + 'entity.team_invitation.delete_form', |
| 160 | + ]; |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments