forked from apigee/apigee-m10n-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apigee_m10n.install
176 lines (157 loc) · 6.12 KB
/
apigee_m10n.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?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;
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;
}
if (FALSE === $monetized) {
$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();
}