apigee_m10n-8.x-1.7/apigee_m10n.install
apigee_m10n.install
<?php
/**
* @file
* Copyright 2018 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\Exception\ApiException;
use Drupal\apigee_m10n\Monetization;
use Drupal\apigee_m10n\MonetizationInterface;
use Drupal\user\RoleInterface;
use Drupal\user\Entity\Role;
/**
* @file
* Install, update and uninstall functions for Apigee Monetization.
*/
/**
* Implements hook_requirements().
*/
function apigee_m10n_requirements($phase) {
$requirements = [];
// If monetization is disabled after module has been installed, show error on status page.
if ($phase === 'runtime') {
try {
/** @var \Drupal\apigee_m10n\MonetizationInterface $monetization */
$monetization = Drupal::service('apigee_m10n.monetization');
if (!$monetization->isMonetizationEnabled()) {
$requirements['apigee_monetization_connection_error'] = [
'title' => t('Apigee Monetization'),
'description' => t(Monetization::MONETIZATION_DISABLED_ERROR_MESSAGE),
'severity' => REQUIREMENT_ERROR,
];
}
}
catch (ApiException $exception) {
Drupal::logger('apigee_m10n')->error($exception->getMessage());
}
}
if ($phase == 'install' || $phase == 'runtime') {
if (!extension_loaded('bcmath')) {
$requirements['apigee_monetization_bcmath'] = [
'title' => t('BC Math'),
'description' => t('Apigee Monetization requires the BC Math PHP extension in order to format prices.'),
'severity' => REQUIREMENT_ERROR,
];
}
}
// Should not be able to install if organization is not monetized.
if ($phase === 'install') {
$message = t("Apigee Monetization module functionality is not available for the organization used.");
$monetized = TRUE;
if (\Drupal::moduleHandler()->moduleExists('apigee_edge')) {
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());
// Check if org is ApigeeX.
if ($organization && ('CLOUD' === $organization->getRuntimeType() || 'HYBRID' === $organization->getRuntimeType())) {
if (!$organization->getAddonsConfig() || !$organization->getAddonsConfig()->getMonetizationConfig() || (FALSE === $organization->getAddonsConfig()->getMonetizationConfig()->getEnabled())) {
$monetized = FALSE;
}
}
elseif ($organization && !$organization->getPropertyValue('features.isMonetizationEnabled')) {
$monetized = FALSE;
}
}
catch (\Exception $exception) {
// If not able to connect to Apigee Edge.
$monetized = FALSE;
}
} else {
$monetized = FALSE;
}
if (FALSE === $monetized && \Drupal::moduleHandler()->moduleExists('apigee_edge') && \Drupal::moduleHandler()->moduleExists('requirement')) {
$requirements['apigee_monetization_not_supported'] = [
'title' => t('Apigee Monetization'),
'description' => $message,
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function apigee_m10n_install() {
// Populate the default permissions for the authenticated user.
if (Drupal::moduleHandler()->moduleExists('user')) {
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, MonetizationInterface::DEFAULT_AUTHENTICATED_PERMISSIONS);
}
}
/**
* Diable payment funding field from view display.
*/
function apigee_m10n_update_8201(&$sandbox) {
$display_repository = \Drupal::service('entity_display.repository');
// Get the default view display for xrateplan.
$view_display = $display_repository->getViewDisplay('xrate_plan', 'xrate_plan', 'default');
$view_display->removeComponent('paymentFundingModel')->save();
}
/**
* Revoke the permission added for ApigeeX.
*/
function apigee_m10n_update_8202(&$sandbox) {
$roles = Role::loadMultiple();
$updatePermissions = [
"view own purchased_product" => "view own purchased_plan",
"view any purchased_product" => "view any purchased_plan",
"update own purchased_product" => "update own purchased_plan",
"update any purchased_product" => "update any purchased_plan",
"view xrate_plan" => "view rate_plan",
"view xrate_plan as anyone" => "view rate_plan as anyone",
"purchase xrate_plan" => "purchase rate_plan",
"purchase xrate_plan as anyone" => "purchase rate_plan as anyone"
];
foreach ($roles as $rid) {
$role_permissions = $rid->getPermissions();
foreach ($updatePermissions as $new_permission => $old_permission) {
if (in_array($new_permission, $role_permissions)) {
$rid->revokePermission($new_permission)->save();
$rid->grantPermission($old_permission)->save();
}
}
}
// Clear all caches.
drupal_flush_all_caches();
}
/**
* Updating apiProduct field display in xrate-plan default display mode and enabling 'view
* xproduct' permission for authenticated user.
*/
function apigee_m10n_update_8203(&$sandbox) {
$display_repository = \Drupal::service('entity_display.repository');
// Get the default view display for xrateplan.
$view_display = $display_repository->getViewDisplay('xrate_plan', 'xrate_plan', 'default');
if ($component = $view_display->getComponent('apiProduct')) {
$settings = [
'link' => false,
];
$view_display->setComponent('apiProduct', ['settings' => $settings,] + $component)->save();
}
if (Drupal::moduleHandler()->moduleExists('user')) {
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view xproduct']);
}
// Clear all caches.
drupal_flush_all_caches();
}
