nextcloud/apps/dav/lib/Command/DeleteCalendar.php

125 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
/**
*
* @copyright Copyright (c) 2021, Mattia Narducci (mattianarducci1@gmail.com)
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace OCA\DAV\Command;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\IConfig;
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;
class DeleteCalendar extends Command {
/** @var CalDavBackend */
private $calDav;
/** @var IConfig */
private $config;
/** @var IUserManager */
private $userManager;
/**
* @param CalDavBackend $calDav
* @param IConfig $config
* @param IUserManager $userManager
*/
public function __construct(
CalDavBackend $calDav,
IConfig $config,
IUserManager $userManager
) {
parent::__construct();
$this->calDav = $calDav;
$this->config = $config;
$this->userManager = $userManager;
}
protected function configure(): void {
$this
->setName('dav:delete-calendar')
->setDescription('Delete a dav calendar')
->addArgument('uid',
InputArgument::REQUIRED,
'User who owns the calendar')
->addArgument('name',
InputArgument::OPTIONAL,
'Name of the calendar to delete')
->addOption('birthday',
null,
InputOption::VALUE_NONE,
'Delete the birthday calendar')
->addOption('force',
'f',
InputOption::VALUE_NONE,
'Force delete skipping trashbin');
}
protected function execute(
InputInterface $input,
OutputInterface $output
): int {
/** @var string $user **/
$user = $input->getArgument('uid');
if (!$this->userManager->userExists($user)) {
throw new \InvalidArgumentException(
'User <' . $user . '> is unknown.');
}
$birthday = $input->getOption('birthday');
if ($birthday !== false) {
$name = BirthdayService::BIRTHDAY_CALENDAR_URI;
} else {
$name = $input->getArgument('name');
if (!$name) {
throw new \InvalidArgumentException(
'Please specify a calendar name or --birthday');
}
}
$calendar = $this->calDav->getCalendarByUri(
'principals/users/' . $user,
$name);
if ($calendar === null) {
throw new \InvalidArgumentException(
'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.');
}
if ($name === BirthdayService::BIRTHDAY_CALENDAR_URI) {
$this->config->setUserValue($user,
'dav', 'generateBirthdayCalendar', 'no');
}
$force = $input->getOption('force');
$this->calDav->deleteCalendar($calendar['id'], $force !== false);
return 0;
}
}