Skip to content

Commit 1510d3b

Browse files
Fix for scope issue using service (#1195)
1 parent c264bba commit 1510d3b

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

modules/apigee_edge_teams/apigee_edge_teams.services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,7 @@ services:
157157
class: Drupal\apigee_edge_teams\User\RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer
158158
decorates: apigee_edge.post_user_delete_action_performer
159159
arguments: [ '@apigee_edge_teams.post_user_delete_action_performer.inner', '@entity_type.manager', '@logger.channel.apigee_edge_teams' ]
160+
161+
apigee_edge_teams.app_group_scope_manager:
162+
class: Drupal\apigee_edge_teams\Service\AppGroupScopeManager
163+
arguments: ['@apigee_edge.sdk_connector', '@apigee_edge.controller.organization']
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/Entity/Controller/AppCredentialControllerBase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,17 @@ public function __construct(string $owner, string $app_name, SDKConnectorInterfa
116116
* {@inheritdoc}
117117
*/
118118
public function addProducts(string $consumer_key, array $api_products): AppCredentialInterface {
119+
// Keep the original scopes from before the products are added.
120+
$originalScopes = [];
121+
if ($this->getAppType() === 'team') {
122+
$originalScopes = $this->load($consumer_key)->getScopes();
123+
}
119124
$credential = $this->decorated()->addProducts($consumer_key, $api_products);
125+
if ($this->getAppType() === 'team' && !empty($originalScopes) && \Drupal::hasService('apigee_edge_teams.app_group_scope_manager')) {
126+
$app_group_scope_manager = \Drupal::service('apigee_edge_teams.app_group_scope_manager');
127+
$app_group_scope_manager->overrideScopes($originalScopes, $credential, $this->owner, $this->appName);
128+
}
129+
120130
$this->eventDispatcher->dispatch(
121131
new AppCredentialAddApiProductEvent($this->getAppType(), $this->owner, $this->appName, $credential, $api_products),
122132
AppCredentialAddApiProductEvent::EVENT_NAME

0 commit comments

Comments
 (0)