Skip to content

Commit dce3097

Browse files
author
Johannes Leuker
committed
Show group backends in occ group:list --info and group:info
Signed-off-by: Johannes Leuker <j.leuker@hosting.de>
1 parent 9fd72b0 commit dce3097

File tree

12 files changed

+401
-14
lines changed

12 files changed

+401
-14
lines changed

apps/user_ldap/lib/Group_Proxy.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828

2929
namespace OCA\User_LDAP;
3030

31+
use OCP\Group\Backend\INamedBackend;
3132
use OCP\Group\Backend\IGetDisplayNameBackend;
3233

33-
class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend {
34+
class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend {
3435
private $backends = [];
3536
private $refBackend = null;
3637

@@ -298,4 +299,13 @@ public function getNewLDAPConnection($gid) {
298299
public function getDisplayName(string $gid): string {
299300
return $this->handleRequest($gid, 'getDisplayName', [$gid]);
300301
}
302+
303+
/**
304+
* Backend name to be shown in group management
305+
* @return string the name of the backend to be shown
306+
* @since 22.0.0
307+
*/
308+
public function getBackendName(): string {
309+
return 'LDAP';
310+
}
301311
}

core/Command/Group/Info.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <j.leuker@hosting.de>
7+
*
8+
* @author Johannes Leuker <j.leuker@hosting.de>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OC\Core\Command\Group;
28+
29+
use OC\Core\Command\Base;
30+
use OCP\IGroup;
31+
use OCP\IGroupManager;
32+
use Symfony\Component\Console\Input\InputArgument;
33+
use Symfony\Component\Console\Input\InputInterface;
34+
use Symfony\Component\Console\Input\InputOption;
35+
use Symfony\Component\Console\Output\OutputInterface;
36+
37+
class Info extends Base {
38+
/** @var IGroupManager */
39+
protected $groupManager;
40+
41+
/**
42+
* @param IGroupManager $groupManager
43+
*/
44+
public function __construct(IGroupManager $groupManager) {
45+
$this->groupManager = $groupManager;
46+
parent::__construct();
47+
}
48+
49+
protected function configure() {
50+
$this
51+
->setName('group:info')
52+
->setDescription('Show information about a group')
53+
->addArgument(
54+
'groupid',
55+
InputArgument::REQUIRED,
56+
'Group id'
57+
)->addOption(
58+
'output',
59+
null,
60+
InputOption::VALUE_OPTIONAL,
61+
'Output format (plain, json or json_pretty, default is plain)',
62+
$this->defaultOutputFormat
63+
);
64+
}
65+
66+
protected function execute(InputInterface $input, OutputInterface $output): int {
67+
$gid = $input->getArgument('groupid');
68+
$group = $this->groupManager->get($gid);
69+
if (!$group instanceof IGroup) {
70+
$output->writeln('<error>Group "' . $gid . '" does not exist.</error>');
71+
return 1;
72+
} else {
73+
$groupOutput = [
74+
'groupID' => $gid,
75+
'displayName' => $group->getDisplayName(),
76+
'backends' => $group->getBackendNames(),
77+
];
78+
79+
$this->writeArrayInOutputFormat($input, $output, $groupOutput);
80+
return 0;
81+
}
82+
}
83+
}

core/Command/Group/ListCommand.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ protected function configure() {
5959
InputOption::VALUE_OPTIONAL,
6060
'Offset for retrieving groups',
6161
0
62+
)->addOption(
63+
'info',
64+
'i',
65+
InputOption::VALUE_NONE,
66+
'Show additional info (backend)'
6267
)->addOption(
6368
'output',
6469
null,
@@ -70,21 +75,31 @@ protected function configure() {
7075

7176
protected function execute(InputInterface $input, OutputInterface $output): int {
7277
$groups = $this->groupManager->search('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
73-
$this->writeArrayInOutputFormat($input, $output, $this->formatGroups($groups));
78+
$this->writeArrayInOutputFormat($input, $output, $this->formatGroups($groups, (bool)$input->getOption('info')));
7479
return 0;
7580
}
7681

7782
/**
7883
* @param IGroup[] $groups
7984
* @return array
8085
*/
81-
private function formatGroups(array $groups) {
86+
private function formatGroups(array $groups, bool $addInfo = false) {
8287
$keys = array_map(function (IGroup $group) {
8388
return $group->getGID();
8489
}, $groups);
85-
$values = array_map(function (IGroup $group) {
86-
return array_keys($group->getUsers());
87-
}, $groups);
90+
91+
if ($addInfo) {
92+
$values = array_map(function (IGroup $group) {
93+
return [
94+
'backends' => $group->getBackendNames(),
95+
'users' => array_keys($group->getUsers()),
96+
];
97+
}, $groups);
98+
} else {
99+
$values = array_map(function (IGroup $group) {
100+
return array_keys($group->getUsers());
101+
}, $groups);
102+
}
88103
return array_combine($keys, $values);
89104
}
90105
}

core/register_command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
$application->add(new OC\Core\Command\Group\ListCommand(\OC::$server->getGroupManager()));
187187
$application->add(new OC\Core\Command\Group\AddUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
188188
$application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
189+
$application->add(new OC\Core\Command\Group\Info(\OC::$server->get(\OCP\IGroupManager::class)));
189190

190191
$application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(), \OC::$server->getL10N('core')));
191192
$application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager()));

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@
351351
'OCP\\Group\\Backend\\IGroupDetailsBackend' => $baseDir . '/lib/public/Group/Backend/IGroupDetailsBackend.php',
352352
'OCP\\Group\\Backend\\IHideFromCollaborationBackend' => $baseDir . '/lib/public/Group/Backend/IHideFromCollaborationBackend.php',
353353
'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php',
354+
'OCP\\Group\\Backend\\INamedBackend' => $baseDir . '/lib/public/Group/Backend/INamedBackend.php',
354355
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => $baseDir . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
355356
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
356357
'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
@@ -833,6 +834,7 @@
833834
'OC\\Core\\Command\\Group\\Add' => $baseDir . '/core/Command/Group/Add.php',
834835
'OC\\Core\\Command\\Group\\AddUser' => $baseDir . '/core/Command/Group/AddUser.php',
835836
'OC\\Core\\Command\\Group\\Delete' => $baseDir . '/core/Command/Group/Delete.php',
837+
'OC\\Core\\Command\\Group\\Info' => $baseDir . '/core/Command/Group/Info.php',
836838
'OC\\Core\\Command\\Group\\ListCommand' => $baseDir . '/core/Command/Group/ListCommand.php',
837839
'OC\\Core\\Command\\Group\\RemoveUser' => $baseDir . '/core/Command/Group/RemoveUser.php',
838840
'OC\\Core\\Command\\Integrity\\CheckApp' => $baseDir . '/core/Command/Integrity/CheckApp.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
380380
'OCP\\Group\\Backend\\IGroupDetailsBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IGroupDetailsBackend.php',
381381
'OCP\\Group\\Backend\\IHideFromCollaborationBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IHideFromCollaborationBackend.php',
382382
'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php',
383+
'OCP\\Group\\Backend\\INamedBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/INamedBackend.php',
383384
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
384385
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
385386
'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
@@ -862,6 +863,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
862863
'OC\\Core\\Command\\Group\\Add' => __DIR__ . '/../../..' . '/core/Command/Group/Add.php',
863864
'OC\\Core\\Command\\Group\\AddUser' => __DIR__ . '/../../..' . '/core/Command/Group/AddUser.php',
864865
'OC\\Core\\Command\\Group\\Delete' => __DIR__ . '/../../..' . '/core/Command/Group/Delete.php',
866+
'OC\\Core\\Command\\Group\\Info' => __DIR__ . '/../../..' . '/core/Command/Group/Info.php',
865867
'OC\\Core\\Command\\Group\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/Group/ListCommand.php',
866868
'OC\\Core\\Command\\Group\\RemoveUser' => __DIR__ . '/../../..' . '/core/Command/Group/RemoveUser.php',
867869
'OC\\Core\\Command\\Integrity\\CheckApp' => __DIR__ . '/../../..' . '/core/Command/Integrity/CheckApp.php',

lib/private/Group/Database.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use OCP\Group\Backend\IGroupDetailsBackend;
5656
use OCP\Group\Backend\IRemoveFromGroupBackend;
5757
use OCP\Group\Backend\ISetDisplayNameBackend;
58+
use OCP\Group\Backend\INamedBackend;
5859
use OCP\IDBConnection;
5960

6061
/**
@@ -69,7 +70,8 @@ class Database extends ABackend implements
6970
IGetDisplayNameBackend,
7071
IGroupDetailsBackend,
7172
IRemoveFromGroupBackend,
72-
ISetDisplayNameBackend {
73+
ISetDisplayNameBackend,
74+
INamedBackend {
7375

7476
/** @var string[] */
7577
private $groupCache = [];
@@ -502,4 +504,13 @@ public function setDisplayName(string $gid, string $displayName): bool {
502504

503505
return true;
504506
}
507+
508+
/**
509+
* Backend name to be shown in group management
510+
* @return string the name of the backend to be shown
511+
* @since 21.0.0
512+
*/
513+
public function getBackendName(): string {
514+
return 'Database';
515+
}
505516
}

lib/private/Group/Group.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\Group\Backend\ICountDisabledInGroup;
3737
use OCP\Group\Backend\IGetDisplayNameBackend;
3838
use OCP\Group\Backend\IHideFromCollaborationBackend;
39+
use OCP\Group\Backend\INamedBackend;
3940
use OCP\Group\Backend\ISetDisplayNameBackend;
4041
use OCP\GroupInterface;
4142
use OCP\IGroup;
@@ -317,6 +318,24 @@ public function searchDisplayName($search, $limit = null, $offset = null) {
317318
return array_values($users);
318319
}
319320

321+
/**
322+
* Get the names of the backend classes the group is connected to
323+
*
324+
* @return string[]
325+
*/
326+
public function getBackendNames() {
327+
$backends = [];
328+
foreach ($this->backends as $backend) {
329+
if ($backend instanceof INamedBackend) {
330+
$backends[] = $backend->getBackendName();
331+
} else {
332+
$backends[] = get_class($backend);
333+
}
334+
}
335+
336+
return $backends;
337+
}
338+
320339
/**
321340
* delete the group
322341
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <j.leuker@hosting.de>.
4+
*
5+
* @author Johannes Leuker <j.leuker@hosting.de>.
6+
*
7+
* @license AGPL-3.0
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
23+
namespace OCP\Group\Backend;
24+
25+
/**
26+
* @since 22.0.0
27+
*/
28+
interface INamedBackend {
29+
30+
/**
31+
* Backend name to be shown in group management
32+
* @return string the name of the backend to be shown
33+
* @since 22.0.0
34+
*/
35+
public function getBackendName(): string;
36+
}

lib/public/IGroup.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ public function countDisabled();
129129
*/
130130
public function searchDisplayName($search, $limit = null, $offset = null);
131131

132+
/**
133+
* Get the names of the backends the group is connected to
134+
*
135+
* @return string[]
136+
* @since 22.0.0
137+
*/
138+
public function getBackendNames();
139+
132140
/**
133141
* delete the group
134142
*

0 commit comments

Comments
 (0)