You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDeletedTokenCleanupListener.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Authentication\Listeners;
  26. use OC\Authentication\Token\Manager;
  27. use OCP\EventDispatcher\Event;
  28. use OCP\EventDispatcher\IEventListener;
  29. use OCP\User\Events\UserDeletedEvent;
  30. use Psr\Log\LoggerInterface;
  31. use Throwable;
  32. /**
  33. * @template-implements IEventListener<\OCP\User\Events\UserDeletedEvent>
  34. */
  35. class UserDeletedTokenCleanupListener implements IEventListener {
  36. /** @var Manager */
  37. private $manager;
  38. /** @var LoggerInterface */
  39. private $logger;
  40. public function __construct(Manager $manager,
  41. LoggerInterface $logger) {
  42. $this->manager = $manager;
  43. $this->logger = $logger;
  44. }
  45. public function handle(Event $event): void {
  46. if (!($event instanceof UserDeletedEvent)) {
  47. // Unrelated
  48. return;
  49. }
  50. /**
  51. * Catch any exception during this process as any failure here shouldn't block the
  52. * user deletion.
  53. */
  54. try {
  55. $uid = $event->getUser()->getUID();
  56. $tokens = $this->manager->getTokenByUser($uid);
  57. foreach ($tokens as $token) {
  58. $this->manager->invalidateTokenById($uid, $token->getId());
  59. }
  60. } catch (Throwable $e) {
  61. $this->logger->error('Could not clean up auth tokens after user deletion: ' . $e->getMessage(), [
  62. 'exception' => $e,
  63. ]);
  64. }
  65. }
  66. }