Browse Source
better cleanup of user files on user deletion
better cleanup of user files on user deletion
Signed-off-by: Robin Appelman <robin@icewind.nl>pull/26792/head
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
7 changed files with 83 additions and 14 deletions
-
4core/Application.php
-
1lib/composer/composer/autoload_classmap.php
-
1lib/composer/composer/autoload_static.php
-
73lib/private/Authentication/Listeners/UserDeletedFilesCleanupListener.php
-
2lib/private/Files/Storage/Common.php
-
13lib/private/User/User.php
-
3tests/lib/User/UserTest.php
@ -0,0 +1,73 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
/** |
|||
* @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl> |
|||
* |
|||
* @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 OC\Authentication\Listeners; |
|||
|
|||
use OC\Files\Cache\Cache; |
|||
use OCP\EventDispatcher\Event; |
|||
use OCP\EventDispatcher\IEventListener; |
|||
use OCP\Files\Config\IMountProviderCollection; |
|||
use OCP\Files\Storage\IStorage; |
|||
use OCP\User\Events\BeforeUserDeletedEvent; |
|||
use OCP\User\Events\UserDeletedEvent; |
|||
|
|||
class UserDeletedFilesCleanupListener implements IEventListener { |
|||
/** @var array<string,IStorage> */ |
|||
private $homeStorageCache = []; |
|||
|
|||
/** @var IMountProviderCollection */ |
|||
private $mountProviderCollection; |
|||
|
|||
public function __construct(IMountProviderCollection $mountProviderCollection) { |
|||
$this->mountProviderCollection = $mountProviderCollection; |
|||
} |
|||
|
|||
public function handle(Event $event): void { |
|||
// since we can't reliably get the user home storage after the user is deleted
|
|||
// but the user deletion might get canceled during the before event
|
|||
// we only cache the user home storage during the before event and then do the
|
|||
// action deletion during the after event
|
|||
|
|||
if ($event instanceof BeforeUserDeletedEvent) { |
|||
$userHome = $this->mountProviderCollection->getHomeMountForUser($event->getUser()); |
|||
$storage = $userHome->getStorage(); |
|||
if (!$storage) { |
|||
throw new \Exception("User has no home storage"); |
|||
} |
|||
$this->homeStorageCache[$event->getUser()->getUID()] = $storage; |
|||
} |
|||
if ($event instanceof UserDeletedEvent) { |
|||
if (!isset($this->homeStorageCache[$event->getUser()->getUID()])) { |
|||
throw new \Exception("UserDeletedEvent fired without matching BeforeUserDeletedEvent"); |
|||
} |
|||
$storage = $this->homeStorageCache[$event->getUser()->getUID()]; |
|||
$cache = $storage->getCache(); |
|||
if ($cache instanceof Cache) { |
|||
$cache->clear(); |
|||
} else { |
|||
throw new \Exception("Home storage has invalid cache"); |
|||
} |
|||
$storage->rmdir(''); |
|||
} |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue