1616use OCP \Files \Storage \IStorage ;
1717use OCP \User \Events \BeforeUserDeletedEvent ;
1818use OCP \User \Events \UserDeletedEvent ;
19+ use Psr \Log \LoggerInterface ;
1920
2021/** @template-implements IEventListener<BeforeUserDeletedEvent|UserDeletedEvent> */
2122class UserDeletedFilesCleanupListener implements IEventListener {
2223 /** @var array<string,IStorage> */
2324 private $ homeStorageCache = [];
2425
25- /** @var IMountProviderCollection */
26- private $ mountProviderCollection ;
27-
28- public function __construct (IMountProviderCollection $ mountProviderCollection ) {
29- $ this ->mountProviderCollection = $ mountProviderCollection ;
26+ public function __construct (
27+ private IMountProviderCollection $ mountProviderCollection ,
28+ private LoggerInterface $ logger ,
29+ ) {
3030 }
3131
3232 public function handle (Event $ event ): void {
33+ $ user = $ event ->getUser ();
34+
3335 // since we can't reliably get the user home storage after the user is deleted
3436 // but the user deletion might get canceled during the before event
3537 // we only cache the user home storage during the before event and then do the
3638 // action deletion during the after event
3739
3840 if ($ event instanceof BeforeUserDeletedEvent) {
39- $ userHome = $ this ->mountProviderCollection ->getHomeMountForUser ($ event ->getUser ());
41+ $ this ->logger ->debug ('Prepare deleting storage for user {userId} ' , ['userId ' => $ user ->getUID ()]);
42+
43+ $ userHome = $ this ->mountProviderCollection ->getHomeMountForUser ($ user );
4044 $ storage = $ userHome ->getStorage ();
4145 if (!$ storage ) {
4246 throw new \Exception ('Account has no home storage ' );
@@ -51,12 +55,14 @@ public function handle(Event $event): void {
5155 $ this ->homeStorageCache [$ event ->getUser ()->getUID ()] = $ storage ;
5256 }
5357 if ($ event instanceof UserDeletedEvent) {
54- if (!isset ($ this ->homeStorageCache [$ event -> getUser () ->getUID ()])) {
58+ if (!isset ($ this ->homeStorageCache [$ user ->getUID ()])) {
5559 throw new \Exception ('UserDeletedEvent fired without matching BeforeUserDeletedEvent ' );
5660 }
57- $ storage = $ this ->homeStorageCache [$ event -> getUser () ->getUID ()];
61+ $ storage = $ this ->homeStorageCache [$ user ->getUID ()];
5862 $ cache = $ storage ->getCache ();
5963 $ storage ->rmdir ('' );
64+ $ this ->logger ->debug ('Deleted storage for user {userId} ' , ['userId ' => $ user ->getUID ()]);
65+
6066 if ($ cache instanceof Cache) {
6167 $ cache ->clear ();
6268 } else {
0 commit comments