Skip to content

Commit f828090

Browse files
Support apps with revoked credentials only (#786)
Co-authored-by: Shishir <[email protected]>
1 parent d235c18 commit f828090

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

src/Form/AppApiKeyAddFormBase.php

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
use Apigee\Edge\Api\Management\Entity\AppCredentialInterface;
2323
use Apigee\Edge\Structure\CredentialProductInterface;
24-
use Drupal\apigee_edge\Entity\AppInterface;
25-
use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
2624
use Drupal\Core\Cache\Cache;
2725
use Drupal\Core\Form\FormBase;
2826
use Drupal\Core\Form\FormStateInterface;
2927
use Drupal\Core\Url;
28+
use Drupal\apigee_edge\Entity\AppInterface;
29+
use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
3030

3131
/**
3232
* Provides app API key add base form.
@@ -58,7 +58,7 @@ public function getFormId() {
5858
* @return \Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface
5959
* The app api-key controller.
6060
*/
61-
abstract protected function appCredentialController(string $owner, string $app_name) : AppCredentialControllerInterface;
61+
abstract protected function appCredentialController(string $owner, string $app_name): AppCredentialControllerInterface;
6262

6363
/**
6464
* Returns the redirect url for the app.
@@ -165,54 +165,75 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
165165
* {@inheritdoc}
166166
*/
167167
public function submitForm(array &$form, FormStateInterface $form_state) {
168+
$t_args = [
169+
'@app' => $this->app->label(),
170+
];
168171
$expiry = $form_state->getValue('expiry');
169172
$expiry_date = $form_state->getValue('expiry_date');
170173
$expires_in = $expiry === 'date' ? (strtotime($expiry_date) - time()) * 1000 : -1;
171-
$selected_products = [];
174+
175+
$form_state->setRedirectUrl($this->getRedirectUrl());
172176

173177
$api_products = $this->getApiProductsForApp($this->app);
174-
if (count($api_products)) {
175-
$selected_products = array_map(function (CredentialProductInterface $api_product) {
176-
return $api_product->getApiproduct();
177-
}, $api_products);
178+
// @todo The "Add credential button must not be available when it cannot
179+
// be used.
180+
if ($api_products === []) {
181+
// Is this a skeleton key?
182+
$this->messenger()->addWarning($this->t('The @app @app_entity_label has no @apis associated.', $t_args + [
183+
'@app_entity_label' => $this->app->getEntityType()->getSingularLabel(),
184+
// @todo DI dependency.
185+
// phpcs:disable
186+
'@apis' => \Drupal::entityTypeManager()->getDefinition('api_product')->getPluralLabel(),
187+
// phpcs:enable
188+
]));
189+
return;
178190
}
179191

180-
$args = [
181-
'@app' => $this->app->label(),
182-
];
192+
$selected_products = array_map(static function (CredentialProductInterface $api_product) {
193+
return $api_product->getApiproduct();
194+
}, $api_products);
183195

184196
try {
185197
$this->appCredentialController($this->app->getAppOwner(), $this->app->getName())
186-
->generate($selected_products, $this->app->getAttributes(), $this->app->getCallbackUrl() ?? "", [], $expires_in);
198+
->generate($selected_products, $this->app->getAttributes(), $this->app->getCallbackUrl() ?? '', [], $expires_in);
187199
Cache::invalidateTags($this->app->getCacheTags());
188-
$this->messenger()->addStatus($this->t('New API key added to @app.', $args));
189-
$form_state->setRedirectUrl($this->getRedirectUrl());
200+
$this->messenger()->addStatus($this->t('New API key added to @app.', $t_args));
190201
}
191202
catch (\Exception $exception) {
192-
$this->messenger()->addError($this->t('Failed to add API key for @app.', $args));
203+
$this->messenger()->addError($this->t('Failed to add API key for @app.', $t_args));
193204
}
194205
}
195206

196207
/**
197208
* Helper to find API products based on the recently active API key.
198209
*
210+
* Returns the most recent approved credential, if there is any, otherwise
211+
* returns the most recent revoked credential.
212+
*
199213
* @param \Drupal\apigee_edge\Entity\AppInterface $app
200214
* The app entity.
201215
*
202-
* @return \Apigee\Edge\Structure\CredentialProductInterface[]|array
203-
* An array of API products.
216+
* @return \Apigee\Edge\Structure\CredentialProductInterface[]
217+
* An array of credential API products.
204218
*/
205219
protected function getApiProductsForApp(AppInterface $app): array {
206-
$approved_credentials = array_filter($app->getCredentials(), function (AppCredentialInterface $credential) {
207-
return $credential->getStatus() === AppCredentialInterface::STATUS_APPROVED;
208-
});
220+
if ($app->getCredentials() === []) {
221+
// Is this a skeleton key?
222+
return [];
223+
}
209224

210-
// Find the recently active one.
211-
usort($approved_credentials, function (AppCredentialInterface $a, AppCredentialInterface $b) {
225+
$credentials = $app->getCredentials();
226+
usort($credentials, static function (AppCredentialInterface $a, AppCredentialInterface $b) {
212227
return $b->getIssuedAt() <=> $a->getIssuedAt();
213228
});
214229

215-
return count($approved_credentials) ? $approved_credentials[0]->getApiProducts() : [];
230+
$approved_credentials = array_filter($app->getCredentials(), static function (AppCredentialInterface $credential) {
231+
return $credential->getStatus() === AppCredentialInterface::STATUS_APPROVED;
232+
});
233+
234+
$credential = $approved_credentials !== [] ? reset($approved_credentials) : reset($credentials);
235+
236+
return $credential->getApiProducts();
216237
}
217238

218239
}

0 commit comments

Comments
 (0)