Skip to content

Commit d9aa41f

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 d9aa41f

File tree

7 files changed

+371
-13
lines changed

7 files changed

+371
-13
lines changed

core/Command/Group/Info.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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('Add 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) {
70+
if (!$group instanceof IGroup) {
71+
$output->writeln('<error>Could not get group</error>');
72+
return 2;
73+
}
74+
75+
$group_output = [
76+
'groupID' => $gid,
77+
'displayName' => $group->getDisplayName(),
78+
'backends' => $group->getBackendNames(),
79+
];
80+
81+
$this->writeArrayInOutputFormat($input, $output, $group_output);
82+
return 0;
83+
} else {
84+
$output->writeln('<error>Group "' . $gid . '" does not exist.</error>');
85+
return 1;
86+
}
87+
return 0;
88+
}
89+
}

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 $add_info = 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 ($add_info) {
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->getGroupManager()));
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/private/Group/Group.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,43 @@ public function searchDisplayName($search, $limit = null, $offset = null) {
317317
return array_values($users);
318318
}
319319

320+
/**
321+
* Get the names of the backend classes the group is connected with
322+
*
323+
* @return string[]
324+
*/
325+
public function getBackendClassNames() {
326+
$backends = [];
327+
foreach ($this->backends as $backend) {
328+
$backends[] = get_class($backend);
329+
}
330+
return $backends;
331+
}
332+
333+
/**
334+
* Get the names of the backends the group is connected with
335+
*
336+
* @return string[]
337+
*/
338+
public function getBackendNames() {
339+
$backends = [];
340+
341+
foreach ($this->getBackendClassNames() as $classname) {
342+
switch ($classname) {
343+
case 'OC\Group\Database':
344+
$backends[] = 'Database';
345+
break;
346+
case 'OCA\User_LDAP\Group_Proxy':
347+
$backends[] = 'LDAP';
348+
break;
349+
default:
350+
$backends[] = $classname;
351+
break;
352+
}
353+
}
354+
return $backends;
355+
}
356+
320357
/**
321358
* delete the group
322359
*

lib/public/IGroup.php

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

132+
/**
133+
* Get the names of the backend classes the group is connected with
134+
*
135+
* @return string[]
136+
* @since 21.0.0
137+
*/
138+
public function getBackendClassNames();
139+
140+
141+
/**
142+
* Get the names of the backends the group is connected with
143+
*
144+
* @return string[]
145+
* @since 21.0.0
146+
*/
147+
public function getBackendNames();
148+
132149
/**
133150
* delete the group
134151
*
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* @copyright 2021, hosting.de, Johannes Leuker <j.leuker@hosting.de>
4+
*
5+
* @author Johannes Leuker <j.leuker@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Afferoq General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace Test\Core\Command\Group;
25+
26+
use OC\Core\Command\Group\Info;
27+
use OCP\IGroup;
28+
use OCP\IGroupManager;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Output\OutputInterface;
31+
use Test\TestCase;
32+
33+
class InfoTest extends TestCase {
34+
35+
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
36+
private $groupManager;
37+
38+
/** @var Info|\PHPUnit\Framework\MockObject\MockObject */
39+
private $command;
40+
41+
/** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
42+
private $input;
43+
44+
/** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
45+
private $output;
46+
47+
protected function setUp(): void {
48+
parent::setUp();
49+
50+
$this->groupManager = $this->createMock(IGroupManager::class);
51+
$this->command = $this->getMockBuilder(Info::class)
52+
->setConstructorArgs([$this->groupManager])
53+
->setMethods(['writeArrayInOutputFormat'])
54+
->getMock();
55+
56+
$this->input = $this->createMock(InputInterface::class);
57+
$this->output = $this->createMock(OutputInterface::class);
58+
}
59+
60+
public function testDoesNotExists() {
61+
$gid = 'myGroup';
62+
$this->input->method('getArgument')
63+
->willReturnCallback(function ($arg) use ($gid) {
64+
if ($arg === 'groupid') {
65+
return $gid;
66+
}
67+
throw new \Exception();
68+
});
69+
$this->groupManager->method('get')
70+
->with($gid)
71+
->willReturn(null);
72+
73+
$this->output->expects($this->once())
74+
->method('writeln')
75+
->with($this->equalTo('<error>Group "' . $gid . '" does not exist.</error>'));
76+
77+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
78+
}
79+
80+
public function testInfo() {
81+
$gid = 'myGroup';
82+
$this->input->method('getArgument')
83+
->willReturnCallback(function ($arg) use ($gid) {
84+
if ($arg === 'groupid') {
85+
return $gid;
86+
}
87+
throw new \Exception();
88+
});
89+
90+
$group = $this->createMock(IGroup::class);
91+
$group->method('getGID')->willReturn($gid);
92+
$group->method('getDisplayName')
93+
->willReturn('My Group');
94+
$group->method('getBackendNames')
95+
->willReturn(['Database']);
96+
97+
$this->groupManager->method('get')
98+
->with($gid)
99+
->willReturn($group);
100+
101+
$this->command->expects($this->once())
102+
->method('writeArrayInOutputFormat')
103+
->with(
104+
$this->equalTo($this->input),
105+
$this->equalTo($this->output),
106+
[
107+
'groupID' => 'myGroup',
108+
'displayName' => 'My Group',
109+
'backends' => ['Database'],
110+
]
111+
);
112+
113+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
114+
}
115+
}

0 commit comments

Comments
 (0)