From c222c8b054ddcf824f7a28f216f56730bc305d7d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 26 Mar 2020 17:08:56 +0100 Subject: [PATCH] Can delete all user groups --- src/controllers/AccountController.ts | 5 +++-- src/controllers/GroupsController.ts | 2 ++ src/helpers/AccountHelper.ts | 14 ++++++++++++++ src/helpers/GroupsHelper.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 6cfa0f3..bb6fd69 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -298,8 +298,9 @@ export class AccountController { public static async DeleteAccount(h: RequestHandler) { await h.needUserPostPassword("password"); - // TODO : implement - h.error(500, "Method not implemented yet."); + await AccountHelper.Delete(h.getUserId()); + + h.error(500, "Method not completely implemented yet."); } /** diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index d9a57f3..356e0de 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -414,6 +414,7 @@ export class GroupsController { h.error(404, "Membership not found!"); // If the user is an admin, he must not be the last admin of the group + // TODO : use new method if(userID == h.getUserId() && currUserMembership.level == GroupMembershipLevels.ADMINISTRATOR && await GroupsHelper.CountMembersAtLevel(groupID, GroupMembershipLevels.ADMINISTRATOR) == 1) h.error(401, "You are the last administrator of this group!"); @@ -533,6 +534,7 @@ export class GroupsController { const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS); const level = await GroupsHelper.GetMembershipLevel(groupID, h.getUserId()); + // TODO : use new method if(level == GroupMembershipLevels.ADMINISTRATOR && await GroupsHelper.CountMembersAtLevel(groupID, GroupMembershipLevels.ADMINISTRATOR) == 1) h.error(401, "You are the last administrator of the group!"); diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 0920d84..a9bdbf2 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -434,4 +434,18 @@ export class AccountHelper { return data; } + + /** + * Remove completely a user account + * + * @param userID Target user ID + */ + public static async Delete(userID: number) { + + // Delete all groups memberships + await GroupsHelper.DeleteAllUsersGroups(userID); + + // TODO : continue work + + } } \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 78ce794..ee6a8d4 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -392,6 +392,17 @@ export class GroupsHelper { } + /** + * Check out whether a user is the last admin of a user or not + * + * @param groupID Target group ID + * @param userID Target user ID + */ + private static async IsLastAdmin(groupID: number, userID: number) : Promise { + return await this.GetMembershipLevel(groupID, userID) == GroupMembershipLevels.ADMINISTRATOR + && await GroupsHelper.CountMembersAtLevel(groupID, GroupMembershipLevels.ADMINISTRATOR) == 1; + } + /** * Get the current access of a user to a group * @@ -617,6 +628,23 @@ export class GroupsHelper { }); } + /** + * Delete all the groups the user belongs to + * + * @param userID Target user ID + */ + public static async DeleteAllUsersGroups(userID: number) { + + // Process each group + for(const groupID of await this.GetListUser(userID)) { + if(await this.IsLastAdmin(groupID, userID)) + await this.Delete(groupID); + + else + await this.DeleteMember(groupID, userID); + } + } + /** * Turn a database row into a {GroupInfo} object *