From 386572e199f8e4c5df7df2533c3e5769f48205c7 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 31 Oct 2019 16:53:28 +0400 Subject: [PATCH 1/4] [#156] Show expired credentials on the app key status --- apigee_edge.module | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apigee_edge.module b/apigee_edge.module index 555b98574..f32cebde9 100644 --- a/apigee_edge.module +++ b/apigee_edge.module @@ -1081,6 +1081,10 @@ function template_preprocess_app_credential(array &$variables) { ]; } elseif ($def['value_type'] === 'status') { + // Check if expired. + if ($normalized['expiresAt'] !== -1 && \Drupal::time()->getRequestTime() - intval($normalized['expiresAt'] / 1000) > 0) { + $value = t('Expired'); + } $variables['primary_wrapper'][$property]['value'] = [ '#type' => 'status_property', '#value' => $value, From 41f03d25f76bbbe5cad0e3b08e5b1eff6fc3ff25 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 31 Oct 2019 16:56:24 +0400 Subject: [PATCH 2/4] [#156] Show expired credentials warning in the app list builder --- src/Entity/ListBuilder/AppListBuilder.php | 25 ++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Entity/ListBuilder/AppListBuilder.php b/src/Entity/ListBuilder/AppListBuilder.php index 5e911a69c..fc6f24519 100644 --- a/src/Entity/ListBuilder/AppListBuilder.php +++ b/src/Entity/ListBuilder/AppListBuilder.php @@ -215,7 +215,7 @@ protected function buildWarningRow(AppInterface $app, array &$rows) { // - any credentials of the app is in revoked status // - any products of any credentials of the app is in revoked or // pending status. - if ($app->getStatus() === AppInterface::STATUS_APPROVED && ($warnings['revokedCred'] || $warnings['revokedOrPendingCredProduct'])) { + if ($app->getStatus() === AppInterface::STATUS_APPROVED && ($warnings['revokedCred'] || $warnings['revokedOrPendingCredProduct'] || $warnings['expiredCred'])) { $build['status'] = $rows[$info_row_css_id]['data']['status']['data']; $build['warning'] = [ '#type' => 'html_tag', @@ -246,12 +246,22 @@ protected function buildWarningRow(AppInterface $app, array &$rows) { 'colspan' => count($this->buildHeader()), ]; + $items = []; if ($warnings['revokedCred']) { - $row['data']['info']['data'] = $warnings['revokedCred']; + $items[] = $warnings['revokedCred']; } elseif ($warnings['revokedOrPendingCredProduct']) { - $row['data']['info']['data'] = $warnings['revokedOrPendingCredProduct']; + $items[] = $warnings['revokedOrPendingCredProduct']; } + + if ($warnings['expiredCred']) { + $items[] = $warnings['expiredCred']; + } + + $row['data']['info']['data'] = [ + '#theme' => 'item_list', + '#items' => $items, + ]; } $rows[$warning_row_css_id] = $row; @@ -302,6 +312,7 @@ protected function checkAppCredentialWarnings(AppInterface $app): array { $warnings = []; $warnings['revokedCred'] = FALSE; $warnings['revokedOrPendingCredProduct'] = FALSE; + $warnings['expiredCred'] = FALSE; foreach ($app->getCredentials() as $credential) { if ($credential->getStatus() === AppCredential::STATUS_REVOKED) { @@ -316,6 +327,14 @@ protected function checkAppCredentialWarnings(AppInterface $app): array { } break; } + + // Check for expired credentials. + if (($expired_date = $credential->getExpiresAt()) && \Drupal::time()->getRequestTime() - intval($expired_date->getTimestamp() / 1000) > 0) { + $warnings['expiredCred'] = $this->t('At least one of the credentials associated with this @app is expired.', [ + '@app' => $this->entityType->getLowercaseLabel(), + ]); + } + foreach ($credential->getApiProducts() as $cred_product) { if ($cred_product->getStatus() == CredentialProduct::STATUS_REVOKED || $cred_product->getStatus() == CredentialProduct::STATUS_PENDING) { $args = [ From ac39ba74da53ee0e6a09545557a89c950af5ed1d Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 31 Oct 2019 18:32:48 +0400 Subject: [PATCH 3/4] [#156] Implement code review suggestions --- apigee_edge.module | 2 +- .../Entity/ListBuilder/TeamAppListByTeam.php | 8 ++++++-- src/Entity/ListBuilder/AppListBuilder.php | 20 +++++++++++++++---- .../DeveloperAppListBuilderForDeveloper.php | 8 ++++++-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/apigee_edge.module b/apigee_edge.module index f32cebde9..4809f766d 100644 --- a/apigee_edge.module +++ b/apigee_edge.module @@ -1082,7 +1082,7 @@ function template_preprocess_app_credential(array &$variables) { } elseif ($def['value_type'] === 'status') { // Check if expired. - if ($normalized['expiresAt'] !== -1 && \Drupal::time()->getRequestTime() - intval($normalized['expiresAt'] / 1000) > 0) { + if ($normalized['expiresAt'] !== -1 && \Drupal::time()->getRequestTime() - (int)($normalized['expiresAt'] / 1000) > 0) { $value = t('Expired'); } $variables['primary_wrapper'][$property]['value'] = [ diff --git a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamAppListByTeam.php b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamAppListByTeam.php index dcd24e33c..d7753f0ad 100644 --- a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamAppListByTeam.php +++ b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamAppListByTeam.php @@ -21,6 +21,7 @@ namespace Drupal\apigee_edge_teams\Entity\ListBuilder; use Drupal\apigee_edge\Entity\ListBuilder\AppListBuilder; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -54,11 +55,13 @@ class TeamAppListByTeam extends AppListBuilder implements ContainerInjectionInte * The render. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack object. + * @param \Drupal\Component\Datetime\TimeInterface $time + * The time service. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match object. */ - public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, RequestStack $request_stack, RouteMatchInterface $route_match) { - parent::__construct($entity_type, $entity_type_manager, $render, $request_stack); + public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, RequestStack $request_stack, TimeInterface $time, RouteMatchInterface $route_match) { + parent::__construct($entity_type, $entity_type_manager, $render, $request_stack, $time); $this->routeMatch = $route_match; } @@ -71,6 +74,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('entity_type.manager'), $container->get('renderer'), $container->get('request_stack'), + $container->get('datetime.time'), $container->get('current_route_match') ); } diff --git a/src/Entity/ListBuilder/AppListBuilder.php b/src/Entity/ListBuilder/AppListBuilder.php index fc6f24519..a94dc382e 100644 --- a/src/Entity/ListBuilder/AppListBuilder.php +++ b/src/Entity/ListBuilder/AppListBuilder.php @@ -23,6 +23,7 @@ use Apigee\Edge\Api\Management\Entity\AppCredential; use Apigee\Edge\Structure\CredentialProduct; use Drupal\apigee_edge\Entity\AppInterface; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityInterface; @@ -53,6 +54,13 @@ class AppListBuilder extends EdgeEntityListBuilder { */ protected $requestStack; + /** + * The time service. + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; + /** * AppListBuilder constructor. * @@ -64,12 +72,15 @@ class AppListBuilder extends EdgeEntityListBuilder { * The renderer service. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack object. + * @param \Drupal\Component\Datetime\TimeInterface $time + * The time service. */ - public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, RequestStack $request_stack) { + public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, RequestStack $request_stack, TimeInterface $time) { parent::__construct($entity_type, $entity_type_manager); $this->renderer = $renderer; $this->entityTypeManager = $entity_type_manager; $this->requestStack = $request_stack; + $this->time = $time; } /** @@ -80,7 +91,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $entity_type, $container->get('entity_type.manager'), $container->get('renderer'), - $container->get('request_stack') + $container->get('request_stack'), + $container->get('datetime.time') ); } @@ -212,7 +224,7 @@ protected function buildWarningRow(AppInterface $app, array &$rows) { // Display warning sign next to the status if the app's status is // approved, but: - // - any credentials of the app is in revoked status + // - any credentials of the app is in revoked or expired status // - any products of any credentials of the app is in revoked or // pending status. if ($app->getStatus() === AppInterface::STATUS_APPROVED && ($warnings['revokedCred'] || $warnings['revokedOrPendingCredProduct'] || $warnings['expiredCred'])) { @@ -329,7 +341,7 @@ protected function checkAppCredentialWarnings(AppInterface $app): array { } // Check for expired credentials. - if (($expired_date = $credential->getExpiresAt()) && \Drupal::time()->getRequestTime() - intval($expired_date->getTimestamp() / 1000) > 0) { + if (($expired_date = $credential->getExpiresAt()) && $this->time->getRequestTime() - $expired_date->getTimestamp() > 0) { $warnings['expiredCred'] = $this->t('At least one of the credentials associated with this @app is expired.', [ '@app' => $this->entityType->getLowercaseLabel(), ]); diff --git a/src/Entity/ListBuilder/DeveloperAppListBuilderForDeveloper.php b/src/Entity/ListBuilder/DeveloperAppListBuilderForDeveloper.php index 8b5c9d482..0da47af77 100644 --- a/src/Entity/ListBuilder/DeveloperAppListBuilderForDeveloper.php +++ b/src/Entity/ListBuilder/DeveloperAppListBuilderForDeveloper.php @@ -22,6 +22,7 @@ use Drupal\apigee_edge\Entity\AppInterface; use Drupal\apigee_edge\Exception\DeveloperDoesNotExistException; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Component\Utility\Html; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityInterface; @@ -75,13 +76,15 @@ class DeveloperAppListBuilderForDeveloper extends AppListBuilder implements Cont * The render. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack object. + * @param \Drupal\Component\Datetime\TimeInterface $time + * The time service. * @param \Drupal\Core\Session\AccountInterface $current_user * Currently logged-in user. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match object. */ - public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, RequestStack $request_stack, AccountInterface $current_user, RouteMatchInterface $route_match) { - parent::__construct($entity_type, $entity_type_manager, $render, $request_stack); + public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, RequestStack $request_stack, TimeInterface $time, AccountInterface $current_user, RouteMatchInterface $route_match) { + parent::__construct($entity_type, $entity_type_manager, $render, $request_stack, $time); $this->currentUser = $current_user; $this->routeMatch = $route_match; } @@ -95,6 +98,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('entity_type.manager'), $container->get('renderer'), $container->get('request_stack'), + $container->get('datetime.time'), $container->get('current_user'), $container->get('current_route_match') ); From d1cc7c2ca14f50829a8c78d6eba66eb3ccb86cf8 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Mon, 4 Nov 2019 20:50:45 +0400 Subject: [PATCH 4/4] [#156] Fix phpcs notice --- apigee_edge.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apigee_edge.module b/apigee_edge.module index 4809f766d..c13f1e2bd 100644 --- a/apigee_edge.module +++ b/apigee_edge.module @@ -1082,7 +1082,7 @@ function template_preprocess_app_credential(array &$variables) { } elseif ($def['value_type'] === 'status') { // Check if expired. - if ($normalized['expiresAt'] !== -1 && \Drupal::time()->getRequestTime() - (int)($normalized['expiresAt'] / 1000) > 0) { + if ($normalized['expiresAt'] !== -1 && \Drupal::time()->getRequestTime() - (int) ($normalized['expiresAt'] / 1000) > 0) { $value = t('Expired'); } $variables['primary_wrapper'][$property]['value'] = [