Skip to content

Commit 78b48e4

Browse files
committed
Add an option to remove groups via occ
Signed-off-by: Denis Mosolov <denismosolov@gmail.com>
1 parent cb27f99 commit 78b48e4

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

core/Command/Group/Delete.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2018 Denis Mosolov <denismosolov@gmail.com>
5+
*
6+
* @author Denis Mosolov <denismosolov@gmail.com>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OC\Core\Command\Group;
26+
27+
use OC\Core\Command\Base;
28+
use OCP\IGroupManager;
29+
use Symfony\Component\Console\Input\InputArgument;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
class Delete extends Base {
34+
/** @var IGroupManager */
35+
protected $groupManager;
36+
37+
/**
38+
* @param IGroupManager $groupManager
39+
*/
40+
public function __construct(IGroupManager $groupManager) {
41+
$this->groupManager = $groupManager;
42+
parent::__construct();
43+
}
44+
45+
protected function configure() {
46+
$this
47+
->setName('group:delete')
48+
->setDescription('Remove a group')
49+
->addArgument(
50+
'groupid',
51+
InputArgument::REQUIRED,
52+
'Group name'
53+
);
54+
}
55+
56+
protected function execute(InputInterface $input, OutputInterface $output) {
57+
$gid = $input->getArgument('groupid');
58+
if ($gid === 'admin') {
59+
$output->writeln('<error>Group "' . $gid . '" could not be deleted.</error>');
60+
return 1;
61+
}
62+
if (! $this->groupManager->groupExists($gid)) {
63+
$output->writeln('<error>Group "' . $gid . '" does not exist.</error>');
64+
return 1;
65+
}
66+
$group = $this->groupManager->get($gid);
67+
if ($group->delete()) {
68+
$output->writeln('Group "' . $gid . '" was removed');
69+
} else {
70+
$output->writeln('<error>Group "' . $gid . '" could not be deleted. Please check the logs.</error>');
71+
return 1;
72+
}
73+
}
74+
}

core/register_command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
$application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
156156

