summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Command/MoveCalendar.php
blob: 20d04bfdd6fe09c6ecddeb1e0a7719c82e85e18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
 * @author Thomas Citharel <tcit@tcit.fr>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */
namespace OCA\DAV\Command;

use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class MoveCalendar extends Command {

	/** @var IUserManager */
	protected $userManager;

	/** @var IGroupManager $groupManager */
	private $groupManager;

	/** @var \OCP\IDBConnection */
	protected $dbConnection;

	/** @var IL10N */
	protected $l10n;

	/** @var SymfonyStyle */
	private $io;

	/** @var CalDavBackend */
	private $caldav;

	const URI_USERS = 'principals/users/';

	/**
	 * @param IUserManager $userManager
	 * @param IGroupManager $groupManager
	 * @param IDBConnection $dbConnection
	 * @param IL10N $l10n
	 */
	function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection, IL10N $l10n) {
		parent::__construct();
		$this->userManager = $userManager;
		$this->groupManager = $groupManager;
		$this->dbConnection = $dbConnection;
		$this->l10n = $l10n;
	}

	protected function configure() {
		$this
			->setName('dav:move-calendar')
			->setDescription('Move a calendar from an user to another')
			->addArgument('name',
				InputArgument::REQUIRED,
				'Name of the calendar to move')
			->addArgument('userorigin',
				InputArgument::REQUIRED,
				'User who currently owns the calendar')
			->addArgument('userdestination',
				InputArgument::REQUIRED,
				'User who will receive the calendar')
			->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing shares with groups that the destination user is not in");
	}

	protected function execute(InputInterface $input, OutputInterface $output) {
		$userOrigin = $input->getArgument('userorigin');
		$userDestination = $input->getArgument('userdestination');

		$this->io = new SymfonyStyle($input, $output);

		if (in_array('system', [$userOrigin, $userDestination], true)) {
			throw new \InvalidArgumentException("User can't be system");
		}

		if (!$this->userManager->userExists($userOrigin)) {
			throw new \InvalidArgumentException("User <$userOrigin> is unknown.");
		}


		if (!$this->userManager->userExists($userDestination)) {
			throw new \InvalidArgumentException("User <$userDestination> is unknown.");
		}

		$principalBackend = new Principal(
			$this->userManager,
			$this->groupManager
		);
		$random = \OC::$server->getSecureRandom();
		$dispatcher = \OC::$server->getEventDispatcher();

		$name = $input->getArgument('name');
		$this->caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $random, $dispatcher);

		$calendar = $this->caldav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);

		if (null === $calendar) {
			/**  If we got no matching calendar with URI, let's try the display names */
			$suggestedUris = $this->caldav->findCalendarsUrisByDisplayName($name, self::URI_USERS . $userOrigin);
			if (count($suggestedUris) > 0) {
				$this->io->note('No calendar with this URI was found, but you may want to try with these?');
				$this->io->listing($suggestedUris);
			}
			throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>.");
		}

		if (null !== $this->caldav->getCalendarByUri(self::URI_USERS . $userDestination, $name)) {
			throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
		}

		$this->checkShares($calendar, $userDestination, $input->getOption('force'));

		$this->caldav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination);

		$this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>");
	}

	/**
	 * Check that user destination is member of the groups which whom the calendar was shared
	 * If we ask to force the migration, the share with the group is dropped
	 *
	 * @param $calendar
	 * @param $userDestination
	 * @param bool $force
	 */
	private function checkShares($calendar, $userDestination, $force = false)
	{
		$shares = $this->caldav->getShares($calendar['id']);
		foreach ($shares as $share) {
			list($prefix, $group) = str_split($share['href'], 28);
			if ('principal:principals/groups/' === $prefix && !$this->groupManager->isInGroup($userDestination, $group)) {
				if ($force) {
					$this->caldav->updateShares(new Calendar($this->caldav, $calendar, $this->l10n), [], ['href' => 'principal:principals/groups/' . $group]);
				} else {
					throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$group> with which the calendar <" . $calendar['uri'] . "> was shared. You may use -f to move the calendar while deleting this share.");
				}
			}
		}
	}
}