1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

outsource user leftovers to delete worker

This commit is contained in:
Torsten Dittmann 2020-07-03 00:42:21 +02:00
parent 4c6a300a22
commit 7484bdc347
2 changed files with 34 additions and 24 deletions

View file

@ -199,14 +199,14 @@ $utopia->delete('/v1/users/:userId')
->label('abuse-limit', 100)
->param('userId', '', function () {return new UID();}, 'User unique ID.')
->action(
function ($userId) use ($response, $request, $projectDB) {
function ($userId) use ($response, $deletes, $projectDB) {
$user = $projectDB->getDocument($userId);
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
if (!$projectDB->deleteDocument($userId)) {
throw new Exception('Failed to remove file from DB', 500);
throw new Exception('Failed to remove user from DB', 500);
}
$reservedId = $projectDB->createDocument([
@ -221,28 +221,7 @@ $utopia->delete('/v1/users/:userId')
throw new Exception('Failed saving reserved id to DB', 500);
}
$tokens = $user->getAttribute('tokens', []);
foreach ($tokens as $token) {
if (!$projectDB->deleteDocument($token->getId())) {
throw new Exception('Failed to remove token from DB', 500);
}
}
$memberships = $projectDB->getCollection([
'limit' => 2000,
'offset' => 0,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS,
'userId='.$userId,
],
]);
foreach ($memberships as $membership) {
if (!$projectDB->deleteDocument($membership->getId())) {
throw new Exception('Failed to remove team membership from DB', 500);
}
}
$deletes->setParam('document', $user);
$response->noContent();
}

View file

@ -29,6 +29,9 @@ class DeletesV1
case Database::SYSTEM_COLLECTION_PROJECTS:
$this->deleteProject($document);
break;
case Database::SYSTEM_COLLECTION_USERS:
$this->deleteUser($document);
break;
default:
break;
@ -52,4 +55,32 @@ class DeletesV1
$uploads->delete($uploads->getRoot(), true);
$cache->delete($cache->getRoot(), true);
}
protected function deleteUser(Document $user)
{
global $projectDB;
$tokens = $user->getAttribute('tokens', []);
foreach ($tokens as $token) {
if (!$projectDB->deleteDocument($token->getId())) {
throw new Exception('Failed to remove token from DB', 500);
}
}
$memberships = $projectDB->getCollection([
'limit' => 2000, // TODO add members limit
'offset' => 0,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS,
'userId='.$user->getId(),
],
]);
foreach ($memberships as $membership) {
if (!$projectDB->deleteDocument($membership->getId())) {
throw new Exception('Failed to remove team membership from DB', 500);
}
}
}
}