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 Mar 7, 2018
1 parent 83f46ca commit f69b77c
Show file tree
Hide file tree
Showing 4 changed files with 148 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 @@ -183,4 +183,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 @@ -227,6 +226,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);
}
107 changes: 107 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 @@ 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 @@ -161,6 +177,97 @@ public function testAddStorage($storageParams) {
$this->assertEquals($baseId + 1, $nextStorage->getId());
}

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 f69b77c

Please sign in to comment.