apigee_edge-8.x-1.17/modules/apigee_edge_teams/apigee_edge_teams.install

modules/apigee_edge_teams/apigee_edge_teams.install
<?php

/**
 * @file
 * Copyright 2019 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

use Apigee\Edge\Utility\OrganizationFeatures;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\RoleInterface;
use Drupal\views\Views;

/**
 * Install, update and uninstall functions for Apigee Edge and ApigeeX Teams.
 */

/**
 * Implements hook_requirements().
 */
function apigee_edge_teams_requirements($phase) {
  $requirements = [];

  if ($phase == 'install' || $phase == 'runtime') {
    try {
      /** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
      $sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
      $org_controller = \Drupal::service('apigee_edge.controller.organization');
      /* @var \Apigee\Edge\Api\Management\Entity\Organization $organization */
      $organization = $org_controller->load($sdk_connector->getOrganization());
      if ($organization && $org_controller->isOrganizationApigeeX()) {
        // AppGroup APIs are not supported in Apigee X / Hybrid orgs with monetization enabled.
        if ($organization->getAddonsConfig() && $organization->getAddonsConfig()->getMonetizationConfig() && TRUE === $organization->getAddonsConfig()->getMonetizationConfig()->getEnabled()) {
          $url = [
            ':url' => 'https://cloud.google.com/apigee/docs/api-platform/publish/organizing-client-app-ownership?hl=en#appgroups-limitations-and-known-issues',
          ];
          $message = ($phase == 'runtime') ?
            t("The Teams module functionality is not available for monetization enabled org on Apigee X / Hybrid and should be uninstalled, because <a href=':url' target='_blank'>AppGroup APIs are not supported in Apigee X / Hybrid orgs with monetization enabled</a>.", $url) :
            t("The Teams module functionality is not available for monetization enabled org on Apigee X / Hybrid because <a href=':url' target='_blank'>AppGroup APIs are not supported in Apigee X / Hybrid orgs with monetization enabled</a>.", $url);
          $requirements['apigee_edge_teams_not_supported'] = [
            'title' => t('Apigee Teams'),
            'description' => $message,
            'severity' => REQUIREMENT_ERROR,
          ];
        }
      }
    }
    catch (\Exception $exception) {
      // Do nothing if connection to Edge is not available.
    }
  }

  return $requirements;
}

/**
 * Implements hook_install().
 */
function apigee_edge_teams_install() {
  if (\Drupal::moduleHandler()->moduleExists('user')) {
    $authenticated_user_permissions = [
      'accept own team invitation',
      'decline own team invitation',
    ];
    user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, $authenticated_user_permissions);
  }
}


/**
 * Assign "add_api_key", "revoke_api_key" and "edit_api_products" permissions to team administrators.
 */
function apigee_edge_teams_update_8701() {
  $role = 'admin';
  $api_key_permissions = [
    'team_app_add_api_key',
    'team_app_edit_api_products',
    'team_app_revoke_api_key',
  ];
  /** @var \Drupal\apigee_edge_teams\Entity\Storage\TeamRoleStorageInterface $storage */
  $storage = Drupal::entityTypeManager()->getStorage('team_role');
  /** @var \Drupal\apigee_edge_teams\Entity\TeamRoleInterface $admin_role */
  $admin_role = $storage->load($role);
  $storage->changePermissions($role, array_combine($admin_role->getPermissions(), $admin_role->getPermissions()) + array_combine($api_key_permissions, $api_key_permissions));
}

/**
 * Install team_invitation entity, dependencies and config.
 */
function apigee_edge_teams_update_8702() {
  // Install 'team_invitation' entity.
  \Drupal::entityTypeManager()->clearCachedDefinitions();
  $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_definition_update_manager->installEntityType(\Drupal::entityTypeManager()->getDefinition('team_invitation'));

  // Install 'views' module.
  \Drupal::service('module_installer')->install(['views']);

  // Install new config.
  $settings_to_sync = [
    'team_invitation_expiry_days',
    'team_invitation_email_existing',
    'team_invitation_email_new',
  ];
  /** @var \Drupal\Core\Config\StorageInterface $config_storage */
  $config_storage = \Drupal::service('config.storage');
  $module_path = \Drupal::service('extension.list.module')->getPath('apigee_edge_teams');
  $source = new FileStorage("$module_path/config/install");
  $new_team_settings = $source->read('apigee_edge_teams.team_settings');
  $team_settings = $config_storage->read('apigee_edge_teams.team_settings');
  foreach ($settings_to_sync as $setting) {
    $team_settings[$setting] = $new_team_settings[$setting];
  }
  $config_storage->write('apigee_edge_teams.team_settings', $team_settings);

  $optional = new FileStorage("$module_path/config/optional");
  $team_invitations_view = $optional->read('views.view.team_invitations');
  $config_storage->write('views.view.team_invitations', $team_invitations_view);
}

