]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Deleting a user will remove all user files.
authornickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544>
Mon, 23 Nov 2009 19:43:49 +0000 (19:43 +0000)
committernickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544>
Mon, 23 Nov 2009 19:43:49 +0000 (19:43 +0000)
Fixes #607

git-svn-id: http://code.elgg.org/elgg/trunk@3700 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/filestore.php
engine/lib/users.php
engine/tests/objects/filestore.php

index 455d011d53d9965b8caf2a715d60b8cbeb42e3c0..4fa0dc96e3970fb6b309d77cc962fad8e65cc82f 100644 (file)
@@ -1296,6 +1296,57 @@ function file_set_page_owner($file) {
        }
 }
 
+/**
+ * Recursively delete a directory
+ *
+ * @param str $directory
+ */
+function delete_directory($directory) {
+       // sanity check: must be a directory
+       if (!$handle = opendir($directory)) {
+               return FALSE;
+       }
+       
+       // loop through all files
+       while (($file = readdir($handle)) !== FALSE) {
+               if (in_array($file, array('.', '..'))) {
+                       continue;
+               }
+               
+               $path = "$directory/$file";
+               if (is_dir($path)) {
+                       // recurse down through directory
+                       if (!delete_directory($path)) {
+                               return FALSE;
+                       }
+               } else {
+                       // delete file
+                       unlink($path);
+               }
+       }
+       
+       // remove empty directory
+       closedir($handle);
+       return rmdir($directory);
+}
+
+/**
+ * Removes all user files
+ *
+ * @param ElggUser $user
+ * @return void
+ */
+function clear_user_files($user) {
+       global $CONFIG;
+       
+       $time_created = date('Y/m/d', $user->time_created);
+       $file_path = "$CONFIG->dataroot$time_created/$user->guid";
+       if (file_exists($file_path)) {
+               delete_directory($file_path);
+       }
+}
+
+
 /// Variable holding the default datastore
 $DEFAULT_FILE_STORE = NULL;
 
index 313566dfd6fb2f16b5e674e2fac19cfa564dbbd3..d7d13bdfe9390c7dc3af4427c4da71d666d91ffe 100644 (file)
@@ -156,6 +156,7 @@ class ElggUser extends ElggEntity
                // Delete owned data
                clear_annotations_by_owner($this->guid);
                clear_metadata_by_owner($this->guid);
+               clear_user_files($this);
 
                // Delete entity
                return parent::delete();
index a262a7c3fb7489b280b6cdede533a1ea904b24a3..e2013718783bfd62e00a724049d1435ba3858f51 100644 (file)
@@ -69,18 +69,23 @@ class ElggCoreFilestoreTest extends ElggCoreUnitTest {
                $user = $this->createTestUser();
                $created = date('Y/m/d', $user->time_created);
                
-               // setup a test file; no need to save
+               // setup a test file
                $file = new ElggFile();
                $file->owner_guid = $user->guid;
                $file->setFilename('testing/filestore.txt');
+               $file->open('write');
+               $file->write('Testing!');
+               $this->assertTrue($file->close());
                
                // ensure filename and path is expected
                $filename = $file->getFilenameOnFilestore($file);
                $filepath = "$CONFIG->dataroot$created/$user->guid/testing/filestore.txt";
                $this->assertIdentical($filename, $filepath);
+               $this->assertTrue(file_exists($filepath));
                
-               // clean up user
+               // ensure file removed on user delete
                $user->delete();
+               $this->assertFalse(file_exists($filepath));
        }