UserDeletedFilesCleanupListener.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Authentication\Listeners;
  25. use OC\Files\Cache\Cache;
  26. use OC\Files\Storage\Wrapper\Wrapper;
  27. use OCP\EventDispatcher\Event;
  28. use OCP\EventDispatcher\IEventListener;
  29. use OCP\Files\Config\IMountProviderCollection;
  30. use OCP\Files\Storage\IStorage;
  31. use OCP\User\Events\BeforeUserDeletedEvent;
  32. use OCP\User\Events\UserDeletedEvent;
  33. class UserDeletedFilesCleanupListener implements IEventListener {
  34. /** @var array<string,IStorage> */
  35. private $homeStorageCache = [];
  36. /** @var IMountProviderCollection */
  37. private $mountProviderCollection;
  38. public function __construct(IMountProviderCollection $mountProviderCollection) {
  39. $this->mountProviderCollection = $mountProviderCollection;
  40. }
  41. public function handle(Event $event): void {
  42. // since we can't reliably get the user home storage after the user is deleted
  43. // but the user deletion might get canceled during the before event
  44. // we only cache the user home storage during the before event and then do the
  45. // action deletion during the after event
  46. if ($event instanceof BeforeUserDeletedEvent) {
  47. $userHome = $this->mountProviderCollection->getHomeMountForUser($event->getUser());
  48. $storage = $userHome->getStorage();
  49. if (!$storage) {
  50. throw new \Exception("User has no home storage");
  51. }
  52. // remove all wrappers, so we do the delete directly on the home storage bypassing any wrapper
  53. while ($storage->instanceOfStorage(Wrapper::class)) {
  54. /** @var Wrapper $storage */
  55. $storage = $storage->getWrapperStorage();
  56. }
  57. $this->homeStorageCache[$event->getUser()->getUID()] = $storage;
  58. }
  59. if ($event instanceof UserDeletedEvent) {
  60. if (!isset($this->homeStorageCache[$event->getUser()->getUID()])) {
  61. throw new \Exception("UserDeletedEvent fired without matching BeforeUserDeletedEvent");
  62. }
  63. $storage = $this->homeStorageCache[$event->getUser()->getUID()];
  64. $cache = $storage->getCache();
  65. $storage->rmdir('');
  66. if ($cache instanceof Cache) {
  67. $cache->clear();
  68. } else {
  69. throw new \Exception("Home storage has invalid cache");
  70. }
  71. }
  72. }
  73. }