Skip to content

Commit a0b84bc

Browse files
authored
Merge pull request #10334 from denismosolov/group-add
Add options to create/remove groups via occ
2 parents 8563ab9 + 0b18e2c commit a0b84bc

File tree

7 files changed

+393
-0
lines changed

7 files changed

+393
-0
lines changed

core/Command/Group/Add.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2018 Denis Mosolov <[email protected]>
5+
*
6+
* @author Denis Mosolov <[email protected]>
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 Add 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:add')
48+
->setDescription('Add 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+
$group = $this->groupManager->get($gid);
59+
if ($group) {
60+
$output->writeln('<error>Group "' . $gid . '" already exists.</error>');
61+
return 1;
62+
} else {
63+
$group = $this->groupManager->createGroup($gid);
64+
$output->writeln('Created group "' . $group->getGID() . '"');
65+
}
66+
}
67+
}

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 <[email protected]>
5+
*
6+
* @author Denis Mosolov <[email protected]>
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
$application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager()));
155155
$application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
156156

157+
$application->add(new OC\Core\Command\Group\Add(\OC::$server->getGroupManager()));
158+
$application->add(new OC\Core\Command\Group\Delete(\OC::$server->getGroupManager()));
157159
$application->add(new OC\Core\Command\Group\ListCommand(\OC::$server->getGroupManager()));
158160
$application->add(new OC\Core\Command\Group\AddUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
159161
$application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@
541541
'OC\\Core\\Command\\Encryption\\SetDefaultModule' => $baseDir . '/core/Command/Encryption/SetDefaultModule.php',
542542
'OC\\Core\\Command\\Encryption\\ShowKeyStorageRoot' => $baseDir . '/core/Command/Encryption/ShowKeyStorageRoot.php',
543543
'OC\\Core\\Command\\Encryption\\Status' => $baseDir . '/core/Command/Encryption/Status.php',
544+
'OC\\Core\\Command\\Group\\Add' => $baseDir . '/core/Command/Group/Add.php',
544545
'OC\\Core\\Command\\Group\\AddUser' => $baseDir . '/core/Command/Group/AddUser.php',
546+
'OC\\Core\\Command\\Group\\Delete' => $baseDir . '/core/Command/Group/Delete.php',
545547
'OC\\Core\\Command\\Group\\ListCommand' => $baseDir . '/core/Command/Group/ListCommand.php',
546548
'OC\\Core\\Command\\Group\\RemoveUser' => $baseDir . '/core/Command/Group/RemoveUser.php',
547549
'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
@@ -571,7 +571,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
571571
'OC\\Core\\Command\\Encryption\\SetDefaultModule' => __DIR__ . '/../../..' . '/core/Command/Encryption/SetDefaultModule.php',
572572
'OC\\Core\\Command\\Encryption\\ShowKeyStorageRoot' => __DIR__ . '/../../..' . '/core/Command/Encryption/ShowKeyStorageRoot.php',
573573
'OC\\Core\\Command\\Encryption\\Status' => __DIR__ . '/../../..' . '/core/Command/Encryption/Status.php',
574+
'OC\\Core\\Command\\Group\\Add' => __DIR__ . '/../../..' . '/core/Command/Group/Add.php',
574575
'OC\\Core\\Command\\Group\\AddUser' => __DIR__ . '/../../..' . '/core/Command/Group/AddUser.php',
576+
'OC\\Core\\Command\\Group\\Delete' => __DIR__ . '/../../..' . '/core/Command/Group/Delete.php',
575577
'OC\\Core\\Command\\Group\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/Group/ListCommand.php',
576578
'OC\\Core\\Command\\Group\\RemoveUser' => __DIR__ . '/../../..' . '/core/Command/Group/RemoveUser.php',
577579
'OC\\Core\\Command\\Integrity\\CheckApp' => __DIR__ . '/../../..' . '/core/Command/Integrity/CheckApp.php',
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* @copyright 2018, Denis Mosolov <[email protected]>
4+
*
5+
* @author Denis Mosolov <[email protected]>
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\Add;
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 AddTest extends TestCase {
33+
34+
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
35+
private $groupManager;
36+
37+
/** @var Add */
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 Add($this->groupManager);
51+
52+
$this->input = $this->createMock(InputInterface::class);
53+
$this->input->method('getArgument')
54+
->willReturnCallback(function($arg) {
55+
if ($arg === 'groupid') {
56+
return 'myGroup';
57+
}
58+
throw new \Exception();
59+
});
60+
$this->output = $this->createMock(OutputInterface::class);
61+
}
62+
63+
public function testGroupExists() {
64+
$gid = 'myGroup';
65+
$group = $this->createMock(IGroup::class);
66+
$this->groupManager->method('get')
67+
->with($gid)
68+
->willReturn($group);
69+
70+
$this->groupManager->expects($this->never())
71+
->method('createGroup');
72+
$this->output->expects($this->once())
73+
->method('writeln')
74+
->with($this->equalTo('<error>Group "' . $gid . '" already exists.</error>'));
75+
76+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
77+
}
78+
79+
public function testAdd() {
80+
$gid = 'myGroup';
81+
$group = $this->createMock(IGroup::class);
82+
$group->method('getGID')
83+
->willReturn($gid);
84+
$this->groupManager->method('createGroup')
85+
->willReturn($group);
86+
87+
$this->groupManager->expects($this->once())
88+
->method('createGroup')
89+
->with($this->equalTo($gid));
90+
$this->output->expects($this->once())
91+
->method('writeln')
92+
->with($this->equalTo('Created group "' . $group->getGID() . '"'));
93+
94+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
95+
}
96+
97+
98+
}

0 commit comments

Comments
 (0)