Skip to content

Commit 1a5f2ed

Browse files
committed
include circles to acl
Signed-off-by: Maxence Lange <[email protected]>
1 parent a023ef9 commit 1a5f2ed

File tree

5 files changed

+142
-22
lines changed

5 files changed

+142
-22
lines changed

β€Žlib/ACL/UserMapping/UserMappingManager.php

+52-7
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,78 @@
88

99
namespace OCA\GroupFolders\ACL\UserMapping;
1010

11+
use OCA\Circles\CirclesManager;
12+
use OCA\Circles\Model\Circle;
13+
use OCP\AutoloadNotAllowedException;
1114
use OCP\IGroup;
1215
use OCP\IGroupManager;
1316
use OCP\IUser;
1417
use OCP\IUserManager;
18+
use OCP\Server;
19+
use Psr\Container\ContainerExceptionInterface;
20+
use Psr\Log\LoggerInterface;
1521

1622
class UserMappingManager implements IUserMappingManager {
1723
public function __construct(
1824
private IGroupManager $groupManager,
1925
private IUserManager $userManager,
26+
private LoggerInterface $logger,
2027
) {
2128
}
2229

2330
public function getMappingsForUser(IUser $user, bool $userAssignable = true): array {
2431
$groupMappings = array_values(array_map(fn (IGroup $group): UserMapping => new UserMapping('group', $group->getGID(), $group->getDisplayName()), $this->groupManager->getUserGroups($user)));
32+
$circleMappings = array_values(array_map(fn (Circle $circle): UserMapping => new UserMapping('circle', $circle->getSingleId(), $circle->getDisplayName()), $this->getUserCircles($user->getUID())));
2533

2634
return array_merge([
2735
new UserMapping('user', $user->getUID(), $user->getDisplayName()),
28-
], $groupMappings);
36+
], $groupMappings, $circleMappings);
2937
}
3038

3139
public function mappingFromId(string $type, string $id): ?IUserMapping {
32-
$mappingObject = ($type === 'group' ? $this->groupManager : $this->userManager)->get($id);
33-
if ($mappingObject) {
34-
$displayName = $mappingObject->getDisplayName();
35-
/** @var 'user'|'group' $type */
36-
return new UserMapping($type, $id, $displayName);
37-
} else {
40+
switch ($type) {
41+
case 'group':
42+
$displayName = $this->groupManager->get($id)?->getDisplayName();
43+
break;
44+
case 'user':
45+
$displayName = $this->userManager->get($id)?->getDisplayName();
46+
break;
47+
case 'circle':
48+
$displayName = 'qwerty';
49+
break;
50+
}
51+
if ($displayName === null) {
52+
return null;
53+
}
54+
55+
return new UserMapping($type, $id, $displayName);
56+
}
57+
58+
/**
59+
* returns list of circles a user is member of
60+
*/
61+
public function getUserCircles(string $userId): array {
62+
$circlesManager = $this->getCirclesManager();
63+
if ($circlesManager === null) {
64+
return [];
65+
}
66+
67+
$circlesManager->startSession($circlesManager->getLocalFederatedUser($userId));
68+
try {
69+
return $circlesManager->probeCircles();
70+
} catch (\Exception $e) {
71+
$this->logger->warning('', ['exception' => $e]);
72+
} finally {
73+
$circlesManager->stopSession();
74+
}
75+
76+
return [];
77+
}
78+
79+
public function getCirclesManager(): ?CirclesManager {
80+
try {
81+
return Server::get(CirclesManager::class);
82+
} catch (ContainerExceptionInterface|AutoloadNotAllowedException) {
3883
return null;
3984
}
4085
}

β€Žlib/Command/ListCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
106106
}, array_keys($folder['groups']), array_values($folder['groups']));
107107
$folder['groups'] = implode("\n", $groupStrings);
108108
$folder['acl'] = $folder['acl'] ? 'Enabled' : 'Disabled';
109-
$manageStrings = array_map(fn (array $manage): string => $manage['id'] . ' (' . $manage['type'] . ')', $folder['manage']);
109+
$manageStrings = array_map(fn (array $manage): string => $manage['displayname'] . ' (' . $manage['type'] . ')', $folder['manage']);
110110
$folder['manage'] = implode("\n", $manageStrings);
111111

112112
return $folder;

β€Žlib/Controller/FolderController.php

+2
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,13 @@ public function aclMappingSearch(int $id, string $search = ''): DataResponse {
492492
if ($this->manager->canManageACL($id, $this->user) === true) {
493493
$groups = $this->manager->searchGroups($id, $search);
494494
$users = $this->manager->searchUsers($id, $search);
495+
$circles = $this->manager->searchCircles($id, $search);
495496
}
496497

497498
return new DataResponse([
498499
'users' => $users,
499500
'groups' => $groups,
501+
'circles' => $circles
500502
]);
501503
}
502504
}

β€Žlib/Folder/FolderManager.php

+82-14
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
use OC\Files\Node\Node;
1212
use OCA\Circles\CirclesManager;
1313
use OCA\Circles\Exceptions\CircleNotFoundException;
14+
use OCA\Circles\Model\Circle;
15+
use OCA\Circles\Model\Member;
1416
use OCA\Circles\Model\Probes\CircleProbe;
1517
use OCA\GroupFolders\Mount\GroupMountPoint;
1618
use OCA\GroupFolders\ResponseDefinitions;
17-
use OCA\GroupFolders\Settings\Admin;
1819
use OCP\AutoloadNotAllowedException;
1920
use OCP\Constants;
2021
use OCP\DB\Exception;
@@ -36,6 +37,7 @@
3637

