-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathExAppMapper.php
More file actions
233 lines (220 loc) · 6.85 KB
/
ExAppMapper.php
File metadata and controls
233 lines (220 loc) · 6.85 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AppAPI\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* @template-extends QBMapper<ExApp>
*/
class ExAppMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'ex_apps');
}
/**
* @throws Exception
*
* @return ExApp[]
*/
public function findAll(?int $limit = null, ?int $offset = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select(
'a.*',
'd.protocol',
'd.host',
'd.deploy_config',
'd.accepts_deploy_id',
'r.url',
'r.verb',
'r.access_level',
'r.headers_to_exclude',
'r.bruteforce_protection',
)
->from($this->tableName, 'a')
->leftJoin('a', 'ex_apps_daemons', 'd', $qb->expr()->eq('a.daemon_config_name', 'd.name'))
->leftJoin('a', 'ex_apps_routes', 'r', $qb->expr()->eq('a.appid', 'r.appid'))
->orderBy('a.appid', 'ASC')
->setMaxResults($limit)
->setFirstResult($offset);
return $this->buildExAppWithRoutes($qb->executeQuery()->fetchAll());
}
/**
* @param string $appId
*
* @throws DoesNotExistException if not found
* @throws MultipleObjectsReturnedException if more than one result
* @throws Exception
*
* @return ExApp
*/
public function findByAppId(string $appId): Entity {
$qb = $this->db->getQueryBuilder();
$qb->select(
'a.*',
'd.protocol',
'd.host',
'd.deploy_config',
'd.accepts_deploy_id',
'r.url',
'r.verb',
'r.access_level',
'r.headers_to_exclude',
'r.bruteforce_protection',
)
->from($this->tableName, 'a')
->leftJoin('a', 'ex_apps_daemons', 'd', $qb->expr()->eq('a.daemon_config_name', 'd.name'))
->leftJoin('a', 'ex_apps_routes', 'r', $qb->expr()->eq('a.appid', 'r.appid'))
->orderBy('a.appid', 'ASC')
->where(
$qb->expr()->eq('a.appid', $qb->createNamedParameter($appId))
);
$apps = $this->buildExAppWithRoutes($qb->executeQuery()->fetchAll());
if (count($apps) === 0) {
throw new DoesNotExistException('No ExApp found with appId ' . $appId);
}
if (count($apps) > 1) {
throw new MultipleObjectsReturnedException('Multiple ExApps found with appId ' . $appId);
}
return $apps[0];
}
/**
* @param array $result fetched rows from the database
*
* @return array of ExApps with composed routes
*/
private function buildExAppWithRoutes(array $result): array {
$apps = [];
$lastAppId = null;
$lastApp = null;
foreach ($result as $row) {
if ($lastAppId !== $row['appid'] || $lastAppId === null) {
$lastAppId = $row['appid'];
$lastApp = new ExApp([
'id' => $row['id'],
'appid' => $row['appid'],
'version' => $row['version'],
'name' => $row['name'],
'daemon_config_name' => $row['daemon_config_name'],
'protocol' => $row['protocol'],
'host' => $row['host'],
'port' => $row['port'],
'secret' => $row['secret'],
'status' => $row['status'],
'enabled' => $row['enabled'],
'created_time' => $row['created_time'],
'deploy_config' => $row['deploy_config'],
'accepts_deploy_id' => $row['accepts_deploy_id'],
'routes' => [],
]);
$apps[] = $lastApp;
}
if (isset($row['url'])) {
$route = [
'url' => $row['url'],
'verb' => $row['verb'],
'access_level' => $row['access_level'],
'headers_to_exclude' => $row['headers_to_exclude'],
'bruteforce_protection' => $row['bruteforce_protection'],
];
$lastAppRoutes = $lastApp->getRoutes();
$lastAppRoutes[] = $route;
$lastApp->setRoutes($lastAppRoutes);
}
}
return $apps;
}
/**
* @return array
* @throws Exception
*/
public function getUsedPorts(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('port')->from($this->tableName);
$result = $qb->executeQuery();
$ports = [];
while ($row = $result->fetch()) {
$ports[] = $row['port'];
}
$result->closeCursor();
return $ports;
}
public function deleteExApp(string $appId): int {
$qb = $this->db->getQueryBuilder();
try {
return $qb->delete($this->tableName)
->where(
$qb->expr()->eq('appid', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR))
)->executeStatement();
} catch (Exception) {
return 0;
}
}
/**
* @throws Exception
*/
public function updateExApp(ExApp $exApp, array $fields): int {
$qb = $this->db->getQueryBuilder();
$qb = $qb->update($this->tableName);
foreach ($fields as $field) {
if ($field === 'version') {
$qb = $qb->set('version', $qb->createNamedParameter($exApp->getVersion()));
} elseif ($field === 'name') {
$qb = $qb->set('name', $qb->createNamedParameter($exApp->getName()));
} elseif ($field === 'port') {
$qb = $qb->set('port', $qb->createNamedParameter($exApp->getPort(), IQueryBuilder::PARAM_INT));
} elseif ($field === 'status') {
$qb = $qb->set('status', $qb->createNamedParameter($exApp->getStatus(), IQueryBuilder::PARAM_JSON));
} elseif ($field === 'enabled') {
$qb = $qb->set('enabled', $qb->createNamedParameter($exApp->getEnabled(), IQueryBuilder::PARAM_INT));
}
}
return $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($exApp->getAppid())))->executeStatement();
}
/**
* @throws Exception
*/
public function registerExAppRoutes(ExApp $exApp, array $routes): int {
$qb = $this->db->getQueryBuilder();
$count = 0;
foreach ($routes as $route) {
if (isset($route['bruteforce_protection']) && is_string($route['bruteforce_protection'])) {
$route['bruteforce_protection'] = json_decode($route['bruteforce_protection'], false);
}
if (!isset($route['headers_to_exclude'])) {
$route['headers_to_exclude'] = [];
}
$qb->insert('ex_apps_routes')
->values([
'appid' => $qb->createNamedParameter($exApp->getAppid()),
'url' => $qb->createNamedParameter($route['url']),
'verb' => $qb->createNamedParameter($route['verb']),
'access_level' => $qb->createNamedParameter($route['access_level']),
'headers_to_exclude' => $qb->createNamedParameter(is_array($route['headers_to_exclude']) ? json_encode($route['headers_to_exclude']) : '[]'),
'bruteforce_protection' => $qb->createNamedParameter(
isset($route['bruteforce_protection']) && is_array($route['bruteforce_protection'])
? json_encode($route['bruteforce_protection'])
: '[]'
),
]);
$count += $qb->executeStatement();
}
return $count;
}
/**
* @throws Exception
*/
public function removeExAppRoutes(ExApp $exApp): int {
$qb = $this->db->getQueryBuilder();
return $qb->delete('ex_apps_routes')
->where($qb->expr()->eq('appid', $qb->createNamedParameter($exApp->getAppid())))
->executeStatement();
}
}