View source
<?php
namespace Drupal\apigee_edge_teams\Entity\Form;
use Apigee\Edge\Exception\ApiException;
use Apigee\Edge\Exception\ClientErrorException;
use Drupal\apigee_edge\Entity\ApiProductInterface;
use Drupal\apigee_edge_teams\TeamMemberApiProductAccessHandlerInterface;
use Drupal\apigee_edge_teams\TeamMembershipManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\Error;
trait TeamAppFormTrait {
public static function appExists(string $name, array $element, FormStateInterface $form_state) : bool {
if ($name === '') {
return FALSE;
}
$factory = \Drupal::service('apigee_edge_teams.controller.team_app_controller_factory');
$app = TRUE;
try {
$app = $factory
->teamAppController($form_state
->getValue('owner'))
->load($name);
} catch (ApiException $exception) {
if ($exception instanceof ClientErrorException && $exception
->getEdgeErrorCode() === 'developer.service.AppDoesNotExist') {
$app = FALSE;
}
else {
$context = [
'%app_name' => $name,
'%owner' => $form_state
->getValue('owner'),
];
$context += Error::decodeException($exception);
\Drupal::logger('apigee_edge_teams')
->error("Unable to properly validate an app name's uniqueness. App name: %app_name. Owner: %owner. @message %function (line %line of %file). <pre>@backtrace_string</pre>", $context);
}
}
return (bool) $app;
}
protected function appEntityDefinition() : EntityTypeInterface {
return $this
->getEntityTypeManager()
->getDefinition('team_app');
}
protected function appOwnerEntityDefinition() : EntityTypeInterface {
return $this
->getEntityTypeManager()
->getDefinition('team');
}
protected function appCredentialLifeTime() : int {
$config_name = 'apigee_edge_teams.team_app_settings';
$config = method_exists($this, 'config') ? $this
->config($config_name) : \Drupal::config($config_name);
return $config
->get('credential_lifetime');
}
private function getEntityTypeManager() : EntityTypeManagerInterface {
if (property_exists($this, 'entityTypeManager') && $this->entityTypeManager instanceof EntityTypeManagerInterface) {
return $this->entityTypeManager;
}
return \Drupal::entityTypeManager();
}
private function getTeamMemberApiProductAccessHandler() : TeamMemberApiProductAccessHandlerInterface {
if (property_exists($this, 'teamMemberApiProductAccessHandler') && $this->teamMemberApiProductAccessHandler instanceof TeamMemberApiProductAccessHandlerInterface) {
return $this->teamMemberApiProductAccessHandler;
}
return \Drupal::service('apigee_edge_teams.team_member_api_product_access_handler');
}
private function getTeamMembershipMananger() : TeamMembershipManagerInterface {
if (property_exists($this, 'teamMembershipManager') && $this->teamMembershipManager instanceof TeamMembershipManagerInterface) {
return $this->teamMembershipManager;
}
return \Drupal::service('apigee_edge_teams.team_membership_manager');
}
private function getConfigObject(string $config) : ImmutableConfig {
$config_factory = \Drupal::service('config.factory');
if (method_exists($this, 'configFactory') && $this
->configFactory() instanceof ConfigFactoryInterface) {
$config_factory = $this
->configFactory();
}
elseif (property_exists($this, 'configFactory') && $this->configFactory instanceof ConfigFactoryInterface) {
$config_factory = $this->configFactory;
}
return $config_factory
->get($config);
}
protected function nonMemberApiProductAccessWarningElement(array $form, FormStateInterface $form_state) : array {
$element = [
'#theme' => 'status_messages',
'#message_list' => [],
'#weight' => -100,
];
if (!in_array($this
->getTeamName($form, $form_state), $this
->getTeamMembershipMananger()
->getTeams(\Drupal::currentUser()
->getEmail()))) {
$element['#message_list']['warning'][] = t('You are not member of this @team. You may see @api_products here that a @team member can not see.', [
'@team' => mb_strtolower($this
->getEntityTypeManager()
->getDefinition('team')
->getSingularLabel()),
'@api_products' => $this
->getEntityTypeManager()
->getDefinition('api_product')
->getPluralLabel(),
]);
}
return [
'non_member_api_product_access_warning' => $element,
];
}
protected function apiProductList(array $form, FormStateInterface $form_state) : array {
$team_name = $this
->getTeamName($form, $form_state);
$team = $this
->getEntityTypeManager()
->getStorage('team')
->load($team_name);
if ($team === NULL) {
return [];
}
if (!in_array($team_name, $this
->getTeamMembershipMananger()
->getTeams(\Drupal::currentUser()
->getEmail()))) {
$filter = function (ApiProductInterface $api_product) use ($team) {
$visibility = $api_product
->getAttributeValue('access') ?? 'public';
return in_array($visibility, $this
->getConfigObject('apigee_edge_teams.team_settings')
->get('non_member_team_apps_visible_api_products'));
};
}
else {
$filter = function (ApiProductInterface $api_product) use ($team) {
return $this
->getTeamMemberApiProductAccessHandler()
->access($api_product, 'assign', $team);
};
}
return array_filter($this
->getEntityTypeManager()
->getStorage('api_product')
->loadMultiple(), $filter);
}
protected function getTeamName(array &$form, FormStateInterface $form_state) : string {
$team_name = $form_state
->getValue('owner') ?? $form['owner']['#value'] ?? $form['owner']['#default_value'];
return $team_name;
}
}