-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFileMapper.php
More file actions
277 lines (246 loc) · 7.4 KB
/
FileMapper.php
File metadata and controls
277 lines (246 loc) · 7.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Db;
use OCA\Libresign\Enum\FileStatus;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\Comments\ICommentsManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IL10N;
/**
* Class FileMapper
*
* @package OCA\Libresign\DB
* @template-extends QBMapper<File>
*/
class FileMapper extends QBMapper {
/** @var File[] */
private $file = [];
public function __construct(
IDBConnection $db,
private IL10N $l,
) {
parent::__construct($db, 'libresign_file');
}
/**
* Return LibreSign file by ID
*
* @throws DoesNotExistException
* @return File Row of table libresign_file
*/
public function getById(int $id): File {
foreach ($this->file as $file) {
if ($file->getId() === $id) {
return $file;
}
}
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
/** @var File */
$file = $this->findEntity($qb);
$this->file[] = $file;
return $file;
}
/**
* Return LibreSign file by signed hash
*
* @throws DoesNotExistException
* @return File Row of table libresign_file
*/
public function getBySignedHash(string $hash): File {
foreach ($this->file as $file) {
if ($file->getSignedHash() === $hash) {
return $file;
}
}
$qb = $this->db->getQueryBuilder();
$qb->select('f.*')
->from($this->getTableName(), 'f')
->join('f', 'libresign_sign_request', 'sr', $qb->expr()->eq('f.id', 'sr.file_id'))
->where(
$qb->expr()->orX(
$qb->expr()->eq('f.signed_hash', $qb->createNamedParameter($hash)),
$qb->expr()->eq('sr.signed_hash', $qb->createNamedParameter($hash))
)
)
->setMaxResults(1);
/** @var File */
$file = $this->findEntity($qb);
$this->file[] = $file;
return $file;
}
/**
* Return LibreSign file by file UUID
*/
public function getByUuid(?string $uuid = null): File {
if (is_null($uuid) && !empty($this->file)) {
return current($this->file);
}
foreach ($this->file as $file) {
if ($file->getUuid() === $uuid) {
return $file;
}
}
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('uuid', $qb->createNamedParameter($uuid))
);
/** @var File */
$file = $this->findEntity($qb);
$this->file[] = $file;
return $file;
}
/**
* Return LibreSign file by signer UUID
*/
public function getBySignerUuid(?string $uuid = null): File {
if (is_null($uuid) && !empty($this->file)) {
return current($this->file);
}
$qb = $this->db->getQueryBuilder();
$qb->select('f.*')
->from($this->getTableName(), 'f')
->join('f', 'libresign_sign_request', 'sr', $qb->expr()->eq('f.id', 'sr.file_id'))
->where(
$qb->expr()->eq('sr.uuid', $qb->createNamedParameter($uuid))
);
/** @var File */
$file = $this->findEntity($qb);
$this->file[] = $file;
return $file;
}
/**
* Return LibreSign file by nodeId
*/
public function getByFileId(?int $nodeId = null): File {
$exists = array_filter($this->file, fn ($f) => $f->getNodeId() === $nodeId || $f->getSignedNodeId() === $nodeId);
if (!empty($exists)) {
return current($exists);
}
foreach ($this->file as $file) {
if ($file->getNodeId() === $nodeId) {
return $file;
}
}
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->orX(
$qb->expr()->eq('node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('signed_node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT))
)
);
/** @var File */
$file = $this->findEntity($qb);
$this->file[] = $file;
return $file;
}
public function fileIdExists(int $nodeId): bool {
$exists = array_filter($this->file, fn ($f) => $f->getNodeId() === $nodeId || $f->getSignedNodeId() === $nodeId);
if (!empty($exists)) {
return true;
}
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->orX(
$qb->expr()->eq('node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('signed_node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT))
)
);
$files = $this->findEntities($qb);
if (!empty($files)) {
foreach ($files as $file) {
$this->file[] = $file;
}
return true;
}
return false;
}
/**
* @return File[]
*/
public function getFilesOfAccount(string $userId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('lf.*')
->from($this->getTableName(), 'lf')
->join('lf', 'libresign_id_docs', 'lid', 'lid.file_id = lf.id')
->where(
$qb->expr()->eq('lid.user_id', $qb->createNamedParameter($userId))
);
$cursor = $qb->executeQuery();
$return = [];
while ($row = $cursor->fetch()) {
/** @var File */
$file = $this->mapRowToEntity($row);
$this->file[] = $file;
$return[] = $file;
}
return $return;
}
public function getFileType(int $id): string {
$fullOuterJoin = $this->db->getQueryBuilder();
$fullOuterJoin->select($fullOuterJoin->expr()->literal(1));
$qb = $this->db->getQueryBuilder();
$qb
->selectAlias('f.id', 'file')
->selectAlias('sf.signed_node_id', 'signed_file')
->selectAlias('ue.id', 'user_element')
->selectAlias('fe.id', 'file_element')
->from($qb->createFunction('(' . $fullOuterJoin->getSQL() . ')'), 'foj')
->leftJoin('foj', 'libresign_file', 'f', $qb->expr()->eq('f.node_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
->leftJoin('foj', 'libresign_file', 'sf', $qb->expr()->eq('sf.signed_node_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
->leftJoin('foj', 'libresign_user_element', 'ue', $qb->expr()->eq('ue.file_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
->leftJoin('foj', 'libresign_file_element', 'fe', $qb->expr()->eq('fe.file_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$cursor = $qb->executeQuery();
$row = $cursor->fetch();
if ($row) {
foreach ($row as $key => $value) {
if ($value) {
return $key;
}
}
}
return 'not_libresign_file';
}
public function getTextOfStatus(int|FileStatus $status): string {
if (is_int($status)) {
$status = FileStatus::from($status);
}
return $status->getLabel($this->l);
}
public function neutralizeDeletedUser(string $userId, string $displayName): void {
$update = $this->db->getQueryBuilder();
$qb = $this->db->getQueryBuilder();
$qb->select('f.id')
->addSelect('f.metadata')
->from($this->getTableName(), 'f')
->where($qb->expr()->eq('f.user_id', $qb->createNamedParameter($userId)));
$cursor = $qb->executeQuery();
while ($row = $cursor->fetch()) {
$row['metadata'] = json_decode((string)$row['metadata'], true);
$row['metadata']['deleted_account'] = [
'account' => $userId,
'display_name' => $displayName,
];
$update->update($this->getTableName())
->set('user_id', $update->createNamedParameter(ICommentsManager::DELETED_USER))
->set('metadata', $update->createNamedParameter($row['metadata'], IQueryBuilder::PARAM_JSON))
->where($update->expr()->eq('id', $update->createNamedParameter($row['id'])));
$update->executeStatement();
}
}
}