Skip to content

Commit

Permalink
Remove user from the storage setting or storage when user is deleted
Browse files Browse the repository at this point in the history
Remove user from the storage if there are multiple
users associated with the storage during deletion
of the user. Else remove the storage when user is
deleted, since the storage is only associated with
the user being deleted.

Signed-off-by: Sujith H <sharidasan@owncloud.com>
  • Loading branch information
sharidas committed Jun 21, 2018
1 parent 2df5b94 commit 0b9fd38
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 1 deletion.
30 changes: 30 additions & 0 deletions lib/private/Files/External/Service/GlobalStoragesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,34 @@ public function getStorageForAllUsers() {

return \array_combine($keys, $configs);
}

/**
* Deletes the external storages mounted to the user
*
* @param $userId
* @return bool
*/
public function deleteAllForUser($userId) {
$result = false;
$mounts = $this->getStorageForAllUsers();
foreach ($mounts as $mount) {
$applicableUsers = $mount->getApplicableUsers();
$id = $mount->getId();
if (\in_array($userId, $applicableUsers, true)) {
if (\count($applicableUsers) === 1) {
//As this storage is associated only with this user.
$this->removeStorage($id);
$result = true;
} else {
$storage = $this->getStorage($id);
$userIndex = \array_search($userId, $applicableUsers, true);
unset($applicableUsers[$userIndex]);
$storage->setApplicableUsers($applicableUsers);
$this->updateStorage($storage);
$result = true;
}
}
}
return $result;
}
}
20 changes: 20 additions & 0 deletions lib/private/Files/External/Service/UserStoragesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OC\Files\Filesystem;

use OCP\Files\Config\IUserMountCache;
use OCP\IUser;
use OCP\IUserSession;

use OCP\Files\External\IStorageConfig;
Expand Down Expand Up @@ -56,6 +57,7 @@ public function __construct(
IUserMountCache $userMountCache
) {
$this->userSession = $userSession;
$this->userMountCache = $userMountCache;
parent::__construct($backendService, $dbConfig, $userMountCache);
}

Expand Down Expand Up @@ -140,4 +142,22 @@ public function getVisibilityType() {
protected function isApplicable(IStorageConfig $config) {
return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === IStorageConfig::MOUNT_TYPE_PERSONAl;
}

/**
* Deletes the storages mounted to a user
* @param IUser $user
* @return bool
*/
public function deleteAllMountsForUser(IUser $user) {
$getUserMounts = $this->userMountCache->getMountsForUser($user);
$result = false;
if (\count($getUserMounts) > 0) {
foreach ($getUserMounts as $userMount) {
$id = $userMount->getStorageId();
$this->userMountCache->removeUserStorageMount($id, $user->getUID());
$result = true;
}
}
return $result;
}
}
6 changes: 5 additions & 1 deletion lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use OCP\IUserBackend;
use OCP\IUserSession;
use OCP\User\IChangePasswordBackend;
use OCP\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;

Expand Down Expand Up @@ -226,6 +225,11 @@ public function delete() {
// Delete the user's keys in preferences
\OC::$server->getConfig()->deleteAllUserValues($this->getUID());

// Delete all mount points for user
\OC::$server->getUserStoragesService()->deleteAllMountsForUser($this);
//Delete external storage or remove user from applicableUsers list
\OC::$server->getGlobalStoragesService()->deleteAllForUser($this->getUID());

// Delete user files in /data/
if ($homePath !== false) {
// FIXME: this operates directly on FS, should use View instead...
Expand Down
8 changes: 8 additions & 0 deletions lib/public/Files/External/Service/IGlobalStoragesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ interface IGlobalStoragesService extends IStoragesService {
* @since 10.0
*/
public function getStorageForAllUsers();

/**
* Deletes the external storages mounted to the user
* @param $userId
* @return bool
* @since 10.0.8
*/
public function deleteAllForUser($userId);
}
7 changes: 7 additions & 0 deletions lib/public/Files/External/Service/IUserStoragesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@
* @since 10.0
*/
interface IUserStoragesService extends IStoragesService {
/**
* Deletes the storages mounted to a user
* @param \OCP\IUser $user
* @return bool
* @since 10.0.8
*/
public function deleteAllMountsForUser(\OCP\IUser $user);
}
120 changes: 120 additions & 0 deletions tests/lib/Files/External/Service/GlobalStoragesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public function storageDataProvider() {
'priority' => 15,
],
],
// single user
[
[
'mountPoint' => 'mountpoint',
'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
'backendOptions' => [
'option1' => 'value1',
'option2' => 'value2',
'password' => 'testPassword',
],
'applicableUsers' => ['foo'],
'applicableGroups' => [],
'priority' => 15,
],
],
// some groups
[
[
Expand Down Expand Up @@ -160,6 +176,110 @@ public function testAddStorage($storageParams) {
$this->assertEquals($baseId + 1, $nextStorage->getId());
}

public function providesDeleteAllForUser() {
return [
//False test
[
[
]
],
// all users
[
[
'mountPoint' => 'mountpoint',
'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
'backendOptions' => [
'option1' => 'value1',
'option2' => 'value2',
'password' => 'testPassword',
],
'applicableUsers' => [],
'applicableGroups' => [],
'priority' => 15,
],
],
// multiple users
[
[
'mountPoint' => 'mountpoint',
'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
'backendOptions' => [
'option1' => 'value1',
'option2' => 'value2',
'password' => 'testPassword',
],
'applicableUsers' => ['user1', 'user2'],
'applicableGroups' => [],
'priority' => 15,
],
],
// single user
[
[
'mountPoint' => 'mountpoint',
'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
'backendOptions' => [
'option1' => 'value1',
'option2' => 'value2',
'password' => 'testPassword',
],
'applicableUsers' => ['foo'],
'applicableGroups' => [],
'priority' => 15,
],
],
// both users and groups
[
[
'mountPoint' => 'mountpoint',
'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
'backendOptions' => [
'option1' => 'value1',
'option2' => 'value2',
'password' => 'testPassword',
],
'applicableUsers' => ['user1', 'user2'],
'applicableGroups' => ['group1', 'group2'],
'priority' => 15,
],
],
];
}

/**
* @dataProvider providesDeleteAllForUser
*/
public function testDeleteAllForUser($storageParams) {
$this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
if (\count($storageParams) === 0) {
$this->assertFalse($this->service->deleteAllForUser('foo'));
} elseif (\count($storageParams['applicableUsers']) >= 1) {
$storage = $this->makeStorageConfig($storageParams);
$this->service->addStorage($storage);
if (isset($storageParams['applicableUsers'])) {
$initialApplicableUsers = \count($storageParams['applicableUsers']);
$this->assertTrue($this->service->deleteAllForUser($storageParams['applicableUsers'][0]));
if ($initialApplicableUsers > 1) {
$finalApplicableUsers = $this->service->getStorage($storage->getId())->getApplicableUsers();
if (isset($storageParams['applicableGroups'])) {
$finalApplicableGroups = $this->service->getStorage($storage->getId())->getApplicableGroups();
$this->assertEquals(\count($storageParams['applicableGroups']), \count($finalApplicableGroups));
}
$this->assertEquals(1, $initialApplicableUsers - \count($finalApplicableUsers));
\array_shift($storageParams['applicableUsers']);
$this->assertEquals([], \array_diff($storageParams['applicableUsers'], $finalApplicableUsers));
} else {
$storages = \count($this->service->getAllStorages());
$this->assertEquals(0, $storages);
}
}
}
}

/**
* @dataProvider storageDataProvider
*/
Expand Down

0 comments on commit 0b9fd38

Please sign in to comment.