157157
$application->add(new OC\Core\Command\Group\Add(\OC::$server->getGroupManager()));
158+
$application->add(new OC\Core\Command\Group\Delete(\OC::$server->getGroupManager()));
158159
$application->add(new OC\Core\Command\Group\ListCommand(\OC::$server->getGroupManager()));
159160
$application->add(new OC\Core\Command\Group\AddUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
160161
$application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@
543543
'OC\\Core\\Command\\Encryption\\Status' => $baseDir . '/core/Command/Encryption/Status.php',
544544
'OC\\Core\\Command\\Group\\Add' => $baseDir . '/core/Command/Group/Add.php',
545545
'OC\\Core\\Command\\Group\\AddUser' => $baseDir . '/core/Command/Group/AddUser.php',
546+
'OC\\Core\\Command\\Group\\Delete' => $baseDir . '/core/Command/Group/Delete.php',
546547
'OC\\Core\\Command\\Group\\ListCommand' => $baseDir . '/core/Command/Group/ListCommand.php',
547548
'OC\\Core\\Command\\Group\\RemoveUser' => $baseDir . '/core/Command/Group/RemoveUser.php',
548549
'OC\\Core\\Command\\Integrity\\CheckApp' => $baseDir . '/core/Command/Integrity/CheckApp.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
573573
'OC\\Core\\Command\\Encryption\\Status' => __DIR__ . '/../../..' . '/core/Command/Encryption/Status.php',
574574
'OC\\Core\\Command\\Group\\Add' => __DIR__ . '/../../..' . '/core/Command/Group/Add.php',
575575
'OC\\Core\\Command\\Group\\AddUser' => __DIR__ . '/../../..' . '/core/Command/Group/AddUser.php',
576+
'OC\\Core\\Command\\Group\\Delete' => __DIR__ . '/../../..' . '/core/Command/Group/Delete.php',
576577
'OC\\Core\\Command\\Group\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/Group/ListCommand.php',
577578
'OC\\Core\\Command\\Group\\RemoveUser' => __DIR__ . '/../../..' . '/core/Command/Group/RemoveUser.php',
578579
'OC\\Core\\Command\\Integrity\\CheckApp' => __DIR__ . '/../../..' . '/core/Command/Integrity/CheckApp.php',
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
/**
3+
* @copyright 2018, Denis Mosolov <denismosolov@gmail.com>
4+
*
5+
* @author Denis Mosolov <denismosolov@gmail.com>
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+
namespace Test\Core\Command\Group;
24+
25+
use OC\Core\Command\Group\Delete;
26+
use OCP\IGroup;
27+
use OCP\IGroupManager;
28+
use Symfony\Component\Console\Input\InputInterface;
29+
use Symfony\Component\Console\Output\OutputInterface;
30+
use Test\TestCase;
31+
32+
class DeleteTest extends TestCase {
33+
34+
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
35+
private $groupManager;
36+
37+
/** @var Delete */
38+
private $command;
39+
40+
/** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */
41+
private $input;
42+
43+
/** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */
44+
private $output;
45+
46+
public function setUp() {
47+
parent::setUp();
48+
49+
$this->groupManager = $this->createMock(IGroupManager::class);
50+
$this->command = new Delete($this->groupManager);
51+
52+
$this->input = $this->createMock(InputInterface::class);
53+
$this->output = $this->createMock(OutputInterface::class);
54+
}
55+
56+
public function testDoesNotExists() {
57+
$gid = 'myGroup';
58+
$this->input->method('getArgument')
59+
->willReturnCallback(function($arg) use ($gid) {
60+
if ($arg === 'groupid') {
61+
return $gid;
62+
}
63+
throw new \Exception();
64+
});
65+
$this->groupManager->method('groupExists')
66+
->with($gid)
67+
->willReturn(false);
68+
69+
$this->groupManager->expects($this->never())
70+
->method('get');
71+
$this->output->expects($this->once())
72+
->method('writeln')
73+
->with($this->equalTo('<error>Group "' . $gid . '" does not exist.</error>'));
74+
75+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
76+
}
77+
78+
public function testDeleteAdmin() {
79+
$gid = 'admin';
80+
$this->input->method('getArgument')
81+
->willReturnCallback(function($arg) use ($gid) {
82+
if ($arg === 'groupid') {
83+
return $gid;
84+
}
85+
throw new \Exception();
86+
});
87+
88+
$this->groupManager->expects($this->never())
89+
->method($this->anything());
90+
$this->output->expects($this->once())
91+
->method('writeln')
92+
->with($this->equalTo('<error>Group "' . $gid . '" could not be deleted.</error>'));
93+
94+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
95+
}
96+
97+
public function testDeleteFailed() {
98+
$gid = 'myGroup';
99+
$this->input->method('getArgument')
100+
->willReturnCallback(function($arg) use ($gid) {
101+
if ($arg === 'groupid') {
102+
return $gid;
103+
}
104+
throw new \Exception();
105+
});
106+
$group = $this->createMock(IGroup::class);
107+
$group->method('delete')
108+
->willReturn(false);
109+
$this->groupManager->method('groupExists')
110+
->with($gid)
111+
->willReturn(true);
112+
$this->groupManager->method('get')
113+
->with($gid)
114+
->willReturn($group);
115+
116+
$this->output->expects($this->once())
117+
->method('writeln')
118+
->with($this->equalTo('<error>Group "' . $gid . '" could not be deleted. Please check the logs.</error>'));
119+
120+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
121+
}
122+
123+
public function testDelete() {
124+
$gid = 'myGroup';
125+
$this->input->method('getArgument')
126+
->willReturnCallback(function($arg) use ($gid) {
127+
if ($arg === 'groupid') {
128+
return $gid;
129+
}
130+
throw new \Exception();
131+
});
132+
$group = $this->createMock(IGroup::class);
133+
$group->method('delete')
134+
->willReturn(true);
135+
$this->groupManager->method('groupExists')
136+
->with($gid)
137+
->willReturn(true);
138+
$this->groupManager->method('get')
139+
->with($gid)
140+
->willReturn($group);
141+
142+
$this->output->expects($this->once())
143+
->method('writeln')
144+
->with($this->equalTo('Group "' . $gid . '" was removed'));
145+
146+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
147+
}
148+
}

0 commit comments

Comments
 (0)