You are here

apigee_edge_teams.install in Apigee Edge 8

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.

File

modules/apigee_edge_teams/apigee_edge_teams.install
View source
<?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\user\RoleInterface;
use Drupal\views\Views;

/**
 * Install, update and uninstall functions for Apigee Edge 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 && !OrganizationFeatures::isCompaniesFeatureAvailable($organization)) {
        $url = [
          ':url' => 'https://cloud.google.com/apigee/docs/api-platform/get-started/compare-apigee-products#unsupported-apis',
        ];
        $message = $phase == 'runtime' ? t("The Apigee Edge Teams module functionality is not available for your org and should be uninstalled, because <a href=':url' target='_blank'>Edge company APIs are not supported in Apigee hybrid orgs</a>.", $url) : t("The Apigee Edge Teams module functionality is not available for your org because <a href=':url' target='_blank'>Edge company APIs are not supported in Apigee hybrid orgs</a>.", $url);
        $requirements['apigee_edge_teams_not_supported'] = [
          'title' => t('Apigee Edge 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_get_path('module', '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();
  }
}

Functions

Namesort descending Description
apigee_edge_teams_install Implements hook_install().
apigee_edge_teams_requirements Implements hook_requirements().
apigee_edge_teams_update_8701 Assign "add_api_key", "revoke_api_key" and "edit_api_products" permissions to team administrators.
apigee_edge_teams_update_8702 Install team_invitation entity, dependencies and config.
apigee_edge_teams_update_8703 Assign accept and decline team invitations permissions to authenticated users.
apigee_edge_teams_update_8704 Remove the "Manage team members and invitations" access for the Team invitations view.
apigee_edge_teams_update_8705 Set disable_sql_rewrite to false for Team invitations view.