3738
/**
3839
* @psalm-import-type GroupFoldersGroup from ResponseDefinitions
40+
* @psalm-import-type GroupFoldersCircle from ResponseDefinitions
3941
* @psalm-import-type GroupFoldersUser from ResponseDefinitions
4042
* @psalm-import-type GroupFoldersAclManage from ResponseDefinitions
4143
* @psalm-import-type GroupFoldersApplicable from ResponseDefinitions
@@ -264,16 +266,33 @@ private function getManageAcl(array $mappings): array {
264266
];
265267
}
266268

267-
$group = Server::get(IGroupManager::class)->get($entry['mapping_id']);
268-
if ($group === null) {
269-
return null;
269+
if ($entry['mapping_type'] === 'group') {
270+
$group = Server::get(IGroupManager::class)->get($entry['mapping_id']);
271+
if ($group === null) {
272+
return null;
273+
}
274+
275+
return [
276+
'type' => 'group',
277+
'id' => $group->getGID(),
278+
'displayname' => $group->getDisplayName()
279+
];
270280
}
271281

272-
return [
273-
'type' => 'group',
274-
'id' => $group->getGID(),
275-
'displayname' => $group->getDisplayName()
276-
];
282+
if ($entry['mapping_type'] === 'circle') {
283+
$circle = $this->getCircle($entry['mapping_id']);
284+
if ($circle === null) {
285+
return null;
286+
}
287+
288+
return [
289+
'type' => 'circle',
290+
'id' => $circle->getSingleId(),
291+
'displayname' => $circle->getDisplayName()
292+
];
293+
}
294+
295+
return null;
277296
}, $mappings)));
278297
}
279298

@@ -401,6 +420,20 @@ private function getGroups(int $id): array {
401420
], array_values(array_filter($groups)));
402421
}
403422

423+
/**
424+
* @throws Exception
425+
* @return list<GroupFoldersCircle>
426+
*/
427+
private function getCircles(int $id): array {
428+
$circles = $this->getAllApplicable()[$id] ?? [];
429+
$circles = array_map(fn (string $singleId): ?Circle => $this->getCircle($singleId), array_keys($circles));
430+
431+
return array_map(fn(Circle $circle): array => [
432+
'sid' => $circle->getSingleId(),
433+
'displayname' => $circle->getDisplayName()
434+
], array_values(array_filter($circles)));
435+
}
436+
404437
/**
405438
* Check if the user is able to configure the advanced folder permissions. This
406439
* is the case if the user is an admin, has admin permissions for the group folder
@@ -460,6 +493,15 @@ public function searchGroups(int $id, string $search = ''): array {
460493
return array_values(array_filter($groups, fn (array $group): bool => (stripos($group['gid'], $search) !== false) || (stripos($group['displayname'], $search) !== false)));
461494
}
462495

496+
public function searchCircles(int $id, string $search = ''): array {
497+
$circles = $this->getCircles($id);
498+
if ($search === '') {
499+
return $circles;
500+
}
501+
502+
return array_values(array_filter($circles, fn (array $circle): bool => (stripos($circle['displayname'], $search) !== false)));
503+
}
504+
463505
/**
464506
* @throws Exception
465507
* @return list<GroupFoldersUser>
@@ -482,6 +524,27 @@ public function searchUsers(int $id, string $search = '', int $limit = 10, int $
482524
}
483525
}
484526

527+
foreach($this->getCircles($id) as $circleData) {
528+
$circle = $this->getCircle($circleData['sid']);
529+
if ($circle === null) {
530+
continue;
531+
}
532+
533+
foreach($circle->getInheritedMembers(false) as $member) {
534+
if ($member->getUserType() !== Member::TYPE_USER) {
535+
continue;
536+
}
537+
538+
$uid = $member->getUserId();
539+
if (!isset($users[$uid])) {
540+
$users[$uid] = [
541+
'uid' => $uid,
542+
'displayname' => $member->getDisplayName()
543+
];
544+
}
545+
}
546+
}
547+
485548
return array_values($users);
486549
}
487550

@@ -918,27 +981,32 @@ public function getFolderPermissionsForUser(IUser $user, int $folderId): int {
918981
* returns if the groupId is in fact the singleId of an existing Circle
919982
*/
920983
public function isACircle(string $groupId): bool {
984+
return ($this->getCircle($groupId) !== null);
985+
}
986+
987+
/**
988+
* returns the Circle from its single Id, or NULL if not available
989+
*/
990+
public function getCircle(string $groupId): ?Circle {
921991
$circlesManager = $this->getCirclesManager();
922992
if ($circlesManager === null) {
923-
return false;
993+
return null;
924994
}
925995

926996
$circlesManager->startSuperSession();
927997
$probe = new CircleProbe();
928998
$probe->includeSystemCircles();
929999
$probe->includeSingleCircles();
9301000
try {
931-
$circlesManager->getCircle($groupId, $probe);
932-
933-
return true;
1001+
return $circlesManager->getCircle($groupId, $probe);
9341002
} catch (CircleNotFoundException) {
9351003
} catch (\Exception $e) {
9361004
$this->logger->warning('', ['exception' => $e]);
9371005
} finally {
9381006
$circlesManager->stopSession();
9391007
}
9401008

941-
return false;
1009+
return null;
9421010
}
9431011

9441012
public function getCirclesManager(): ?CirclesManager {

β€Žlib/ResponseDefinitions.php

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
* displayname: string,
2424
* }
2525
*
26+
* @psalm-type GroupFoldersCircle = array{
27+
* sid: string,
28+
* displayname: string,
29+
* }
30+
*
2631
* @psalm-type GroupFoldersUser = array{
2732
* uid: string,
2833
* displayname: string,

0 commit comments

Comments
Β (0)