/**
 * Assign accept and decline team invitations permissions to authenticated users.
 */
function apigee_edge_teams_update_8703() {
  if (\Drupal::moduleHandler()->moduleExists('user')) {
    $authenticated_user_permissions = [
      'accept own team invitation',
      'decline own team invitation',
    ];
    user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, $authenticated_user_permissions);
  }
}

/**
 * Remove the "Manage team members and invitations" access for the Team invitations view.
 */
function apigee_edge_teams_update_8704() {
  if ($view = Views::getView('team_invitations')) {
    $view->setDisplay('team');
    $access = $view->getDisplay()->getOption('access');
    if (empty($access['type']) || $access['type'] !== "team_permission") {
      return;
    }

    $view->getDisplay()->setOption('access', [
      'type' => 'none',
      'options' => [],
    ]);
    $view->save();
  }
}

/**
 * Set disable_sql_rewrite to false for Team invitations view.
 */
function apigee_edge_teams_update_8705() {
  /** @var \Drupal\views\ViewExecutable $view */
  if ($view = Views::getView('team_invitations')) {
    $view->setDisplay('user');
    $query = $view->getDisplay()->getOption('query');
    if (empty($query['options']['disable_sql_rewrite']) || $query['options']['disable_sql_rewrite'] === FALSE) {
      return;
    }

    $query['options']['disable_sql_rewrite'] = FALSE;
    $view->getDisplay()->setOption('query', $query);
    $view->save();
  }
}

/**
 * Update the field storage defination of Team App.
 */
function apigee_edge_teams_update_9001() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
  $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
  $entity_type_id = 'team_app';
  $entity_type = $definition_update_manager->getEntityType($entity_type_id);
  $field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);

  $field_storage_definitions['apiProducts'] = BaseFieldDefinition::create('string')
    ->setName('apiproducts')
    ->setTargetEntityTypeId($entity_type_id)
    ->setTargetBundle(NULL)
    ->setStorageRequired(FALSE)
    ->setInternal(TRUE)
    ->setTranslatable(FALSE)
    ->setRevisionable(FALSE);

  $definition_update_manager->updateFieldableEntityType($entity_type, $field_storage_definitions);
}

/**
 * Install Configs for Core Entity View Display for Default Team App
 */
function apigee_edge_teams_update_9002() {
  // Update existing config.
  /** @var \Drupal\Core\Config\StorageInterface $config_storage */
  $config_storage = \Drupal::service('config.storage');
  $module_path = \Drupal::service('extension.list.module')->getPath('apigee_edge_teams');
  $source = new FileStorage("$module_path/config/install");
  $new_team_settings = $source->read('core.entity_view_display.team_app.team_app.default');
  $team_settings = $config_storage->read('core.entity_view_display.team_app.team_app.default');
  $team_settings['content']['callbackUrl'] = $new_team_settings['content']['callbackUrl'];
  $config_storage->write('core.entity_view_display.team_app.team_app.default', $team_settings);
}

/**
 * Install Configs for TeamAlias Form for Teams Setting Page
 */
function apigee_edge_teams_update_9003() {
  // Update existing config.
  /** @var \Drupal\Core\Config\StorageInterface $config_storage */
  $config_storage = \Drupal::service('config.storage');
  $module_path = \Drupal::service('extension.list.module')->getPath('apigee_edge_teams');
  $source = new FileStorage("$module_path/config/install");
  $new_team_settings = $source->read('apigee_edge_teams.team_settings');
  $team_settings = $config_storage->read('apigee_edge_teams.team_settings');
  $team_settings['enablefilter'] = $new_team_settings['enablefilter'];
  $config_storage->write('apigee_edge_teams.team_settings', $team_settings);
}

/**
 * Set cache_insert_chunk_size for Teams and Team apps.
 */
function apigee_edge_teams_update_11401(): void {
  $configs = [
    'apigee_edge_teams.team_settings',
    'apigee_edge_teams.team_app_settings',
  ];
  foreach ($configs as $config) {
    \Drupal::configFactory()->getEditable($config)->set('cache_insert_chunk_size', 100)->save(TRUE);
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc