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 6, 2018
1 parent 7ebf0cb commit d811617
Show file tree
Hide file tree
Showing 4 changed files with 147 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;
}
}
4 changes: 3 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,9 @@ public function delete() {
// Delete the user's keys in preferences
\OC::$server->getConfig()->deleteAllUserValues($this->getUID());

//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);
}
106 changes: 106 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,96 @@ 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'])) {
$this->assertTrue($this->service->deleteAllForUser($storageParams['applicableUsers'][0]));
}
}
}

/**
* @dataProvider storageDataProvider
*/
Expand Down

0 comments on commit d811617

Please sign in to comment.