-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathFolderManager.php
985 lines (855 loc) Β· 29 KB
/
FolderManager.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
<?php
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\GroupFolders\Folder;
use OC\Files\Cache\Cache;
use OC\Files\Node\Node;
use OCA\Circles\CirclesManager;
use OCA\Circles\Exceptions\CircleNotFoundException;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\GroupFolders\Mount\GroupMountPoint;
use OCA\GroupFolders\ResponseDefinitions;
use OCA\GroupFolders\Settings\Admin;
use OCP\AutoloadNotAllowedException;
use OCP\Constants;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileInfo;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use OCP\Server;
use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerInterface;
/**
* @psalm-import-type GroupFoldersGroup from ResponseDefinitions
* @psalm-import-type GroupFoldersUser from ResponseDefinitions
* @psalm-import-type GroupFoldersAclManage from ResponseDefinitions
* @psalm-import-type GroupFoldersApplicable from ResponseDefinitions
* @psalm-type InternalFolder = array{
* folder_id: int,
* mount_point: string,
* permissions: int,
* quota: int,
* acl: bool,
* rootCacheEntry: ?ICacheEntry,
* }
* @psalm-type InternalFolderOut = array{
* id: int,
* mount_point: string,
* groups: array<string, GroupFoldersApplicable>,
* quota: int,
* size: int,
* acl: bool,
* manage: list<GroupFoldersAclManage>,
* }
* @psalm-type InternalFolderMapping = array{
* folder_id: int,
* mapping_type: 'user'|'group',
* mapping_id: string,
* }
*/
class FolderManager {
public const ENTITY_GROUP = 1;
public const ENTITY_CIRCLE = 2;
public const SPACE_DEFAULT = -4;
public function __construct(
private IDBConnection $connection,
private IGroupManager $groupManager,
private IMimeTypeLoader $mimeTypeLoader,
private LoggerInterface $logger,
private IEventDispatcher $eventDispatcher,
private IConfig $config,
) {
}
/**
* @return array<int, InternalFolderOut>
* @throws Exception
*/
public function getAllFolders(): array {
$applicableMap = $this->getAllApplicable();
$query = $this->connection->getQueryBuilder();
$query->select('folder_id', 'mount_point', 'quota', 'acl')
->from('group_folders', 'f');
$rows = $query->executeQuery()->fetchAll();
$folderMap = [];
foreach ($rows as $row) {
$id = (int)$row['folder_id'];
$folderMap[$id] = [
'id' => $id,
'mount_point' => $row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => 0,
'acl' => (bool)$row['acl']
];
}
return $folderMap;
}
/**
* @throws Exception
*/
private function getGroupFolderRootId(int $rootStorageId): int {
$query = $this->connection->getQueryBuilder();
$query->select('fileid')
->from('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($rootStorageId)))
->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5('__groupfolders'))));
return (int)$query->executeQuery()->fetchOne();
}
private function joinQueryWithFileCache(IQueryBuilder $query, int $rootStorageId): void {
$query->leftJoin('f', 'filecache', 'c', $query->expr()->andX(
// concat with empty string to work around missing cast to string
$query->expr()->eq('c.name', $query->func()->concat('f.folder_id', $query->expr()->literal(''))),
$query->expr()->eq('c.parent', $query->createNamedParameter($this->getGroupFolderRootId($rootStorageId))),
$query->expr()->eq('c.storage', $query->createNamedParameter($rootStorageId)),
));
}
/**
* @return array<int, InternalFolderOut>
* @throws Exception
*/
public function getAllFoldersWithSize(int $rootStorageId): array {
$applicableMap = $this->getAllApplicable();
$query = $this->connection->getQueryBuilder();
$query->select('folder_id', 'mount_point', 'quota', 'c.size', 'acl')
->from('group_folders', 'f');
$this->joinQueryWithFileCache($query, $rootStorageId);
$rows = $query->executeQuery()->fetchAll();
$folderMappings = $this->getAllFolderMappings();
$folderMap = [];
foreach ($rows as $row) {
$id = (int)$row['folder_id'];
$mappings = $folderMappings[$id] ?? [];
$folderMap[$id] = [
'id' => $id,
'mount_point' => $row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => $row['size'] ? (int)$row['size'] : 0,
'acl' => (bool)$row['acl'],
'manage' => $this->getManageAcl($mappings)
];
}
return $folderMap;
}
/**
* @return array<int, InternalFolderOut>
* @throws Exception
*/
public function getAllFoldersForUserWithSize(int $rootStorageId, IUser $user): array {
$groups = $this->groupManager->getUserGroupIds($user);
$applicableMap = $this->getAllApplicable();
$query = $this->connection->getQueryBuilder();
$query->select('f.folder_id', 'mount_point', 'quota', 'c.size', 'acl')
->from('group_folders', 'f')
->innerJoin(
'f',
'group_folders_groups',
'a',
$query->expr()->eq('f.folder_id', 'a.folder_id')
)
->where($query->expr()->in('a.group_id', $query->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)));
$this->joinQueryWithFileCache($query, $rootStorageId);
$rows = $query->executeQuery()->fetchAll();
$folderMappings = $this->getAllFolderMappings();
$folderMap = [];
foreach ($rows as $row) {
$id = (int)$row['folder_id'];
$mappings = $folderMappings[$id] ?? [];
$folderMap[$id] = [
'id' => $id,
'mount_point' => $row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => $row['size'] ? (int)$row['size'] : 0,
'acl' => (bool)$row['acl'],
'manage' => $this->getManageAcl($mappings)
];
}
return $folderMap;
}
/**
* @return array<int, list<InternalFolderMapping>>
* @throws Exception
*/
private function getAllFolderMappings(): array {
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('group_folders_manage', 'g');
$rows = $query->executeQuery()->fetchAll();
$folderMap = [];
foreach ($rows as $row) {
$id = (int)$row['folder_id'];
if (!isset($folderMap[$id])) {
$folderMap[$id] = [$row];
} else {
$folderMap[$id][] = $row;
}
}
return $folderMap;
}
/**
* @return array<int, InternalFolderMapping>
* @throws Exception
*/
private function getFolderMappings(int $id): array {
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('group_folders_manage')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $query->executeQuery()->fetchAll();
}
/**
* @param InternalFolderMapping[] $mappings
* @return list<GroupFoldersAclManage>
*/
private function getManageAcl(array $mappings): array {
return array_values(array_filter(array_map(function (array $entry): ?array {
if ($entry['mapping_type'] === 'user') {
$user = \OC::$server->get(IUserManager::class)->get($entry['mapping_id']);
if ($user === null) {
return null;
}
return [
'type' => 'user',
'id' => (string)$user->getUID(),
'displayname' => (string)$user->getDisplayName()
];
}
$group = \OC::$server->get(IGroupManager::class)->get($entry['mapping_id']);
if ($group === null) {
return null;
}
return [
'type' => 'group',
'id' => (string)$group->getGID(),
'displayname' => (string)$group->getDisplayName()
];
}, $mappings)));
}
/**
* @return InternalFolderOut|false
* @throws Exception
*/
public function getFolder(int $id, int $rootStorageId = 0) {
$applicableMap = $this->getAllApplicable();
$query = $this->connection->getQueryBuilder();
$query->select('folder_id', 'mount_point', 'quota', 'c.size', 'acl')
->from('group_folders', 'f')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$this->joinQueryWithFileCache($query, $rootStorageId);
$result = $query->executeQuery();
$row = $result->fetch();
$result->closeCursor();
$folderMappings = $this->getFolderMappings($id);
return $row ? [
'id' => $id,
'mount_point' => (string)$row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => $row['size'] ? $row['size'] : 0,
'acl' => (bool)$row['acl'],
'manage' => $this->getManageAcl($folderMappings)
] : false;
}
/**
* Return just the ACL for the folder.
*
* @return bool
* @throws Exception
*/
public function getFolderAclEnabled(int $id): bool {
$query = $this->connection->getQueryBuilder();
$query->select('acl')
->from('group_folders', 'f')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$result = $query->executeQuery();
$row = $result->fetch();
$result->closeCursor();
return (bool)($row['acl'] ?? false);
}
public function getFolderByPath(string $path): int {
/** @var Node $node */
$node = \OC::$server->get(IRootFolder::class)->get($path);
/** @var GroupMountPoint $mountPoint */
$mountPoint = $node->getMountPoint();
return $mountPoint->getFolderId();
}
/**
* @return array<int, array<string, GroupFoldersApplicable>>
* @throws Exception
*/
private function getAllApplicable(): array {
$queryHelper = null;
if ($this->isCirclesAvailable($circlesManager)) {
$queryHelper = $circlesManager?->getQueryHelper();
}
$query = $queryHelper?->getQueryBuilder() ?? $this->connection->getQueryBuilder();
$query->select('g.folder_id', 'g.group_id', 'g.circle_id', 'g.permissions')
->from('group_folders_groups', 'g');
$queryHelper?->addCircleDetails('g', 'circle_id');
$rows = $query->executeQuery()->fetchAll();
$applicableMap = [];
foreach ($rows as $row) {
$id = (int)$row['folder_id'];
if (!array_key_exists($id, $applicableMap)) {
$applicableMap[$id] = [];
}
if (!$row['circle_id']) {
$entityId = (string)$row['group_id'];
$entry = [
'displayName' => $row['group_id'],
'permissions' => (int)$row['permissions'],
'type' => 'group'
];
} else {
$entityId = (string)$row['circle_id'];
try {
$circle = $queryHelper?->extractCircle($row);
} catch (CircleNotFoundException) {
$circle = null;
}
$entry = [
'displayName' => $circle?->getDisplayName() ?? $row['circle_id'],
'permissions' => (int)$row['permissions'],
'type' => 'circle'
];
}
$applicableMap[$id][$entityId] = $entry;
}
return $applicableMap;
}
/**
* @throws Exception
* @return list<GroupFoldersGroup>
*/
private function getGroups(int $id): array {
$groups = $this->getAllApplicable()[$id] ?? [];
$groups = array_map(function ($gid) {
return $this->groupManager->get($gid);
}, array_keys($groups));
return array_map(function ($group) {
return [
'gid' => $group->getGID(),
'displayname' => $group->getDisplayName()
];
}, array_values(array_filter($groups)));
}
/**
* Check if the user is able to configure the advanced folder permissions. This
* is the case if the user is an admin, has admin permissions for the group folder
* app or is member of a group that can manage permissions for the specific folder.
* @throws Exception
*/
public function canManageACL(int $folderId, IUser $user): bool {
$userId = $user->getUId();
if ($this->groupManager->isAdmin($userId)) {
return true;
}
// Call private server api
if (class_exists('\OC\Settings\AuthorizedGroupMapper')) {
$authorizedGroupMapper = \OC::$server->get('\OC\Settings\AuthorizedGroupMapper');
$settingClasses = $authorizedGroupMapper->findAllClassesForUser($user);
if (in_array('OCA\GroupFolders\Settings\Admin', $settingClasses, true)) {
return true;
}
}
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('group_folders_manage')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('mapping_type', $query->createNamedParameter('user')))
->andWhere($query->expr()->eq('mapping_id', $query->createNamedParameter($userId)));
if ($query->executeQuery()->rowCount() === 1) {
return true;
}
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('group_folders_manage')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId)))
->andWhere($query->expr()->eq('mapping_type', $query->createNamedParameter('group')));
$groups = $query->executeQuery()->fetchAll();
foreach ($groups as $manageRule) {
if ($this->groupManager->isInGroup($userId, $manageRule['mapping_id'])) {
return true;
}
}
return false;
}
/**
* @throws Exception
* @return list<GroupFoldersGroup>
*/
public function searchGroups(int $id, string $search = ''): array {
$groups = $this->getGroups($id);
if ($search === '') {
return $groups;
}
return array_values(array_filter($groups, function ($group) use ($search) {
return (stripos($group['gid'], $search) !== false) || (stripos($group['displayname'], $search) !== false);
}));
}
/**
* @throws Exception
* @return list<GroupFoldersUser>
*/
public function searchUsers(int $id, string $search = '', int $limit = 10, int $offset = 0): array {
$groups = $this->getGroups($id);
$users = [];
foreach ($groups as $groupArray) {
$group = $this->groupManager->get($groupArray['gid']);
if ($group) {
$foundUsers = $this->groupManager->displayNamesInGroup($group->getGID(), $search, $limit, $offset);
foreach ($foundUsers as $uid => $displayName) {
if (!isset($users[$uid])) {
$users[$uid] = [
'uid' => $uid,
'displayname' => $displayName
];
}
}
}
}
return array_values($users);
}
/**
* @return list<InternalFolder>
* @throws Exception
*/
public function getFoldersForGroup(string $groupId, int $rootStorageId = 0): array {
$query = $this->connection->getQueryBuilder();
$query->select(
'f.folder_id',
'mount_point',
'quota',
'acl',
'c.fileid',
'c.storage',
'c.path',
'c.name',
'c.mimetype',
'c.mimepart',
'c.size',
'c.mtime',
'c.storage_mtime',
'c.etag',
'c.encrypted',
'c.parent'
)
->selectAlias('a.permissions', 'group_permissions')
->selectAlias('c.permissions', 'permissions')
->from('group_folders', 'f')
->innerJoin(
'f',
'group_folders_groups',
'a',
$query->expr()->eq('f.folder_id', 'a.folder_id')
)
->where($query->expr()->eq('a.group_id', $query->createNamedParameter($groupId)));
$this->joinQueryWithFileCache($query, $rootStorageId);
$result = $query->executeQuery()->fetchAll();
return array_map(function ($folder): array {
return [
'folder_id' => (int)$folder['folder_id'],
'mount_point' => (string)$folder['mount_point'],
'permissions' => (int)$folder['group_permissions'],
'quota' => $this->getRealQuota((int)$folder['quota']),
'acl' => (bool)$folder['acl'],
'rootCacheEntry' => (isset($folder['fileid'])) ? Cache::cacheEntryFromData($folder, $this->mimeTypeLoader) : null
];
}, $result);
}
/**
* @param string[] $groupIds
* @return list<InternalFolder>
* @throws Exception
*/
public function getFoldersForGroups(array $groupIds, int $rootStorageId = 0): array {
$query = $this->connection->getQueryBuilder();
$query->select(
'f.folder_id',
'mount_point',
'quota',
'acl',
'c.fileid',
'c.storage',
'c.path',
'c.name',
'c.mimetype',
'c.mimepart',
'c.size',
'c.mtime',
'c.storage_mtime',
'c.etag',
'c.encrypted',
'c.parent'
)
->selectAlias('a.permissions', 'group_permissions')
->selectAlias('c.permissions', 'permissions')
->from('group_folders', 'f')
->innerJoin(
'f',
'group_folders_groups',
'a',
$query->expr()->eq('f.folder_id', 'a.folder_id')
)
->where($query->expr()->in('a.group_id', $query->createParameter('groupIds')));
$this->joinQueryWithFileCache($query, $rootStorageId);
// add chunking because Oracle can't deal with more than 1000 values in an expression list for in queries.
$result = [];
foreach (array_chunk($groupIds, 1000) as $chunk) {
$query->setParameter('groupIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
$result = array_merge($result, $query->executeQuery()->fetchAll());
}
return array_map(function (array $folder): array {
return [
'folder_id' => (int)$folder['folder_id'],
'mount_point' => (string)$folder['mount_point'],
'permissions' => (int)$folder['group_permissions'],
'quota' => $this->getRealQuota((int)$folder['quota']),
'acl' => (bool)$folder['acl'],
'rootCacheEntry' => (isset($folder['fileid'])) ? Cache::cacheEntryFromData($folder, $this->mimeTypeLoader) : null
];
}, array_values($result));
}
/**
* @return list<InternalFolder>
* @throws Exception
*/
public function getFoldersFromCircleMemberships(IUser $user, int $rootStorageId = 0): array {
if (!$this->isCirclesAvailable($circlesManager)) {
return [];
}
try {
$federatedUser = $circlesManager->getLocalFederatedUser($user->getUID());
} catch (\Exception $e) {
return [];
}
$queryHelper = $circlesManager->getQueryHelper();
$query = $queryHelper->getQueryBuilder();
$query->select(
'f.folder_id',
'f.mount_point',
'f.quota',
'f.acl',
'c.fileid',
'c.storage',
'c.path',
'c.name',
'c.mimetype',
'c.mimepart',
'c.size',
'c.mtime',
'c.storage_mtime',
'c.etag',
'c.encrypted',
'c.parent'
)
->selectAlias('a.permissions', 'group_permissions')
->selectAlias('c.permissions', 'permissions')
->from('group_folders', 'f')
->innerJoin(
'f',
'group_folders_groups',
'a',
$query->expr()->eq('f.folder_id', 'a.folder_id')
);
$queryHelper->limitToInheritedMembers('a', 'circle_id', $federatedUser);
$this->joinQueryWithFileCache($query, $rootStorageId);
return array_map(function (array $folder): array {
return [
'folder_id' => (int)$folder['folder_id'],
'mount_point' => (string)$folder['mount_point'],
'permissions' => (int)$folder['group_permissions'],
'quota' => $this->getRealQuota((int)$folder['quota']),
'acl' => (bool)$folder['acl'],
'rootCacheEntry' => (isset($folder['fileid'])) ? Cache::cacheEntryFromData($folder, $this->mimeTypeLoader) : null
];
}, array_values($query->executeQuery()->fetchAll()));
}
/**
* @throws Exception
*/
public function createFolder(string $mountPoint): int {
$query = $this->connection->getQueryBuilder();
$query->insert('group_folders')
->values([
'mount_point' => $query->createNamedParameter($mountPoint),
'quota' => self::SPACE_DEFAULT,
]);
$query->executeStatement();
$id = $query->getLastInsertId();
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('A new groupfolder "%s" was created with id %d', [$mountPoint, $id]));
return $id;
}
/**
* @throws Exception
*/
public function addApplicableGroup(int $folderId, string $groupId): void {
$query = $this->connection->getQueryBuilder();
if ($this->isACircle($groupId)) {
$circleId = $groupId;
$groupId = '';
}
$query->insert('group_folders_groups')
->values([
'folder_id' => $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT),
'group_id' => $query->createNamedParameter($groupId),
'circle_id' => $query->createNamedParameter($circleId ?? ''),
'permissions' => $query->createNamedParameter(Constants::PERMISSION_ALL)
]);
$query->executeStatement();
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The group "%s" was given access to the groupfolder with id %d', [$groupId, $folderId]));
}
/**
* @throws Exception
*/
public function removeApplicableGroup(int $folderId, string $groupId): void {
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders_groups')
->where(
$query->expr()->eq(
'folder_id', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)
)
)
->andWhere(
$query->expr()->orX(
$query->expr()->eq('group_id', $query->createNamedParameter($groupId)),
$query->expr()->eq('circle_id', $query->createNamedParameter($groupId))
)
);
$query->executeStatement();
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The group "%s" was revoked access to the groupfolder with id %d', [$groupId, $folderId]));
}
/**
* @throws Exception
*/
public function setGroupPermissions(int $folderId, string $groupId, int $permissions): void {
$query = $this->connection->getQueryBuilder();
$query->update('group_folders_groups')
->set('permissions', $query->createNamedParameter($permissions, IQueryBuilder::PARAM_INT))
->where(
$query->expr()->eq(
'folder_id', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)
)
)
->andWhere(
$query->expr()->orX(
$query->expr()->eq('group_id', $query->createNamedParameter($groupId)),
$query->expr()->eq('circle_id', $query->createNamedParameter($groupId))
)
);
$query->executeStatement();
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The permissions of group "%s" to the groupfolder with id %d was set to %d', [$groupId, $folderId, $permissions]));
}
/**
* @throws Exception
*/
public function setManageACL(int $folderId, string $type, string $id, bool $manageAcl): void {
$query = $this->connection->getQueryBuilder();
if ($manageAcl === true) {
$query->insert('group_folders_manage')
->values([
'folder_id' => $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT),
'mapping_type' => $query->createNamedParameter($type),
'mapping_id' => $query->createNamedParameter($id)
]);
} else {
$query->delete('group_folders_manage')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('mapping_type', $query->createNamedParameter($type)))
->andWhere($query->expr()->eq('mapping_id', $query->createNamedParameter($id)));
}
$query->executeStatement();
$action = $manageAcl ? "given" : "revoked";
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The %s "%s" was %s acl management rights to the groupfolder with id %d', [$type, $id, $action, $folderId]));
}
/**
* @throws Exception
*/
public function removeFolder(int $folderId): void {
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)));
$query->executeStatement();
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The groupfolder with id %d was removed', [$folderId]));
}
/**
* @throws Exception
*/
public function setFolderQuota(int $folderId, int $quota): void {
$query = $this->connection->getQueryBuilder();
$query->update('group_folders')
->set('quota', $query->createNamedParameter($quota))
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId)));
$query->executeStatement();
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The quota for groupfolder with id %d was set to %d bytes', [$folderId, $quota]));
}
/**
* @throws Exception
*/
public function renameFolder(int $folderId, string $newMountPoint): void {
$query = $this->connection->getQueryBuilder();
$query->update('group_folders')
->set('mount_point', $query->createNamedParameter($newMountPoint))
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)));
$query->executeStatement();
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('The groupfolder with id %d was renamed to "%s"', [$folderId, $newMountPoint]));
}
/**
* @throws Exception
*/
public function deleteGroup(string $groupId): void {
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders_groups')
->where($query->expr()->eq('group_id', $query->createNamedParameter($groupId)));
$query->executeStatement();
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders_manage')
->where($query->expr()->eq('mapping_id', $query->createNamedParameter($groupId)))
->andWhere($query->expr()->eq('mapping_type', $query->createNamedParameter('group')));
$query->executeStatement();
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders_acl')
->where($query->expr()->eq('mapping_id', $query->createNamedParameter($groupId)))
->andWhere($query->expr()->eq('mapping_type', $query->createNamedParameter('group')));
$query->executeStatement();
}
/**
* @throws Exception
*/
public function deleteCircle(string $circleId): void {
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders_groups')
->where($query->expr()->eq('circle_id', $query->createNamedParameter($circleId)));
$query->executeStatement();
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders_acl')
->where($query->expr()->eq('mapping_id', $query->createNamedParameter($circleId)))
->andWhere($query->expr()->eq('mapping_type', $query->createNamedParameter('circle')));
$query->executeStatement();
}
/**
* @throws Exception
*/
public function setFolderACL(int $folderId, bool $acl): void {
$query = $this->connection->getQueryBuilder();
$query->update('group_folders')
->set('acl', $query->createNamedParameter((int)$acl, IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId)));
$query->executeStatement();
if ($acl === false) {
$query = $this->connection->getQueryBuilder();
$query->delete('group_folders_manage')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($folderId)));
$query->executeStatement();
}
$action = $acl ? "enabled" : "disabled";
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('Advanced permissions for the groupfolder with id %d was %s', [$folderId, $action]));
}
/**
* @return list<InternalFolder>
* @throws Exception
*/
public function getFoldersForUser(IUser $user, int $rootStorageId = 0): array {
$groups = $this->groupManager->getUserGroupIds($user);
$folders = array_merge(
$this->getFoldersForGroups($groups, $rootStorageId),
$this->getFoldersFromCircleMemberships($user, $rootStorageId)
);
$mergedFolders = [];
foreach ($folders as $folder) {
$id = $folder['folder_id'];
if (isset($mergedFolders[$id])) {
$mergedFolders[$id]['permissions'] |= $folder['permissions'];
} else {
$mergedFolders[$id] = $folder;
}
}
return array_values($mergedFolders);
}
/**
* @param IUser $user
* @param int $folderId
* @return int
* @throws Exception
*/
public function getFolderPermissionsForUser(IUser $user, int $folderId): int {
$groups = $this->groupManager->getUserGroupIds($user);
$folders = array_merge(
$this->getFoldersForGroups($groups),
$this->getFoldersFromCircleMemberships($user)
);
$permissions = 0;
foreach ($folders as $folder) {
if ($folderId === $folder['folder_id']) {
$permissions |= $folder['permissions'];
}
}
return $permissions;
}
/**
* returns if the groupId is in fact the singleId of an existing Circle
*
* @param string $groupId
*
* @return bool
*/
public function isACircle(string $groupId): bool {
if (!$this->isCirclesAvailable($circlesManager)) {
return false;
}
$circlesManager->startSuperSession();
$probe = new CircleProbe();
$probe->includeSystemCircles();
$probe->includeSingleCircles();
try {
$circlesManager->getCircle($groupId, $probe);
return true;
} catch (CircleNotFoundException $e) {
} catch (\Exception $e) {
$this->logger->warning('', ['exception' => $e]);
} finally {
$circlesManager->stopSession();
}
return false;
}
/**
* returns if the circles manager is available.
* also set the parameter.
*
* @param CirclesManager|null $circlesManager
*
* @return bool
*/
public function isCirclesAvailable(?CirclesManager &$circlesManager = null): bool {
try {
/** @var CirclesManager $circlesManager */
$circlesManager = Server::get(CirclesManager::class);
} catch (ContainerExceptionInterface | AutoloadNotAllowedException $e) {
return false;
}
return true;
}
private function getRealQuota(int $quota): int {
if ($quota === self::SPACE_DEFAULT) {
$defaultQuota = $this->config->getSystemValueInt('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED);
// Prevent setting the default quota option to be the default quota value creating an unresolvable self reference
if ($defaultQuota <= 0 && $defaultQuota !== FileInfo::SPACE_UNLIMITED) {
throw new \Exception('Default Groupfolder quota value ' . $defaultQuota . ' is not allowed');
}
return $defaultQuota;
}
return $quota;
}
}