Skip to content

Commit

Permalink
[apigee#431] Remove app warnings code from profile
Browse files Browse the repository at this point in the history
  • Loading branch information
shadcn committed Oct 15, 2020
1 parent 77e78dc commit dc4015c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ mode: collapsible_card
content:
callbackUrl:
label: inline
weight: 2
weight: 3
region: content
settings: { }
third_party_settings: { }
type: uri_link
createdAt:
type: timestamp_ago
label: inline
weight: 4
weight: 5
region: content
settings:
future_format: '@interval hence'
past_format: '@interval ago'
granularity: 2
third_party_settings: { }
credentials:
weight: 3
weight: 4
region: content
settings: { }
third_party_settings: { }
description:
label: inline
weight: 1
weight: 2
region: content
settings: { }
third_party_settings: { }
type: basic_string
lastModifiedAt:
type: timestamp_ago
label: inline
weight: 5
weight: 6
region: content
settings:
future_format: '@interval hence'
Expand All @@ -52,13 +52,18 @@ content:
status:
label: hidden
type: status_property
weight: 0
weight: 1
region: content
settings:
indicator_status_ok: approved
indicator_status_warning: ''
indicator_status_error: revoked
third_party_settings: { }
warnings:
weight: 0
region: content
settings: { }
third_party_settings: { }
hidden:
appFamily: true
appId: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ mode: default
content:
callbackUrl:
label: inline
weight: 2
weight: 3
region: content
settings: { }
third_party_settings: { }
type: uri_link
createdAt:
type: timestamp_ago
label: inline
weight: 4
weight: 5
region: content
settings:
future_format: '@interval hence'
past_format: '@interval ago'
granularity: 2
third_party_settings: { }
credentials:
weight: 3
weight: 4
region: content
settings: { }
third_party_settings: { }
description:
label: inline
weight: 1
weight: 2
region: content
settings: { }
third_party_settings: { }
type: basic_string
lastModifiedAt:
type: timestamp_ago
label: inline
weight: 5
weight: 6
region: content
settings:
future_format: '@interval hence'
Expand All @@ -50,13 +50,18 @@ content:
status:
label: inline
type: status_property
weight: 0
weight: 1
region: content
settings:
indicator_status_ok: approved
indicator_status_warning: ''
indicator_status_error: revoked
third_party_settings: { }
warnings:
weight: 0
region: content
settings: { }
third_party_settings: { }
hidden:
appFamily: true
appId: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
* Main module file for Apigee Kickstart Enhancement.
*/

use Apigee\Edge\Api\Management\Entity\AppCredentialInterface;
use Apigee\Edge\Structure\CredentialProduct;
use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Entity\EdgeEntityViewBuilder;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
Expand Down Expand Up @@ -107,37 +103,9 @@ function apigee_kickstart_enhancement_entity_extra_field_info() {
];
}

// Add warnings to app.
foreach (\Drupal::service('apigee_kickstart.enhancer')->getAppEntityTypes() as $entity_type_id => $app_entity_type) {
$fields[$entity_type_id][$entity_type_id]['display']['warnings'] = [
'label' => t('Warnings'),
'weight' => 10,
'visible' => FALSE,
];
}

return $fields;
}

/**
* Implements hook_entity_view().
*/
function apigee_kickstart_enhancement_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
$entity_type = $entity->getEntityType();
if (\Drupal::service('apigee_kickstart.enhancer')
->isAppEntityType($entity_type)) {
$warnings = _apigee_kickstart_enhancement_check_app_warnings($entity, $entity_type);
if (count($warnings)) {
$build['warnings'] = [
'#theme' => 'status_messages',
'#message_list' => [
'warning' => $warnings,
],
];
}
}
}

/**
* Implements hook_ENTITY_TYPE_view().
*/
Expand Down Expand Up @@ -254,60 +222,3 @@ function _apigee_kickstart_user_extra_fields() {
],
];
}

/**
* Checks credentials of an app and returns warnings about them.
*
* TODO: Abstract
* \Drupal\apigee_edge\Entity\ListBuilder\AppListBuilder::checkAppCredentialWarnings.
*
* @param \Drupal\apigee_edge\Entity\AppInterface $app
* The app entity to be checked.
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The app entity type.
*
* @return array
* An associative array that contains information about the revoked
* credentials and revoked or pending API products in a credential.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function _apigee_kickstart_enhancement_check_app_warnings(AppInterface $app, EntityTypeInterface $entity_type) {
$warnings = [];
$warnings['revokedCred'] = FALSE;
$warnings['revokedOrPendingCredProduct'] = FALSE;
$warnings['expiredCred'] = FALSE;

foreach ($app->getCredentials() as $credential) {
// Check for expired credentials.
if (($expired_date = $credential->getExpiresAt()) && Drupal::time()->getRequestTime() - $expired_date->getTimestamp() > 0) {
$warnings['expiredCred'] = t('At least one of the credentials associated with this @app is expired.', [
'@app' => mb_strtolower($entity_type->getSingularLabel()),
]);
}

if ($credential->getStatus() === AppCredentialInterface::STATUS_APPROVED) {
foreach ($credential->getApiProducts() as $cred_product) {
if ($cred_product->getStatus() == CredentialProduct::STATUS_REVOKED || $cred_product->getStatus() == CredentialProduct::STATUS_PENDING) {
$args = [
'@app' => mb_strtolower($entity_type->getSingularLabel()),
'@api_product' => mb_strtolower(Drupal::entityTypeManager()->getDefinition('api_product')->getSingularLabel()),
'@status' => $cred_product->getStatus() == CredentialProduct::STATUS_REVOKED ? t('revoked') : t('pending'),
];
if (count($app->getCredentials()) === 1) {
/** @var \Drupal\apigee_edge\Entity\ApiProductInterface $apiProduct */
$api_product = Drupal::entityTypeManager()->getStorage('api_product')->load($cred_product->getApiproduct());
$args['%name'] = $api_product->label();
$warnings['revokedOrPendingCredProduct'] = t('%name @api_product associated with this @app is in @status status.', $args);
} else {
$warnings['revokedOrPendingCredProduct'] = t('At least one @api_product associated with one of the credentials of this @app is in @status status.', $args);
}
break;
}
}
}
}

return array_filter($warnings);
}
4 changes: 4 additions & 0 deletions themes/custom/apigee_kickstart/includes/apigee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ function apigee_kickstart_preprocess_apigee_entity__app(array &$variables) {
function apigee_kickstart_preprocess_app_credential(&$variables) {
// Render the consumerKey and the consumerSecret as secret fields.
foreach (['consumerKey', 'consumerSecret'] as $field_name) {
if (empty($variables['primary_wrapper'][$field_name]['value']['#value'])) {
continue;
}

$variables['primary_wrapper'][$field_name]['value'] = [
'#type' => 'secret',
'#value' => $variables['primary_wrapper'][$field_name]['value']['#value'],
Expand Down

0 comments on commit dc4015c

Please sign in to comment.