Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit f88e39f5 authored by Christoph Wurst's avatar Christoph Wurst
Browse files

Delete appointment configs when the user is deleted

parent 50861878
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -24,18 +24,15 @@ declare(strict_types=1);
namespace OCA\Calendar\AppInfo;

use OCA\Calendar\Dashboard\CalendarWidget;
use OCA\Calendar\Listener\UserDeletedListener;
use OCA\Calendar\Profile\AppointmentsAction;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\User\Events\UserDeletedEvent;
use function method_exists;

/**
 * Class Application
 *
 * @package OCA\Calendar\AppInfo
 */
class Application extends App implements IBootstrap {

	/** @var string */
@@ -58,6 +55,8 @@ class Application extends App implements IBootstrap {
		if (method_exists($context, 'registerProfileLinkAction')) {
			$context->registerProfileLinkAction(AppointmentsAction::class);
		}

		$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
	}

	/**
+9 −0
Original line number Diff line number Diff line
@@ -116,4 +116,13 @@ class AppointmentConfigMapper extends QBMapper {

		return $qb->executeStatement();
	}

	public function deleteByUserId(string $uid): void {
		$qb = $this->db->getQueryBuilder();

		$qb->delete($this->tableName)
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR));

		$qb->executeStatement();
	}
}
+17 −0
Original line number Diff line number Diff line
@@ -57,6 +57,23 @@ class BookingMapper extends QBMapper {
		return $this->findEntity($qb);
	}

	public function deleteByUserId(string $uid) {
		$subQuery = $this->db->getQueryBuilder();
		$delete = $this->db->getQueryBuilder();
		$subQuery->select('id')
			->from('calendar_appt_configs')
			->where($delete->expr()->eq('user_id', $delete->createNamedParameter($uid), IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR);
		$delete->delete($this->getTableName())
			->where(
				$delete->expr()->in(
					'appt_config_id',
					$delete->createFunction($subQuery->getSQL()),
					IQueryBuilder::PARAM_INT_ARRAY
				)
			);
		return $delete->executeStatement();
	}

	/**
	 * @param int $validFor is subtracted from time() and then compared against 'created_at'.
	 * @throws DbException
+64 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

/*
 * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @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 <http://www.gnu.org/licenses/>.
 */

namespace OCA\Calendar\Listener;

use OCA\Calendar\Service\Appointments\AppointmentConfigService;
use OCA\Calendar\Service\Appointments\BookingService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\UserDeletedEvent;
use Psr\Log\LoggerInterface;

class UserDeletedListener implements IEventListener {

	/** @var AppointmentConfigService */
	private $appointmentConfigService;

	/** @var BookingService */
	private $bookingService;

	/** @var LoggerInterface */
	private $logger;

	public function __construct(AppointmentConfigService $appointmentConfigService,
								BookingService $bookingService,
								LoggerInterface $logger) {
		$this->appointmentConfigService = $appointmentConfigService;
		$this->bookingService = $bookingService;
		$this->logger = $logger;
	}

	public function handle(Event $event): void {
		if (!($event instanceof UserDeletedEvent)) {
			return;
		}

		$this->bookingService->deleteByUser($event->getUser());
		$this->appointmentConfigService->deleteByUser($event->getUser());

		$this->logger->info("Calendar appointments cleaned up for deleted user " . $event->getUser()->getUID());
	}
}
+5 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http;
use OCP\DB\Exception as DbException;
use OCP\IUser;
use OCP\Security\ISecureRandom;

class AppointmentConfigService {
@@ -201,4 +202,8 @@ class AppointmentConfigService {
			throw new ServiceException('Could not create new appointment', $e->getCode(), $e);
		}
	}

	public function deleteByUser(IUser $getUser): void {
		$this->mapper->deleteByUserId($getUser->getUID());
	}
}
Loading