2020-06-15 16:09:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
2020-08-24 14:54:25 +02:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-12-16 14:54:15 +01:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-06-15 16:09:39 +02:00
|
|
|
*
|
|
|
|
* @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
|
2021-06-04 21:52:51 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-06-15 16:09:39 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2020-08-24 14:54:25 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2020-06-15 16:09:39 +02:00
|
|
|
*/
|
|
|
|
namespace OC\Authentication\Listeners;
|
|
|
|
|
|
|
|
use OC\Authentication\Token\Manager;
|
|
|
|
use OCP\EventDispatcher\Event;
|
|
|
|
use OCP\EventDispatcher\IEventListener;
|
|
|
|
use OCP\User\Events\UserDeletedEvent;
|
2020-10-12 17:14:25 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-06-15 16:09:39 +02:00
|
|
|
use Throwable;
|
|
|
|
|
2020-10-15 16:43:21 +02:00
|
|
|
/**
|
|
|
|
* @template-implements IEventListener<\OCP\User\Events\UserDeletedEvent>
|
|
|
|
*/
|
2020-06-15 16:09:39 +02:00
|
|
|
class UserDeletedTokenCleanupListener implements IEventListener {
|
|
|
|
/** @var Manager */
|
|
|
|
private $manager;
|
|
|
|
|
2020-10-12 17:14:25 +02:00
|
|
|
/** @var LoggerInterface */
|
2020-06-15 16:09:39 +02:00
|
|
|
private $logger;
|
|
|
|
|
|
|
|
public function __construct(Manager $manager,
|
2023-11-23 10:22:34 +01:00
|
|
|
LoggerInterface $logger) {
|
2020-06-15 16:09:39 +02:00
|
|
|
$this->manager = $manager;
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(Event $event): void {
|
|
|
|
if (!($event instanceof UserDeletedEvent)) {
|
|
|
|
// Unrelated
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Catch any exception during this process as any failure here shouldn't block the
|
|
|
|
* user deletion.
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
$uid = $event->getUser()->getUID();
|
|
|
|
$tokens = $this->manager->getTokenByUser($uid);
|
|
|
|
foreach ($tokens as $token) {
|
|
|
|
$this->manager->invalidateTokenById($uid, $token->getId());
|
|
|
|
}
|
|
|
|
} catch (Throwable $e) {
|
2020-10-12 17:14:25 +02:00
|
|
|
$this->logger->error('Could not clean up auth tokens after user deletion: ' . $e->getMessage(), [
|
|
|
|
'exception' => $e,
|
2020-06-15 16:09:39 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|