-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.php
171 lines (146 loc) · 6.67 KB
/
Service.php
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
<?php
/**
* Copyright 2022-2023 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
namespace Box\Mod\Serversync;
class Service implements \FOSSBilling\InjectionAwareInterface
{
protected ?\Pimple\Container $di;
public function setDi(\Pimple\Container $di): void
{
$this->di = $di;
}
public function getDi(): ?\Pimple\Container
{
return $this->di;
}
/**
* Get the list of hosting servers and information about their server managers
* @return array An array of hosting servers
*/
public function getHostingServers(): array
{
$service = $this->di['mod_service']('servicehosting');
$serverPairs = $service->getServerPairs();
$servers = [];
foreach ($serverPairs as $id => $name) {
$hostingServerModel = $this->di['db']->getExistingModelById('ServiceHostingServer', $id, 'Server not found');
$manager = $this->getHostingServerManager($hostingServerModel);
$servers[] = [
'id' => $id,
'name' => $name,
'manager' => [
'label' => $manager->getForm()['label'],
'supports_sync' => $this->hostingServerManagerSupportsSync($manager),
],
];
}
return $servers;
}
/**
* Get the hosting accounts and matching FOSSBilling accounts (if any) for a given hosting server
*
* @param int $serverId The ID of the hosting server
* @return array An array of hosting accounts. The array contains the following keys:
* - server: The account on the hosting server
* - fossbilling: The FOSSBilling account that matches the hosting account, if any. Matching is done by usernames.
* - suggested_actions: An array of suggested actions to take for the account, if any. An empty array means no actions are suggested.
* @throws \Box_Exception
*/
public function getHostingServerAccounts(int $serverId): array
{
$hostingServerModel = $this->di['db']->getExistingModelById('ServiceHostingServer', $serverId, 'Server not found');
$serverManager = $this->getHostingServerManager($hostingServerModel);
if (!$this->hostingServerManagerSupportsSync($serverManager)) {
throw new \Box_Exception('This server manager does not support synchronizing accounts');
}
$remoteServerAccounts = $serverManager->listAccounts();
$hostingAccounts = [];
foreach ($remoteServerAccounts as $remoteServerAccount) {
$fossbillingHostingAccountModel = $this->di['db']->findOne('ServiceHosting', 'service_hosting_server_id = ? AND username = ?', [$serverId, $remoteServerAccount['username']]);
$hostingService = $this->di['mod_service']('servicehosting');
// Kind of messy, but it seems like the only way for now
$fossbillingHostingAccount = $fossbillingHostingAccountModel ? $hostingService->toApiArray($fossbillingHostingAccountModel) : null;
$fossbillingOrderModel = $this->di['db']->findOne('ClientOrder', 'service_type = :service_type AND service_id = :service_id', ['service_type' => 'hosting', 'service_id' => $fossbillingHostingAccountModel->id]);
if ($fossbillingHostingAccount) {
$fossbillingHostingAccount['order'] = $fossbillingOrderModel ? $this->di['mod_service']('order')->toApiArray($fossbillingOrderModel) : null;
$fossbillingHostingAccount['client'] = $fossbillingHostingAccountModel->client;
}
$hostingAccounts[] = [
'server' => $remoteServerAccount,
'fossbilling' => $fossbillingHostingAccount,
'suggested_actions' => $this->suggestActions([
'server' => $remoteServerAccount,
'fossbilling' => $fossbillingHostingAccount,
]),
];
}
// Order the accounts by username on the hosting server
usort($hostingAccounts, function ($a, $b) {
return strcmp($a['server']['username'], $b['server']['username']);
});
return $hostingAccounts;
}
/**
* Suggests actions to take for a given hosting account
*/
private function suggestActions(array $hostingAccounts): array
{
$fossbillingHostingAccount = $hostingAccounts['fossbilling'];
$serverAccount = $hostingAccounts['server'];
$suggested = [];
if ($fossbillingHostingAccount === null) {
$client = $this->di['db']->findOne('Client', 'email = ?', [$serverAccount['email']]);
if ($client) {
$suggested[] = [
'id' => 'create',
'label' => 'Link to: ' . $serverAccount['email'],
];
} else {
$suggested[] = [
'id' => 'create',
'label' => 'Create a client and link to it',
];
}
return $suggested; // No need to check for anything else
}
if ($fossbillingHostingAccount['order']['status'] === \Model_ClientOrder::STATUS_SUSPENDED && $serverAccount['status'] === \Model_ClientOrder::STATUS_ACTIVE) {
$suggested[] = [
'id' => 'unsuspend',
'label' => 'Mark the account unsuspended',
];
}
if ($fossbillingHostingAccount['order']['status'] === \Model_ClientOrder::STATUS_ACTIVE && $serverAccount['status'] === \Model_ClientOrder::STATUS_SUSPENDED) {
$suggested[] = [
'id' => 'suspend',
'label' => 'Mark the account suspended',
];
}
return $suggested;
}
/**
* Get the server manager for a given hosting server
* @param \Model_ServiceHostingServer $serverModel The hosting server model
* @return object The server manager for the hosting server
*/
private function getHostingServerManager($serverModel): object
{
$service = $this->di['mod_service']('servicehosting');
$serverManager = $service->getServerManager($serverModel);
return $serverManager;
}
/**
* Check if a given server manager supports synchronizing account data
* @param object $serverManager The server manager object
* @return bool True if the server manager supports synchronizing account data, false otherwise
*/
public function hostingServerManagerSupportsSync($serverManager): bool
{
return method_exists($serverManager, 'listAccounts');
}
}