Skip to content

Commit

Permalink
Merge pull request #6668 from nextcloud/fix_6621
Browse files Browse the repository at this point in the history
Remove avatar on user deletion
  • Loading branch information
MorrisJobke authored Dec 11, 2017
2 parents ba3c608 + 05d2f61 commit 4597187
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,9 @@ public function getFileInfo($path, $includeMountPoints = true) {
$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);

$mount = Filesystem::getMountManager()->find($path);
if (!$mount) {
return false;
}
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($path);
if ($storage) {
Expand Down Expand Up @@ -1411,6 +1414,9 @@ public function getDirectoryContent($directory, $mimetype_filter = '') {
$path = $this->getAbsolutePath($directory);
$path = Filesystem::normalizePath($path);
$mount = $this->getMount($directory);
if (!$mount) {
return [];
}
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($path);
if ($storage) {
Expand Down
32 changes: 31 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@
use OCA\Theming\Util;
use OCP\Federation\ICloudIdManager;
use OCP\Authentication\LoginCredentials\IStore;
use OCP\Files\NotFoundException;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IServerContainer;
use OCP\ITempManager;
use OCP\Contacts\ContactsMenu\IActionFactory;
use OCP\IUser;
use OCP\Lock\ILockingProvider;
use OCP\Remote\Api\IApiFactory;
use OCP\Remote\IInstanceFactory;
Expand All @@ -133,6 +135,7 @@
use OCP\Share\IShareHelper;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class Server
Expand Down Expand Up @@ -348,6 +351,8 @@ public function __construct($webRoot, \OC\Config $config) {
$defaultTokenProvider = null;
}

$dispatcher = $c->getEventDispatcher();

$userSession = new \OC\User\Session($manager, $session, $timeFactory, $defaultTokenProvider, $c->getConfig(), $c->getSecureRandom(), $c->getLockdownManager());
$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
Expand All @@ -356,9 +361,10 @@ public function __construct($webRoot, \OC\Config $config) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
});
$userSession->listen('\OC\User', 'preDelete', function ($user) {
$userSession->listen('\OC\User', 'preDelete', function ($user) use ($dispatcher) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
$dispatcher->dispatch('OCP\IUser::preDelete', new GenericEvent($user));
});
$userSession->listen('\OC\User', 'postDelete', function ($user) {
/** @var $user \OC\User\User */
Expand Down Expand Up @@ -1122,6 +1128,8 @@ public function __construct($webRoot, \OC\Config $config) {
$memcacheFactory = $c->getMemCacheFactory();
return new InstanceFactory($memcacheFactory->createLocal('remoteinstance.'), $c->getHTTPClientService());
});

$this->connectDispatcher();
}

/**
Expand All @@ -1131,6 +1139,28 @@ public function getCalendarManager() {
return $this->query('CalendarManager');
}

private function connectDispatcher() {
$dispatcher = $this->getEventDispatcher();

// Delete avatar on user deletion
$dispatcher->addListener('OCP\IUser::preDelete', function(GenericEvent $e) {
$logger = $this->getLogger();
$manager = $this->getAvatarManager();
/** @var IUser $user */
$user = $e->getSubject();

try {
$avatar = $manager->getAvatar($user->getUID());
$avatar->remove();
} catch (NotFoundException $e) {
// no avatar to remove
} catch (\Exception $e) {
// Ignore exceptions
$logger->info('Could not cleanup avatar of ' . $user->getUID());
}
});
}

/**
* @return \OCP\Contacts\IManager
*/
Expand Down

0 comments on commit 4597187

Please sign in to comment.