|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2025 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\Service; |
| 22 | + |
| 23 | +use Drupal\apigee_edge\Entity\Controller\OrganizationControllerInterface; |
| 24 | +use Drupal\apigee_edge\SDKConnectorInterface; |
| 25 | +use Apigee\Edge\Api\ApigeeX\Controller\AppGroupAppCredentialController; |
| 26 | +use Apigee\Edge\Api\Management\Entity\AppCredentialInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * Handles AppGroup scopes after API products have been added to a credential. |
| 30 | + */ |
| 31 | +class AppGroupScopeManager { |
| 32 | + |
| 33 | + /** |
| 34 | + * The SDK connector. |
| 35 | + * |
| 36 | + * @var \Drupal\apigee_edge\SDKConnectorInterface |
| 37 | + */ |
| 38 | + protected $sdkConnector; |
| 39 | + |
| 40 | + /** |
| 41 | + * The organization controller. |
| 42 | + * |
| 43 | + * @var \Drupal\apigee_edge\Entity\Controller\OrganizationControllerInterface |
| 44 | + */ |
| 45 | + protected $organizationController; |
| 46 | + |
| 47 | + /** |
| 48 | + * AppGroupScopeManager constructor. |
| 49 | + * |
| 50 | + * @param \Drupal\apigee_edge\SDKConnectorInterface $sdkConnector |
| 51 | + * The SDK connector. |
| 52 | + * @param \Drupal\apigee_edge\Entity\Controller\OrganizationControllerInterface $organizationController |
| 53 | + * The organization controller. |
| 54 | + */ |
| 55 | + public function __construct(SDKConnectorInterface $sdkConnector, OrganizationControllerInterface $organizationController) { |
| 56 | + $this->sdkConnector = $sdkConnector; |
| 57 | + $this->organizationController = $organizationController; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Overrides AppGroup scopes if necessary. |
| 62 | + * |
| 63 | + * @param array $originalScopes |
| 64 | + * The original scopes. |
| 65 | + * @param \Apigee\Edge\Api\Management\Entity\AppCredentialInterface $credential |
| 66 | + * The credential. |
| 67 | + * @param string $ownerId |
| 68 | + * The owner id. |
| 69 | + * @param string $appName |
| 70 | + * The app name. |
| 71 | + */ |
| 72 | + public function overrideScopes(array $originalScopes, AppCredentialInterface $credential, string $ownerId, string $appName): void { |
| 73 | + if (!$this->organizationController->isOrganizationApigeeX()) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $client = $this->sdkConnector->getClient(); |
| 78 | + $organization = $this->sdkConnector->getOrganization(); |
| 79 | + $controller = new AppGroupAppCredentialController($organization, $ownerId, $appName, $client); |
| 80 | + $controller->overrideAppGroupScopes($credential->getConsumerKey(), $originalScopes); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments