Passed
Push — master ( 34c9b5...835e28 )
by Blizzz
15:25 queued 23s
created

ResetGroup::execute()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 27
c 1
b 0
f 0
nc 48
nop 2
dl 0
loc 39
rs 8.4444
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Côme Chilliet <[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
namespace OCA\User_LDAP\Command;
25
26
use OCA\User_LDAP\Group_Proxy;
27
use OCA\User_LDAP\GroupPluginManager;
28
use OCP\IGroup;
29
use OCP\IGroupManager;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Console\Helper\QuestionHelper;
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
use Symfony\Component\Console\Question\Question;
37
38
class ResetGroup extends Command {
39
	private IGroupManager $groupManager;
40
	private GroupPluginManager $pluginManager;
41
	private Group_Proxy $backend;
42
43
	public function __construct(
44
		IGroupManager $groupManager,
45
		GroupPluginManager $pluginManager,
46
		Group_Proxy $backend
47
	) {
48
		$this->groupManager = $groupManager;
49
		$this->pluginManager = $pluginManager;
50
		$this->backend = $backend;
51
		parent::__construct();
52
	}
53
54
	protected function configure(): void {
55
		$this
56
			->setName('ldap:reset-group')
57
			->setDescription('deletes an LDAP group independent of the group state in the LDAP')
58
			->addArgument(
59
				'gid',
60
				InputArgument::REQUIRED,
61
				'the group name as used in Nextcloud'
62
			)
63
			->addOption(
64
				'yes',
65
				'y',
66
				InputOption::VALUE_NONE,
67
				'do not ask for confirmation'
68
			);
69
	}
70
71
	protected function execute(InputInterface $input, OutputInterface $output): int {
72
		try {
73
			$gid = $input->getArgument('gid');
74
			$group = $this->groupManager->get($gid);
75
			if (!$group instanceof IGroup) {
76
				throw new \Exception('Group not found');
77
			}
78
			$backends = $group->getBackendNames();
79
			if (!in_array('LDAP', $backends)) {
80
				throw new \Exception('The given group is not a recognized LDAP group.');
81
			}
82
			if ($input->getOption('yes') === false) {
83
				/** @var QuestionHelper $helper */
84
				$helper = $this->getHelper('question');
85
				$q = new Question('Delete all local data of this group (y|N)? ');
86
				$input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
87
			}
88
			if ($input->getOption('yes') !== true) {
89
				throw new \Exception('Reset cancelled by operator');
90
			}
91
92
			// Disable real deletion if a plugin supports it
93
			$pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
94
			// Bypass groupExists test to force mapping deletion
95
			$this->backend->getLDAPAccess($gid)->connection->writeToCache('groupExists' . $gid, false);
96
			echo "calling delete $gid\n";
97
			if ($group->delete()) {
98
				$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
99
				return 0;
100
			}
101
		} catch (\Throwable $e) {
102
			if (isset($pluginManagerSuppressed)) {
103
				$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
104
			}
105
			$output->writeln('<error>' . $e->getMessage() . '</error>');
106
			return 1;
107
		}
108
		$output->writeln('<error>Error while resetting group</error>');
109
		return 2;
110
	}
111
}
112