Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve user deletion #61

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@fastify/cors": "^8.2.0",
"@fastify/formbody": "^7.3.0",
"@fastify/swagger": "^8.1.0",
"@prisma/client": "4",
"@prisma/client": "4.16.2",
"@sentry/node": "^7.9.0",
"@sentry/tracing": "^7.9.0",
"@simplewebauthn/server": "^5.4.0",
Expand Down Expand Up @@ -54,7 +54,7 @@
"husky": "^4.3.0",
"lint-staged": "^10.5.1",
"prettier": "^2.1.2",
"prisma": "4",
"prisma": "4.16.2",
"ts-node": "^9.0.0"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/common/meiling/identity/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Email, Group, OAuthTokenType, Phone, prisma, User as UserModel, OAuthClient } from '@prisma/client';
import { Email, Group, OAuthTokenType, Phone, User as UserModel, OAuthClient } from '@prisma/client';
import { VerifiedAuthenticationResponse, VerifiedRegistrationResponse } from '@simplewebauthn/server/./dist';

Check warning on line 2 in src/common/meiling/identity/user.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

'VerifiedAuthenticationResponse' is defined but never used

Check warning on line 2 in src/common/meiling/identity/user.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'VerifiedAuthenticationResponse' is defined but never used

Check warning on line 2 in src/common/meiling/identity/user.ts

View workflow job for this annotation

GitHub Actions / build (17.x)

'VerifiedAuthenticationResponse' is defined but never used
import { AttestationFormat } from '@simplewebauthn/server/dist/helpers/decodeAttestationObject';
import { CredentialDeviceType } from '@simplewebauthn/typescript-types';
import bcrypt from 'bcryptjs';
Expand Down Expand Up @@ -299,7 +299,7 @@
id: getUserId(user),
},
},
data: data as any,

Check warning on line 302 in src/common/meiling/identity/user.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected any. Specify a different type

Check warning on line 302 in src/common/meiling/identity/user.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type

Check warning on line 302 in src/common/meiling/identity/user.ts

View workflow job for this annotation

GitHub Actions / build (17.x)

