🐛 Fix sessions could not remove after delete account success

This commit is contained in:
lethemanh
2025-05-15 11:55:42 +07:00
committed by Anton Shepilov
parent b3ead45ab8
commit 70f3298f01
5 changed files with 39 additions and 8 deletions
@@ -16,7 +16,7 @@ export class AdminDeleteUserController {
userId: string, userId: string,
): Promise<{ status: "failed" | "deleting" | "done"; userId?: string }> { ): Promise<{ status: "failed" | "deleting" | "done"; userId?: string }> {
try { try {
await gr.services.console.getClient().userWasDeletedForceLogout(userId); await gr.services.console.getClient().userWasDeletedForceLogout({ userId });
await gr.services.users.markToDeleted({ id: userId }); await gr.services.users.markToDeleted({ id: userId });
} catch (err) { } catch (err) {
adminLogger.error({ err, userId }, "[DELETE USER] Error dustin updating user "); adminLogger.error({ err, userId }, "[DELETE USER] Error dustin updating user ");
@@ -72,7 +72,7 @@ export interface ConsoleServiceClient {
* Similar to backChannelLogout, but must be called by administrative authorized * Similar to backChannelLogout, but must be called by administrative authorized
* code in case of user immediate kick out * code in case of user immediate kick out
*/ */
userWasDeletedForceLogout(userId: string): Promise<void>; userWasDeletedForceLogout(payload: { userId?: string; email?: string }): Promise<void>;
resendVerificationEmail(email: string): Promise<void>; resendVerificationEmail(email: string): Promise<void>;
} }
@@ -115,7 +115,7 @@ export class ConsoleInternalClient implements ConsoleServiceClient {
throw new Error("Method should not be implemented."); throw new Error("Method should not be implemented.");
} }
async userWasDeletedForceLogout(_userId: string) { async userWasDeletedForceLogout(_payload: { userId?: string; email?: string }) {
logger.info("Internal: userWasDeletedForceLogout"); logger.info("Internal: userWasDeletedForceLogout");
throw new Error("Method should not be implemented."); throw new Error("Method should not be implemented.");
} }
@@ -328,10 +328,32 @@ export class ConsoleRemoteClient implements ConsoleServiceClient {
} }
} }
async userWasDeletedForceLogout(userId: string) { async userWasDeletedForceLogout(payload: { userId?: string; email?: string }) {
const sessionRepository = gr.services.console.getSessionRepo(); const sessionRepository = gr.services.console.getSessionRepo();
if (!sessionRepository) return; if (!sessionRepository) return;
const sessions = (await sessionRepository.find({ sub: userId })).getEntities();
const { userId, email } = payload;
if (!userId && !email) {
logger.info("Missing query condition for session");
return;
}
/* Get sessions by userId or email
* Use condition to query for each criterias
* In case DB type is postgres we could not query by using IN as mongodb
* due to the function validate query of postgres do not allow to use array for column type string
*/
let sessions: Session[] = [];
if (userId) {
const results = (await sessionRepository.find({ sub: userId })).getEntities();
sessions = sessions.concat(results);
}
if (email) {
const results = (await sessionRepository.find({ sub: email })).getEntities();
sessions = sessions.concat(results);
}
logger.info({ sessions }, "Sessions to delete");
for (const session of sessions) for (const session of sessions)
if (!session.revoked_at) { if (!session.revoked_at) {
session.revoked_at = new Date().getTime(); session.revoked_at = new Date().getTime();
@@ -33,7 +33,7 @@ import gr from "../../../global-resolver";
import { TYPE as DriveFileType, DriveFile } from "../../../documents/entities/drive-file"; import { TYPE as DriveFileType, DriveFile } from "../../../documents/entities/drive-file";
import { UpdateUser } from "./types"; import { UpdateUser } from "./types";
import { formatUsername } from "../../../../utils/users"; import { formatUsername } from "../../../../utils/users";
import { logger } from "../../../../core/platform/framework"; import { Configuration, logger } from "../../../../core/platform/framework";
export class UserServiceImpl { export class UserServiceImpl {
version: "1"; version: "1";
@@ -44,6 +44,7 @@ export class UserServiceImpl {
driveFileRepository: Repository<DriveFile>; driveFileRepository: Repository<DriveFile>;
private deviceRepository: Repository<Device>; private deviceRepository: Repository<Device>;
private cache: NodeCache; private cache: NodeCache;
private configuration: Configuration;
async init(): Promise<this> { async init(): Promise<this> {
this.searchRepository = gr.platformServices.search.getRepository<User>("user", User); this.searchRepository = gr.platformServices.search.getRepository<User>("user", User);
@@ -62,6 +63,8 @@ export class UserServiceImpl {
this.cache = new NodeCache({ stdTTL: 0.2, checkperiod: 120 }); this.cache = new NodeCache({ stdTTL: 0.2, checkperiod: 120 });
this.configuration = new Configuration("general.accounts");
//If user deleted from Tdrive, remove it from all companies //If user deleted from Tdrive, remove it from all companies
localEventBus.subscribe<ResourceEventsPayload>("user:deleted", async data => { localEventBus.subscribe<ResourceEventsPayload>("user:deleted", async data => {
if (data?.user?.id) gr.services.companies.ensureDeletedUserNotInCompanies(data.user); if (data?.user?.id) gr.services.companies.ensureDeletedUserNotInCompanies(data.user);
@@ -124,7 +127,10 @@ export class UserServiceImpl {
const isChangeEmail = currentEmail !== body.email; const isChangeEmail = currentEmail !== body.email;
if (isChangeEmail) { if (isChangeEmail) {
await gr.services.console.getClient().userWasDeletedForceLogout(currentEmail); const accType = this.configuration.get("type");
if (accType === "remote") {
await gr.services.console.getClient().userWasDeletedForceLogout({ email: currentEmail });
}
const userByEmail = await gr.services.users.getByEmail(body.email, context); const userByEmail = await gr.services.users.getByEmail(body.email, context);
if (userByEmail) { if (userByEmail) {
throw CrudException.badRequest(`Email ${body.email} is existed`); throw CrudException.badRequest(`Email ${body.email} is existed`);
@@ -186,7 +192,10 @@ export class UserServiceImpl {
user.deleted = true; user.deleted = true;
user.delete_process_started_epoch = new Date().getTime(); user.delete_process_started_epoch = new Date().getTime();
await gr.services.console.getClient().userWasDeletedForceLogout(user.id); await gr.services.console.getClient().userWasDeletedForceLogout({
userId: user.id,
email: userCopy.email_canonical,
});
await this.save(user); await this.save(user);