Unexpected any. Specify a different type
method: 'PASSWORD',
allowSingleFactor: false,
allowTwoFactor: false,
Expand Down
36 changes: 32 additions & 4 deletions src/routes/v1/admin/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,38 @@ const userAdminHandler = (app: FastifyInstance, opts: FastifyPluginOptions, done
// actually delete if admin requested permanent deletion
// note: this could lead possible uuid collision for other apps.
if (permanent) {
await getPrismaClient().user.delete({
where: {
id: uuid,
},
const userId = uuid;
await getPrismaClient().$transaction(async (tx) => {
await tx.email.deleteMany({ where: { userId } });
await tx.phone.deleteMany({ where: { userId } });
await tx.authentication.deleteMany({ where: { userId } });
await tx.oAuthClientSecrets.deleteMany({ where: { userId } });
await tx.policyConsent.deleteMany({ where: { userId } });

const authorizations = await tx.oAuthClientAuthorization.findMany({ where: { userId } });
for (const auth of authorizations) {
await tx.oAuthToken.deleteMany({ where: { authorizationId: auth.id } });
}
await tx.oAuthClientAuthorization.deleteMany({ where: { userId } });

await tx.user.update({
where: { id: userId },
data: { groups: { set: [] } },
});

const ownedClients = await tx.oAuthClient.findMany({ where: { owners: { some: { id: userId } } } });
for (const client of ownedClients) {
await tx.oAuthClient.update({
where: { id: client.id },
data: {
owners: {
disconnect: { id: userId },
},
},
});
}

await tx.user.delete({ where: { id: userId } });
});
}

Expand Down
36 changes: 18 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,22 @@
pvtsutils "^1.3.2"
tslib "^2.4.0"

"@prisma/client@4":
version "4.6.1"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-4.6.1.tgz#e8e1d347ecbff44158d21b6591bb99650c8503a8"
integrity sha512-M1+NNrMzqaOIxT7PBGcTs3IZo7d1EW/+gVQd4C4gUgWBDGgD9AcIeZnUSidgWClmpMSgVUdnVORjsWWGUameYA==
"@prisma/client@4.16.2":
version "4.16.2"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-4.16.2.tgz#3bb9ebd49b35c8236b3d468d0215192267016e2b"
integrity sha512-qCoEyxv1ZrQ4bKy39GnylE8Zq31IRmm8bNhNbZx7bF2cU5aiCCnSa93J2imF88MBjn7J9eUQneNxUQVJdl/rPQ==
dependencies:
"@prisma/engines-version" "4.6.1-3.694eea289a8462c80264df36757e4fdc129b1b32"
"@prisma/engines-version" "4.16.1-1.4bc8b6e1b66cb932731fb1bdbbc550d1e010de81"

"@prisma/engines-version@4.6.1-3.694eea289a8462c80264df36757e4fdc129b1b32":
version "4.6.1-3.694eea289a8462c80264df36757e4fdc129b1b32"
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-4.6.1-3.694eea289a8462c80264df36757e4fdc129b1b32.tgz#90a71bbdfd5172fc674009346a6ad6b84410cc0a"
integrity sha512-HUCmkXAU2jqp2O1RvNtbE+seLGLyJGEABZS/R38rZjSAafAy0WzBuHq+tbZMnD+b5OSCsTVtIPVcuvx1ySxcWQ==
"@prisma/engines-version@4.16.1-1.4bc8b6e1b66cb932731fb1bdbbc550d1e010de81":
version "4.16.1-1.4bc8b6e1b66cb932731fb1bdbbc550d1e010de81"
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-4.16.1-1.4bc8b6e1b66cb932731fb1bdbbc550d1e010de81.tgz#d3b5dcf95b6d220e258cbf6ae19b06d30a7e9f14"
integrity sha512-q617EUWfRIDTriWADZ4YiWRZXCa/WuhNgLTVd+HqWLffjMSPzyM5uOWoauX91wvQClSKZU4pzI4JJLQ9Kl62Qg==

"@prisma/engines@4.6.1":
version "4.6.1"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-4.6.1.tgz#ae31309cc0f600f2da22708697b3be4eb1e46f9e"
integrity sha512-3u2/XxvxB+Q7cMXHnKU0CpBiUK1QWqpgiBv28YDo1zOIJE3FCF8DI2vrp6vuwjGt5h0JGXDSvmSf4D4maVjJdw==
"@prisma/engines@4.16.2":
version "4.16.2"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-4.16.2.tgz#5ec8dd672c2173d597e469194916ad4826ce2e5f"
integrity sha512-vx1nxVvN4QeT/cepQce68deh/Turxy5Mr+4L4zClFuK1GlxN3+ivxfuv+ej/gvidWn1cE1uAhW7ALLNlYbRUAw==

"@sentry/core@7.20.1":
version "7.20.1"
Expand Down Expand Up @@ -2449,12 +2449,12 @@ prettier@^2.1.2:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==

prisma@4:
version "4.6.1"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-4.6.1.tgz#6c85fb667abed006a6b849c9c1ddd81d3f071b87"
integrity sha512-BR4itMCuzrDV4tn3e2TF+nh1zIX/RVU0isKtKoN28ADeoJ9nYaMhiuRRkFd2TZN8+l/XfYzoRKyHzUFXLQhmBQ==
prisma@4.16.2:
version "4.16.2"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-4.16.2.tgz#469e0a0991c6ae5bcde289401726bb012253339e"
integrity sha512-SYCsBvDf0/7XSJyf2cHTLjLeTLVXYfqp7pG5eEVafFLeT0u/hLFz/9W196nDRGUOo1JfPatAEb+uEnTQImQC1g==
dependencies:
"@prisma/engines" "4.6.1"
"@prisma/engines" "4.16.2"

process-warning@^1.0.0:
version "1.0.0"
Expand Down
Loading